Setting only one limit in coord_cartesian

56 views
Skip to first unread message

Stavros Macrakis

unread,
Dec 24, 2009, 11:12:01 PM12/24/09
to ggplot2
I have some data which is inherently positive, and would like to ensure that all graphs in a facet-grid start with y=0, but scale appropriately on the high end.

For example, suppose I'm comparing the growth of hamburger and hot dog sales from year to year:

fr <-
  data.frame(
             year=rep(1:10,2),                                         # 2 runs of 10
             quant=(c(1:10,1:10*3)+rnorm(20,0,3))^2,     # some variation in dependent var
             type=rep(c('hamburgers','hot dogs'),each=10))     # first 10 and second 10 are separate series

and plot it with:

ggplot(fr,aes(x=year,y=quant)) +
  geom_point() +
  geom_smooth() +
  facet_grid(type~.,scales="free")

This is fine, except that the geom_smooth overflows into (meaningless) negative values.

So I'd like to do something like

   +  coord_cartesian(ylim=c(0,NA))    # causes internal error

to ensure that the lower y limit is always at 0 to make the graphs comparable.  I do, however, want to let the upper y limit be free.

How do I do this in ggplot?

              -s


Reply all
Reply to author
Forward
0 new messages