简体   繁体   中英

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)

在此处输入图片说明

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)

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)

在此处输入图片说明

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM