y轴限制条形图

时间:2014-04-21 22:45:31

标签: r graph ggplot2 axis-labels

我可以显示条形图,但y轴始终从零开始,这对这些数据没有意义:

y <- data.frame(x=c("a","b","c","d","e","f"), y=c(500,501,502,503,504,505))
ggplot(y, aes(x=x, y=y)) +
  stat_summary(fun.y=mean, geom="bar")

理想情况下,R会像往常一样自动设置轴限制。如果我尝试手动设置,如下所示,我的栏会消失。知道为什么吗?

y <- data.frame(x=c("a","b","c","d","e","f"), y=c(500,501,502,503,504,505))
ggplot(y, aes(x=x, y=y)) +
  stat_summary(fun.y=mean, geom="bar") +
  scale_y_continuous(limits=c(490,510))   

1 个答案:

答案 0 :(得分:0)

我认为您可以使用ylim,如下所示:

y <- data.frame(x=c("a","b","c","d","e","f"), y=c(500,501,502,503,504,505))
ggplot(y, aes(x=x, y=y)) + 
  stat_summary(fun.y=mean, geom="bar") + coord_cartesian(ylim=c(490,510))

结果输出如下:

enter image description here