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

后端 未结 5 937

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:17

    dat<-data.frame(x=1:10, y=c(11:17,5:3))
    ggplot(dat, aes(x,y)) + 
      geom_point()+
      scale_y_continuous(breaks=c(pretty(dat$y, n=4),18), expand=c(0.25, 0.25))
    

    ggplot pretty breaks

    The pretty function chooses nice breaks as 1, 2 or 5 times a power of 10. Then I just included 18 to that vector.

提交回复
热议问题