giac
giac

Reputation: 4299

ggplot2 polar_coord remove background border grid

I can't find a way to remove the black border of a coord_polar figure using ggplot2. Based on this post, I tried this but it doesn't work.

Any suggestions how I could remove the grid and have a uniform colour background?

library(ggplot2)
a <- seq(1,20)
b <- a^0.25
df <- as.data.frame(cbind(a,b))

gg1 = ggplot(df, aes(x = a, y = b, colour = a)) + 
  coord_polar(start = 1) + 
  geom_bar(stat = 'identity') +
  theme_tufte() + 
  theme(plot.background=element_rect(fill="floralwhite"), panel.border = 
element_blank()) +
  theme(legend.position="bottom") 

grid:::grid.rect(gp=gpar(fill="floralwhite", col="floralwhite"))
ggplot2:::print.ggplot(gg1, newpage=FALSE)

enter image description here

Upvotes: 1

Views: 502

Answers (2)

Claus Wilke
Claus Wilke

Reputation: 17790

The black line is drawn by the plot.background element. You need to set its color to NA to remove the line:

gg1 <- ggplot(df, aes(x = a, y = b, colour = a)) + 
  coord_polar(start = 1) + 
  geom_bar(stat = 'identity') +
  theme_tufte() + 
  theme(plot.background = element_rect(fill="floralwhite", color = NA),
        legend.position = "bottom") 

grid:::grid.rect(gp = gpar(fill="floralwhite", col="floralwhite"))
ggplot2:::print.ggplot(gg1, newpage=FALSE)

enter image description here

Note that if you don't want to fix the background drawing using non-exported ggplot2 functions (such as print.ggplot()), you can achieve the same effect using ggdraw() from cowplot:

cowplot::ggdraw(gg1) +
  theme(plot.background = element_rect(fill="floralwhite", color = NA))

(Result is exactly the same as before.)

Upvotes: 2

Thor
Thor

Reputation: 48

Install the cowplot package and use the panel_border() theme and set remove = true. This should hopefully work!

library(ggplot2)
library(cowplot)
a <- seq(1,20)
b <- a^0.25
df <- as.data.frame(cbind(a,b))

gg1 = ggplot(df, aes(x = a, y = b, colour = a)) + 
  coord_polar(start = 1) + 
  geom_bar(stat = 'identity') +
  theme_tufte() + 
  theme(plot.background=element_rect(fill="floralwhite") +
  theme(legend.position="bottom")+
  panel_border(remove = TRUE))

grid:::grid.rect(gp=gpar(fill="floralwhite", col="floralwhite"))
ggplot2:::print.ggplot(gg1, newpage=FALSE)

Upvotes: 2

Related Questions