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

后端 未结 5 939

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

    So I don't think expand is actually the way to go about this, rather expand_limits. This is clearly not the most beautiful code, but this is basically the functionality I'm looking for where the labels on the y-axis encompass the data completely.

    dat<-data.frame(x=1:10, y=c(11:17,5:3))
    ggplot(dat, aes(x,y)) + 
    geom_point() + 
    expand_limits(y=c(min(pretty(c(dat$y, min(dat$y) * (0.95)))), max(pretty(c(dat$y, max(dat$y) * (1.05))))))
    

    scales completely surrounding data

    I've assumed expand default as 0.05 and that pretty is used with defaults. Is there a better way to do this?

提交回复
热议问题