首页 > 解决方案 > 如何从ggplot中的饼图中删除白色边距

问题描述

我正在尝试更改饼图的背景颜色,以使该图更适合我的蒸馏网站。不幸的是,颜色变化在左侧和右侧留下了白色“边框”。
有谁知道我如何删除这些边框或更改它们的颜色?感谢所有帮助!

这就是我(未完成的)情节现在的样子。

我的代码如下所示:

bechdel2 %>%
  filter(country == "New Zealand") %>% 
  count(criteria) -> data_kreis

ggplot(data_kreis, aes(x="", y=desc(n), fill=criteria)) +
      geom_bar(stat="identity", width=1) +
      coord_polar("y", start=0) +
      theme_minimal()+
      geom_text(aes(label = paste0(n)), position = position_stack(vjust = 0.5)) +
      scale_y_continuous(breaks = 0:14) +
      labs(x = NULL, y = NULL, fill = NULL,
           caption = "Ergebnisse des Bechdel-Tests: Anzahl der Filme") +
  theme(
        axis.title = element_blank(), 
        axis.ticks = element_blank(), 
        axis.text = element_blank(),
        panel.background = element_rect(fill = "#FFF5DE",  color = "transparent"),
        panel.border=element_blank(),
        plot.background = element_rect(fill = "#FFF5DE",  color = "transparent"), 
        panel.grid = element_blank(),
        legend.background = element_rect(fill = "white", color = "black"),
        plot.caption = element_text(size = 12, hjust = 0.5),
        strip.background = element_rect(fill = "#FFF5DE",  color = "transparent"))

这是我从 dput(head(data_kreis)) 的输出:

structure(list(country = c("New Zealand", "New Zealand", "New Zealand", 
"New Zealand"), criteria = structure(1:4, .Label = c("Es gibt weniger als 2 Frauen", 
"Frauen reden nicht miteinander", "Frauen reden nur über Männer", 
"Film besteht alle Kriterien", "Bewertung ist unsicher"), class = c("ordered", 
"factor")), n = c(4L, 6L, 2L, 2L)), row.names = c(NA, -4L), groups = structure(list(
    country = "New Zealand", .rows = structure(list(1:4), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, -1L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

标签: rggplot2

解决方案


我发现当我将绘图保存为具有透明背景的 png 并将其作为图片实现到我的蒸馏网站时,白色“边框”不会显示。所以我的解决方案是将代码更改如下:

p <- ggplot(data_kreis, aes(x="", y=desc(n), fill=criteria)) +
      geom_bar(stat="identity", width=1) +
      coord_polar("y", start=0) +
      theme_minimal()+
  scale_fill_manual(values = c("Es gibt weniger als 2 Frauen" = "#3C5186",
                               "Frauen reden nicht miteinander" = "#5371bd",
                               "Frauen reden nur über Männer" = "#6488e8",
                               "Film besteht alle Kriterien" = "#9B72AA",
                               "Bewertung ist unsicher" = "#C6B4CE"
                               )) +
      geom_text(aes(label = paste0(n), fontface = "bold"), position = position_stack(vjust = 0.5), color = "white") +
      scale_y_continuous(breaks = 0:14) +
      labs(x = NULL, y = NULL, fill = NULL,
           caption = "Ergebnisse des Bechdel-Tests: Anzahl der Filme") +
  theme(
        axis.title = element_blank(), 
        axis.ticks = element_blank(), 
        axis.text = element_blank(),
        panel.background = element_rect(fill = "#FFF5DE",  color = "transparent"),
        panel.border=element_blank(),
        plot.background = element_rect(fill = "#FFF5DE",  color = "transparent"), 
        panel.grid = element_blank(),
        legend.background = element_rect(fill = "white", color = "black"),
        plot.caption = element_text(size = 12, hjust = 0.5),
        strip.background = element_rect(fill = "#FFF5DE",  color = "transparent"),
        text=element_text(family = "sans"))

ggsave(p, filename = "images/Kreis_New_Zealand.png", bg = "transparent")

推荐阅读