Tobias P. Tobias P. - 8 months ago 60
R Question

How to remove margins in ggplot2 chart

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

    • 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:

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)])),
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"),


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 that could also be useful here to remove all space between the legend and the plot: = unit(0, "mm").