繁体   English   中英

如何从ggplot中的饼图中删除白边距

[英]How to remove white margins from pie chart in ggplot

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

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

我的代码如下所示:

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)) 的 output:

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

我发现当我将 plot 保存为具有透明背景的 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")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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