Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

r - Y axis won't start at 0 in ggplot

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)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...