Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

ggplot2

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

like image 764
Maiasaura Avatar asked Jun 29 '10 21:06

Maiasaura


2 Answers

Here is another solution:

... + scale_x_continuous(expand=c(0,0))

See also this related question: Margin adjustments when using ggplot’s geom_tile()

like image 72
rcs Avatar answered Sep 23 '22 12:09

rcs


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.

like image 33
Matt Parker Avatar answered Sep 22 '22 12:09

Matt Parker