Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing negative plot area in ggplot2 [duplicate]

Tags:

plot

r

ggplot2

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

like image 337
Elizabeth Avatar asked Aug 15 '13 11:08

Elizabeth


1 Answers

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

like image 143
Simon O'Hanlon Avatar answered Nov 11 '22 19:11

Simon O'Hanlon