Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove white space (i.e., margins) ggplot2 in R

I'm trying to plot a pie chart using GGPLOT2 in R. I want to do this in such a way as to omit the extra margin space.

What I'm doing is similar to what sharoz did in this post here except I want to include a legend.

Here is what I'm doing:

ggplot(DATA, aes(x=factor(0),fill=factor(LABELS),weight=VALUES)) +
   geom_bar(width=1) +
   coord_polar(theta='y') +
   guides(fill=guide_legend(title='LEGEND')) 

enter image description here

like image 858
Chernoff Avatar asked May 04 '13 01:05

Chernoff


1 Answers

Assuming you are talking about the extra white space above and below the figure, the easiest solution is just to tweak the size of the graphics device. Here is aspect ratio is the key. If the aspect ratio of the graphics device matches that of the plot, you get rid of a lot of the whitespace.

What I use to save the plot is ggsave, in code:

ggplot(DATA, aes(x=factor(0),fill=factor(LABELS),weight=VALUES)) +
   geom_bar(width=1) +
   coord_polar(theta='y') +
   guides(fill=guide_legend(title='LEGEND')) 
ggsave("plot.png", width = 10, height = 5)

Just play around with width and height in ggsave until you are happy with the result.

like image 100
Paul Hiemstra Avatar answered Oct 30 '22 18:10

Paul Hiemstra