[英]ggplot2 geom_bar fill aesthetic not changing
我不明白为什么我不能改变这个 geom_bar plot 的填充美学
# Reprex dataset
df <- structure(list(modality = c("Biological therapy", "Biological therapy",
"Biological therapy", "Hormone treatment", "Chemotherapy", "Hormone treatment",
"Biological therapy", "Biological therapy", "Hormone treatment",
"Chemotherapy"), impact = c("Interrupted", "As planned", "Interrupted",
"As planned", "As planned", "As planned", "Interrupted", "Interrupted",
"As planned", "Interrupted")), row.names = c(NA, -10L), class = c("tbl_df",
"tbl", "data.frame"))
这是较大的 dataframe (~2000) 的前 10 行,我想在其中自动计数 & plot modality
的比例和按组大小排序(将是更大图的侧面板)
Plot function:
library(tidyverse)
bar <- df %>%
ggplot(aes(x = fct_rev(fct_infreq(modality)),
y = ..prop..,
group = 1), stat = 'count') +
geom_bar(aes(fill = modality)) +
theme_classic() +
labs(x = NULL,
y = '% Total') +
scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
scale_fill_viridis_d() +
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank()) +
coord_flip()
bar
不显示viridis
调色板(或我传递给fill
的任何内容)。 我错过了什么?
问题是您将“组”美学映射到 1。
你可以试试这个:
bar <- df %>%
ggplot(aes(y = modality,
x = ..count../sum(..count..))) +
geom_bar(aes(fill = modality)) +
theme_classic() +
scale_fill_viridis_d() +
scale_x_continuous(name = "% Total", labels = scales::percent_format(accuracy = 1)) +
scale_y_discrete(name = "", labels = NULL) +
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank())
bar
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.