7

How can I remove the plot area below the x and y axis in ggplot2 (see example below). I have tried several of the theme elements (panel.border, panel.margin, plot.margin) without any luck.

p <- ggplot(mtcars, aes(x = wt, y = mpg,xmin=0,ymin=0)) + geom_point()

enter image description here

Elizabeth
  • 6,391
  • 17
  • 62
  • 90

1 Answers1

9

Use the expand argument in continuous scale aesthetics...

p <- ggplot(mtcars, aes(x = wt, y = mpg,xmin=0,ymin=0)) +
geom_point()+
scale_x_continuous( expand = c(0,0) , limits = c(0,6) )+
scale_y_continuous( expand = c(0,0), limits = c(0,35) )

Set limits to avoid extreme values being cut off. enter image description here

but in case you wanted no margin around the entire plot, you need to use the theme element, plot.margin, like so (notice in the plot below the extreme right edge is cut to zero)..

require(grid) # for unit
p + theme( plot.margin = unit( c(0,0,0,0) , "in" ) )

enter image description here

Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
  • 2
    @Simon0101, If that's the answer, then I'm going to vote to close the question as a duplicate of [this](http://stackoverflow.com/questions/13701347/force-the-origin-to-start-at-0-in-ggplot2-r) especially since the other question is much more explicit than "remove negative plot area".... Still waiting for the OP to clarify though... – A5C1D2H2I1M1N2O1R2T1 Aug 15 '13 at 11:33
  • Hmmm..maybe keep this entry after all since now it is more informative than the existing entry – Elizabeth Aug 15 '13 at 11:48
  • I agree, if that is what you are after, because the latter is not answered in the linked question. Maybe you should retract your own clsoe vote! :-) Cheers! – Simon O'Hanlon Aug 15 '13 at 11:49