How to change the plot.background in a 'geom_rect()' + 'coord.polar()' in a Donut ggplot graph?
I dont know what I´m missing, but I´w working in html black background style and needing to set panel as well plot background to black, but the graph sides of my ggplot are white and I need to know which attribute or command I need to use to turn sides to black too.
Below my code:
my_df %>%
ggplot(aes(ymax=max, ymin=min, xmax=4, xmin=3,fill=ResultCode)) +
geom_rect() +
geom_label( x=3.5, aes(y=labelPosition, label=label), size=4, color="white") +
coord_polar(theta="y") +
xlim(c(2, 4)) +
theme_void() +
theme(legend.position="none",
plot.background=element_rect(fill = "black"),
panel.background = element_rect(fill = "black"),
panel.border = element_blank(),
legend.key = element_blank(),
axis.ticks = element_blank(),
axis.text.y = element_blank(),
panel.grid = element_blank())
Below the resulted graph (see the "white" sides at right and left I need to fill with black)
The problem here is that
ggplot
by default callsgrid::grid.newpage
before drawing. This creates a blank (white) screen. It will then set up a square viewport to fit your plotting window because you are usingcoord_polar
. Once it has done this, it considers the square area to be "the" plotting window. In a sense then, ggplot has no knowledge or control over these white areas. Notheme
element can touch it.The solution is to explicitly call
grid.newpage
yourself, draw a black background manually, and then explicitlyprint
your ggplot using the parameternewpage = FALSE
. You could alternatively set the gridgpar
parameters so that the background is black by default, but this is likely to have undesired side effects later on.Here's a reprex with some made-up data: