Y轴在ggplot中不会从0开始 [英] Y axis won't start at 0 in ggplot

查看:1074
本文介绍了Y轴在ggplot中不会从0开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个图中,我命令ylim为0,但y轴似乎始于-1,这非常烦人。我真的很喜欢y坐标为0的解决方案吗?

In this plot I order the ylim to be 0, but the y axis seems to start at -1 anyway, which is very annoying. I really like the y axis to stat at 0. Solutions?

sub1=subset(table.popstat,POPSTAT==1,select=c(1,3))
ggplot(sub1, aes(x=YR,y=Freq)) + ylim(0,15) +
  geom_bar(stat='identity') + 
  annotate("text",x=3,y=14.9,label="Population status",cex=10)

另外,我有30个这样的图,y轴上有很多不同的范围。我需要一个通用代码,将文本放置在图的左上角,而不管ymax是什么。可以吗?

Also, I have 30 plots like this, and there are many different ranges on the y axis. I need a generic code that places the text in the upper left corner of the graph no matter what the ymax is. Doable?

推荐答案

ggplot会自动扩展轴以确保有空间可以绘制点。您可以使用扩展参数关闭此行为

ggplot automatically extends the axes slightly to make sure there is room for points to plot. You can turn this behaviour off with the expand argument

ggplot(sub1, aes(x=YR,y=Freq)) + 
  geom_bar(stat='identity') + 
  annotate("text",x=3,y=14.9,label="Population status",cex=10) +
  scale_y_continuous(expand = c(0, 0), limits = c(0, 15))

这篇关于Y轴在ggplot中不会从0开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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