ggplot2 geom_bar填充美学不变

布伦特

我不明白为什么我不能更改此geom_bar图的填充美学

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

这是较大数据框(〜2000)的前10行,我要在其中自动计算和绘制比例,modality并按组的大小排序(将是较大图的侧面板)

绘图功能:

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

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档