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

℡╲_俬逩灬. 提交于 2019-12-22 08:05:58

问题


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')) 


回答1:


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.



来源:https://stackoverflow.com/questions/16369708/remove-white-space-i-e-margins-ggplot2-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!