Home > other >  How to point a triangle at the tip of a line [R]
How to point a triangle at the tip of a line [R]

Time:11-18

I am using a triangle to mark an event on a timeline in R, and I've given the coordinates to the specific position on the line where the event occurs in days. In the points( function, I have supplied pch=25 to create the "filled triangle" shape. However, the positioning of the character is based on the center of the triangle. Is it possible to use an argument like "pos" (i.e. pos=3) so that the triangle is positioned immediately above the line and points to to X coordinate of interest?

Example:

plot.new()
segments(0, 0.5, 1, 0.5)
points(0.5, 0.5, pch=25)

have

enter image description here

want

enter image description here

CodePudding user response:

I dont think there is an inherent function for this (i.e. pos-like function) but in the past, I have added a manual adjustment:

plot.new()

adj <- 0.015

segments(0, 0.5, 1, 0.5)
points(0.5, 0.5   adj, pch=25)

enter image description here

So with multiple points:

points(seq(0.1, 0.9, 0.1), rep(0.5, 9)   adj, pch = 25)

enter image description here

CodePudding user response:

Since R's interpreter supports a wide variety of encodings, and since the pch is just the text input, you can just paste the down triangle into the text editor and calculate:

strheight('▽') -> l

and change the last line to points(0.5, 0.5 l/2, pch=25) to get the desired

enter image description here

> strheight('▽')
[1] 0.1022132
  • Related