r - 如何从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"))
解决方案
我发现当我将绘图保存为具有透明背景的 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")
推荐阅读
- c++ - 每次打开组合框时如何创建项目列表?
- c++ - 我如何找到长度?我有 8 个管子和 11 个节点
- macos - 解决警告 NETSDK1138:目标框架“netcoreapp3.0”不受支持
- node.js - “node_modules\uuidv4\build\lib\uuidv4.js”的包试图导入Node标准库模块“util”
- python - 循环字典列表并比较其他列表中的任何匹配值
- asp.net-mvc - 在 Asp.net Mvc 5 中使用 Active Directory 登录?
- vim - 维姆。无法将非英语字符拉到系统剪贴板
- regex - 正则表达式提取包含 PIPE (|) 的子字符串
- python - check if key is present in the row as a substring
- scala - Spark union taking a long time with 1 long-running task