ggplot axis padding: Can I remove this whitespace?

I'm trying to prepare a figure for print. The panel of the ggplot will have a dark background and the image itself will be set against a dark background, so the whitespace surrounding the figure needs to be managed. However, I can't seem to remove some visible whitespace on the left side of the plot which I assume is some type of axes padding as it does not appear on an empty ggplot (which doesn't have axes).

Here's what I'm referring to.

image image

And to show you that it seems to stem from the axes, here's a picture without the caption, to show that it's also at the bottom for the x-axis.

image

A Reprex follows:

library(tidyverse)

p <- ggplot(data = mtcars, aes(x = disp, y = mpg)) +
  geom_line(color = "red")

q <- p +
  labs(caption = "My Chart Thing") +
  theme_minimal() +
  theme(legend.position = "bottom", panel.background = element_rect(fill = "#44475a", color = "#44475a"), 
        panel.grid = element_blank(),
        text = element_text(color = "#8be9fd"), 
        axis.line=element_blank(),
        axis.text = element_blank(),
        axis.ticks=element_blank(),
        axis.title = element_blank(),
        panel.border=element_blank(), 
        panel.spacing = unit(0, "cm"),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        plot.margin = margin(0, 0, 0, 0, "cm"), 
        plot.caption = element_text(hjust=0.02, size=rel(2.2), color = "#44475a")) +
  guides(color = FALSE, linetype = FALSE, fill = FALSE)

q

image

z <- ggplot() +
  labs(caption = "My Chart Thing") +
  theme_minimal() +
  theme(legend.position = "bottom", panel.background = element_rect(fill = "#44475a", color = "#44475a"), 
        panel.grid = element_line(color = "#282a36"),
        text = element_text(color = "#8be9fd"), 
        axis.line=element_blank(),
        axis.text = element_blank(),
        axis.ticks=element_blank(),
        axis.title = element_blank(),
        panel.border=element_blank(), 
        panel.spacing = unit(0, "cm"),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        plot.margin = margin(0, 0, 0, 0, "cm"), 
        plot.caption = element_text(hjust=0.02, size=rel(2.2), color = "#44475a")) +
  guides(color = FALSE, linetype = FALSE, fill = FALSE)

z

image

Created on 2019-12-17 by the reprex package (v0.3.0)

Is there some theme specification I'm missing which handles this? Many thanks in advance.

plot.background gets rid of that white bar but also changes the color behind the caption.

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 3.5.3
p <- ggplot(data = mtcars, aes(x = disp, y = mpg)) +
  geom_line(color = "red")

q <- p +
  labs(caption = "My Chart Thing") +
  theme_minimal() +
  theme(legend.position = "bottom", panel.background = element_rect(fill = "#44475a", color = "#44475a"), 
        panel.grid = element_blank(),
        text = element_text(color = "#8be9fd"), 
        axis.line=element_blank(),
        axis.text = element_blank(),
        axis.ticks=element_blank(),
        axis.title = element_blank(),
        panel.border=element_blank(), 
        panel.spacing = unit(0, "cm"),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        plot.margin = margin(0, 0, 0, 0, "cm"), 
        plot.caption = element_text(hjust=0.02, size=rel(2.2), color = "#FFFFFF"),
        plot.background = element_rect(fill = "#44475a")) +
  guides(color = FALSE, linetype = FALSE, fill = FALSE)
q

Created on 2019-12-16 by the reprex package (v0.3.0.9000)

2 Likes

Thanks so much for your reply @FJCC. After some further noodling around, it turns out I can have the best of both worlds, removing the axes whitespace while maintaining a white background for the caption.

It turns out

axis.ticks = element_blank()

Is not enough. What is required is.

axis.ticks.length = unit(0, "pt"), #length of tick marks

And voila!
image

Reprex for future reference.

library(tidyverse)

p <- ggplot(data = mtcars, aes(x = disp, y = mpg)) +
  geom_line(color = "red")

q <- p +
  labs(caption = "My Chart Thing") +
  theme(legend.position = "bottom", panel.background = element_rect(fill = "#44475a", color = "#44475a"), 
        axis.text = element_blank(),
        axis.title = element_blank(),
        axis.ticks = element_blank(),
        axis.ticks.length = unit(0, "pt"), #length of tick marks
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        plot.margin = margin(0, 0, 0, 0, "pt"),
        plot.caption = element_text(hjust=0.02, size=rel(2.2), color = "#44475a")) +
  guides(color = FALSE, linetype = FALSE, fill = FALSE)

q

Created on 2019-12-17 by the reprex package (v0.3.0)

edit: I should add that the solution was found in the ggplot2 book

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.