1

I am trying to highlight the point with the lowest y value by attempting the following: 1) draw a line from this point down to the x-axis and another to the y-axis; and 2) add a manual tick mark with this point's x and y value on the x-axis and y-axis, respectively. This manual tick mark must be added in addition to the automatic tick marks on both axes.

Sample data:

df <- data.frame(x=1:100,y=rnorm(100,10,1))
ggplot(df) +
  geom_point(aes(x=x,y=y))

Edit:

Here's an illustration of what I am attempting:

enter image description here

Christian
  • 932
  • 1
  • 7
  • 22
  • Have a look at http://ggplot2.tidyverse.org/reference/annotate.html to get some inspiration and also http://www.cookbook-r.com/Graphs/Lines_(ggplot2)/ – infominer Apr 04 '18 at 17:15

1 Answers1

0

It's unclear exactly what you want this to look like but you could do one of two options. You could either use geom_vline() or geom_segment(). Vline will do a line from the bottom to the top, but it sounds like you may prefer to use segment. Try this:

+ geom_segment(x = min(x), xend = min(x), y = 0, yend = 1)

If you change the yend argument you could make the tick smaller or larger. Drawing one for the max value should be as simple as swapping the min() arguments for max() arguments. Or you could just input the values manually. Alternatively, you could add a vline to go the full height of the panel with:

+ geom_vline(xintercept = min(x))

You can read more about both here. If this doesn't help much, you can provide a proper reprex and maybe a sketch of your desired output we can modify that code to get a bit closer to what you want.

edit:

Writing outside of the plot window is a bit more difficult, but this link may help you. I've tried it on a few and always found that in my cases it was easier to use a different solution. Here's one option:

library(ggplot2)

set.seed(123)  # so we have the same toy data

df <- data.frame(x=1:100,y=rnorm(100,10,1))

ggplot(df) +
  geom_point(aes(x=x,y=y)) +
  geom_segment(x=0, xend=18, y=8.033383, yend=8.033383) +  # draw to x axis
  geom_segment(x=18, xend=18, y=0, yend=8.033383) +  # draw to y axis
  annotate("text", 18.2, 8.2, label="(8, 8.03)", size=3)  # ordered pair just above it

If you didn't want to draw all the way to the point you could just change the first xend and yend arguments where the x/y start at zero to be come just above the edge of the plot window.

Community
  • 1
  • 1
cparmstrong
  • 799
  • 6
  • 23
  • see the edited post: I have added an illustration. Thank you for the geom_segment code. Do you know how to add the manual tick marks on top of the automatically generated ones? – Christian Apr 05 '18 at 10:38
  • See my edit. I'm not as familiar with writing outside the plot window but I gave you a link that may help and a workaround. – cparmstrong Apr 05 '18 at 14:28
  • 1
    Thank you, seeellayewhy! – Christian Apr 06 '18 at 07:48