set upper limit in ggplot to include label greater than the maximum value

后端 未结 5 936

My data range always seems to be greater than the top label in the y-axis. Is there a way I can automatically include it without manually setting limits?

e.g. in

5条回答
  •  执念已碎
    2020-12-16 02:26

    What about the following solution:

    library(ggplot2)
    
    d <- data.frame(x=1:11, y=c(11:17,5:2))
    px <- pretty(d$x)
    py <- pretty(d$y)
    
    ggplot(d, aes(x,y)) + geom_point() +
      scale_x_continuous(breaks=px, limits=range(px)) +
      scale_y_continuous(breaks=py, limits=range(px))
    

    enter image description here

提交回复
热议问题