R ggplot2: Labelling a horizontal line on the y axis with a numeric value

前端 未结 4 1528
春和景丽
春和景丽 2020-12-05 02:12

I have a horizontal line in a ggplot and I would like to label it\'s value (7.1) on the y axis.

library(ggplot2)
df <- data.frame(y=c(1:10),x=c(1:10))
h &         


        
4条回答
  •  温柔的废话
    2020-12-05 02:51

    It's not clear if you want 7.1 to be part of the y-axis, or if you just want a way to label the line. Assuming the former, you can use scale_y_continuous() to define your own breaks. Something like this may do what you want (will need some fiddling most likely):

    plot1+ geom_hline(aes(yintercept=h)) + 
      scale_y_continuous(breaks = sort(c(seq(min(df$y), max(df$y), length.out=5), h)))
    

    enter image description here

    Assuming the latter, this is probably more what you want:

    plot1 + geom_hline(aes(yintercept=h)) +
      geom_text(aes(0,h,label = h, vjust = -1))
    

    enter image description here

提交回复
热议问题