0

I want to add inner ticks to my plot given by a vector.

say my vector is myvec <- c(1,3,4:9, 12, 15) and my plot:

library(ggplot2)
df <- data.frame(x=seq(1:100), y=sort(rexp(100, 2), decreasing = T))

ggplot(df, aes(x=x, y=y)) + geom_point() + 
 scale_y_continuous(limits = c(0, 4))

I now want to add inside facing ticks at x= myvec, y=0 in blue color. How do I do it? Tried to work with this solution, but could not use the vector. Annotate ggplot with an extra tick and label

MAPK
  • 5,635
  • 4
  • 37
  • 88
  • quite related https://stackoverflow.com/questions/61348415/how-to-add-a-point-on-the-y-intercept-y-axis-using-ggplot2/61351882#61351882 – tjebo Apr 26 '20 at 12:51

1 Answers1

2

Try This:

df2<- cbind.data.frame("myvec" = myvec, z= rep(0, length(myvec)))

ggplot(df, aes(x=x, y=y)) + geom_point() + 
  scale_y_continuous(limits = c(0, 4)) +
  geom_point(data=df2, aes(x=myvec, y=z), shape = "\U2714", color = "blue", size= 2)

enter image description here

Mohanasundaram
  • 2,889
  • 1
  • 8
  • 18
  • 1
    Nearly there. Now just replace y=z with y =-Inf, and maybe also change the shape of the points, and then you'll have the ticks (you don't need to create a y variable in your data frame if y is always the same) – tjebo Apr 26 '20 at 12:47
  • P.S. As you implicitly suggest, one can use any character as a shape for geom_point. I'd probably still rather use geom_text, or even better, annotate, because you have more control about the shape of your characters. – tjebo Apr 26 '20 at 12:50
  • Thank you. It works for me as: `geom_point(data=df2, aes(x=myvec, y=z), shape = "|", color = "blue", size= 3)` – MAPK Apr 26 '20 at 18:46