在ggplot中,Y轴不会从0开始

Dag*_*Dag 7 r ggplot2

在这个图中我命令ylim为0,但是y轴似乎从-1开始,这非常烦人.我真的很喜欢y轴到0的统计数据.解决方案?

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)
Run Code Online (Sandbox Code Playgroud)

另外,我有30个这样的图,y轴上有许多不同的范围.我需要一个通用代码,无论ymax是什么,都将文本放在图的左上角.可行?

Ric*_*ord 13

ggplot会自动略微扩展轴,以确保有绘图点的空间.您可以使用expand参数关闭此行为

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))
Run Code Online (Sandbox Code Playgroud)