1

Consider the following function:

plotSmth <- function(...) ggplot2::ggplot(...) + 
  expand_limits(x =c(0), y =0) 

Is it possible to override the values set by the expand_limits() of the plotSmth

I tried something like:

plotSmth(thisIsData, aes(x=xhere, y=yhere, colour=as.factor(foo), lty=as.factor(bar)  )  ) +
  expand_limits(x=c(10), y=c(0, 25))

but it still did not override the 0 for the x axis set in the plotSmth's expand_limits().

Is there a way to achieve this?

cross
  • 1,018
  • 13
  • 32
  • 2
    Can you please provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610) with sample input data and clearly describe what the desired output is? – MrFlick Jul 11 '15 at 19:45

1 Answers1

2

expand_limits adds a dummy layer with data to stretch the axes, so you cannot undo that easily. Three options come to mind:

  1. don't use expand_limits in plotSmth, maybe xlim()/ylim() would do the job instead, and those can be reset;
  2. force the limits with e.g. coord_cartesian();
  3. remove the dummy layer

.

p = qplot(1:2,1:2) + expand_limits(x=-1e3)
p
p$layers[2] <- NULL
p
cross
  • 1,018
  • 13
  • 32
baptiste
  • 75,767
  • 19
  • 198
  • 294