在ggplot中设置上限以包括大于最大值的标签 [英] set upper limit in ggplot to include label greater than the maximum value

查看:877
本文介绍了在ggplot中设置上限以包括大于最大值的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据范围似乎总是大于y轴上的顶部标签。有没有一种方法可以自动包含它,而无需手动设置限制?



例如。 in

  ggplot(data.frame(x = 1:11,y = c(11:17,5:2)) ,aes(x,y))+ geom_point()

我想要标签0和20出现在y轴上,o和12出现在x轴上(并且该图相应地展开)。例如,这应该是自动完成的,而不用在比例选项中指定这些数字。

解决方案

如何解决以下问题:

pre $ library(ggplot2)

d < - data.frame(x = 1:11,y = c(11:17,5: 2))
px< - pretty(d $ x)
py< - 漂亮(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))


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

ggplot(data.frame(x=1:11, y=c(11:17,5:2)), aes(x,y)) + geom_point()

I would like to have labels 0 and 20 appear on the y-axis and o and 12 appear on the x-axis (and the plot expand accordingly). This should be done automatically without specifying these numbers in scale options for example.

解决方案

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))

这篇关于在ggplot中设置上限以包括大于最大值的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆