Alex Rivera | Logout

How do I make the y-axis intersect the x-axis at 0 in ggplot2?

Asked 2010-06-29T21:13:56.693
10

So when I make plots sometimes I see the y crossing x at some offset. I generated this figure using:

ggplot(data=d2,aes(y=log10(Nems+1),x=Time)) +
  geom_point(size=3,shape=1) +        
  geom_line(data=d2,aes(x=time_model,y=log10(value),group=variable,linetype=variable)) +
  ylim(0.001,2) + no_bg + draw_axis

I end up manually moving the y in Illustrator. Is there a way to just do it here?

alt text http://img816.imageshack.us/img816/7633/testzh.png

Edit
Report

1 Answer

6

Try adding this to your plot: + coord_cartesian(xlim = c(0, 90))

That should limit the x-axis to 0 through 90.

You could also do + xlim(0, 90), which has a similar effect - but also removes any data outside of its bounds from the dataset. That can be problematic if you're trying to zoom in on features of geoms that should be calculated using the whole dataset (e.g., smooths) because it recalculates those geoms based only on what's inside the limits. coord_cartesian() calculates all the geoms from the full dataset, then limits the window to what you specify.

answered 2010-06-29T21:20:56.607

Your Answer