10

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?

1
  • 1
    we have no idea what the data looks like. output of summary(table.popstat) or dput() of all or part of it wld be helpful
    – hrbrmstr
    Apr 16, 2016 at 20:07

2 Answers 2

24

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))
1
  • 1
    you have missed a parenthesis.
    – DJJ
    Apr 16, 2016 at 20:22
-1
p + scale_y_continuous(
  limits =   c(0,max_num),
  expand = expansion(mult = c(0,0.05))
)

this function ( expansion(mult = c(0,0.05))) may help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.