Skip to content

ggplot2 - expand range?

3 messages · Pedro de Barros, Hadley Wickham

#
Dear UserRs,

I am trying to use systematically ggplot2 for most of my plots, but I 
am fighting some lack of documentation, which I try to overcome.

I want to build a scatterplot where the axes cross exactly at (0,0). 
I tried using scale_y_continuous(limits=c(0,10)), but I always get an 
extra space at the bottom of the axes. The code I used is below.

  > plotdata<-data.frame(x=1:10, y=runif(10))
  > plot<-ggplot()
  > plot<-plot+layer(data=plotdata, mapping=aes_string(x='x',y='y'),
geom='point', stat='identity', size=4)
  > plot<-plot+ scale_y_continuous(limits=c(0,1))
  > print(plot)

I believe there may be a way to use expand_range to fix this (I think 
I read about it somewhere), but I do not seem to find out how.
I have searched the ggplot help and
the archives, but cannot find the answer to this issue.
I would really appreciate if anyone could give me a pointer.

I am using R2.6.0 (patched) on Windows

Thanks,
Pedro
1 day later
#
Hi Pedro,

You need to set the expand parameter to c(0,0) :

scale_y_continuous(limits=c(0,10), expand = c(0,0))

ggplot uses expand_range internally to ensure that you always have
some space between the data and the margins (and you should make sure
this happens in your plot too - otherwise it can be hard to see data
points on the sides)

Hadley
On 11/1/07, Pedro de Barros <pbarros at ualg.pt> wrote:

  
    
#
Thanks!!!!!

I thought expand was already set to c(0,0) when not specifying it.

I agree I must be careful with the space left, but if a scale starts 
on 0, it becomes somewhat strange to see the axis start somewhere on 
negatives...
This is my problem. Normally the issue with extra space will arise on 
the top and right-side of the plots, not on the bottom or left side 
where the axes cross. But maybe there is another way to get this... Any hints?

Thanks again,
Pedro
At 16:40 2007/11/02, you wrote: