1

I am currently creating plots with ggplot2 for a latex document and discovered that ggplot2 adds many unwanted margins:

enter image description here

  • painted red by plot.background=element_rect(fill="red"):
    • small margin on the left
    • small margin between image and legend
  • painted violet with photoshop:
    • margin on the left and the right
    • 1px margin on the bottom

Which more rules are needed to remove these margins? It's really difficult to google all these configuration options. This is my actual chart:

library(ggplot2)
library(scales)
label <- c("A", "B", "C", "D")
value <- c(61, 26, 9, 4)
values <- data.frame(label, value)
myplot <- ggplot(values, aes(x = "", y=value, fill=label))
myplot <- myplot + theme(legend.position="bottom")
myplot <- myplot + labs(fill="")
myplot <- myplot + geom_bar(stat="identity", width=1)
myplot <- myplot + geom_text(
  aes(x=1.3, y=value/2+c(0, cumsum(value)[-length(value)])),
  label=percent(value/100),
  size=2
)
myplot <- myplot + coord_polar(theta="y")
myplot <- myplot + theme(plot.background=element_rect(fill="red"))
myplot <- myplot + theme(
  plot.margin=unit(c(0,0,0,0), "mm"),
  legend.margin=unit(0, "mm"),
  axis.title=element_blank(),
  axis.ticks=element_blank()
)
ggsave("pie.pdf")
Tobias P.
  • 4,537
  • 2
  • 29
  • 36
  • Dupe? [Remove plot margins in `ggplot2`](http://stackoverflow.com/a/17791455/903061) says `labs(x=NULL, y=NULL)` is also needed. – Gregor Thomas Nov 03 '16 at 17:16
  • However it doesn't really seem to help, at least not with the red colored area. – Gregor Thomas Nov 03 '16 at 17:22
  • why dont you just remove ``myplot <- myplot + theme(plot.background=element_rect(fill="red"))`` ? – Cyrus Mohammadian Nov 03 '16 at 17:38
  • Seems like you need to set `axis.text`, `axis.ticks.length`, and possibly `legend.box.spacing`. That last one might be new in the development version of ggplot2. – aosmith Nov 03 '16 at 17:59
  • @aosmith `axis.text` and `axis.ticks.length` have been the correct settings. Can you post this again as an answer that i can mark you answer the correct one? – Tobias P. Nov 04 '16 at 08:10

2 Answers2

3

Adjust the plot.margin settings so that the bottom and left side are negative numbers.

plot.margin=unit(c(0,0,-12,-5), "mm")

If you do get rid of the margin on the bottom, you are also sacrificing the legend.

gleasocd
  • 93
  • 7
  • The problem with this hardcoded values is that you have to recalculate them for every new chart type you make. If you know the correct margin and length options they will fit for every future possible chart you make. – Tobias P. Nov 04 '16 at 08:09
1

You can remove the rest of the axis space via the theme elements axis.text and axis.tick.length.

So you'd add something like the following to your theme code:

axis.text = element_blank(), axis.ticks.length = unit(0, "mm")

In the current development version of ggplot2, ggplot2_2.1.0.9001, there is a new theme element legend.box.spacing that could also be useful here to remove all space between the legend and the plot: legend.box.spacing = unit(0, "mm").

aosmith
  • 34,856
  • 9
  • 84
  • 118