我想要一个堆叠的柱状图的发病率按类型跨越6个年龄组,我想按降序 for each 年龄组的类型,即较大的类型到底部.我try 了以下方法,但它只按每个年龄段的最大疾病类别排序,而不按较小的疾病类别排序:

enter image description here

请让我知道这是否可行.

以下是数据:

structure(list(age = structure(c(7L, 7L, 8L, 8L, 9L, 9L, 10L, 
10L, 11L, 11L, 12L, 12L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 11L, 
11L, 12L, 12L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 11L, 11L, 12L, 
12L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 11L, 11L, 12L, 12L, 7L, 
7L, 8L, 8L, 9L, 9L, 10L, 10L, 11L, 11L, 12L, 12L, 7L, 7L, 8L, 
8L, 9L, 9L, 10L, 10L, 11L, 11L, 12L, 12L, 7L, 7L, 8L, 8L, 9L, 
9L, 10L, 10L, 11L, 11L, 12L, 12L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 
10L, 11L, 11L, 12L, 12L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 11L, 
11L, 12L, 12L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 11L, 11L, 12L, 
12L), levels = c("<5 years", "5-9 years", "10-14 years", "15-19 years", 
"20-24 years", "25-29 years", "30-34 years", "35-39 years", "40-44 years", 
"45-49 years", "50-54 years", "55-59 years", "60-64 years", "65-69 years", 
"70-74 years", "75-79 years", "80-84 years", "85+ years", "Age-standardized"
), class = "factor"), disease = c("infection", "infection", "infection", 
"infection", "infection", "infection", "infection", "infection", 
"infection", "infection", "infection", "infection", "diarrhoea", 
"diarrhoea", "diarrhoea", "diarrhoea", "diarrhoea", "diarrhoea", 
"diarrhoea", "diarrhoea", "diarrhoea", "diarrhoea", "diarrhoea", 
"diarrhoea", "stroke", "stroke", "stroke", "stroke", "stroke", 
"stroke", "stroke", "stroke", "stroke", "stroke", "stroke", "stroke", 
"stroke", "stroke", "stroke", "stroke", "stroke", "stroke", "stroke", 
"stroke", "stroke", "stroke", "stroke", "stroke", "infection", 
"infection", "infection", "infection", "infection", "infection", 
"infection", "infection", "infection", "infection", "infection", 
"infection", "CKD", "CKD", "CKD", "CKD", "CKD", "CKD", "CKD", 
"CKD", "CKD", "CKD", "CKD", "CKD", "HBP", "HBP", "HBP", "HBP", 
"HBP", "HBP", "HBP", "HBP", "HBP", "HBP", "HBP", "HBP", "HBP", 
"HBP", "HBP", "HBP", "HBP", "HBP", "HBP", "HBP", "HBP", "HBP", 
"HBP", "HBP", "CKD", "CKD", "CKD", "CKD", "CKD", "CKD", "CKD", 
"CKD", "CKD", "CKD", "CKD", "CKD", "diarrhoea", "diarrhoea", 
"diarrhoea", "diarrhoea", "diarrhoea", "diarrhoea", "diarrhoea", 
"diarrhoea", "diarrhoea", "diarrhoea", "diarrhoea", "diarrhoea"
), value = c(23353.7, 3.9, 23642.5, 4.4, 24538.1, 5, 28653.1, 
6, 37310.1, 8.5, 46492.1, 12.5, 62458.4, 10.4, 90531.6, 16.7, 
119950.1, 24.3, 154233.1, 32.6, 191329, 43.8, 225451.4, 60.8, 
121.5, 5, 214.7, 8.8, 404.1, 17.2, 733.9, 31.2, 1397.4, 56.5, 
2578.9, 95.2, 120830.8, 20.1, 188983.9, 34.9, 305603.5, 61.9, 
480027.2, 101.3, 759052.9, 173.8, 1048404.1, 282.6, 1.7, 0.1, 
2.2, 0.1, 3.3, 0.1, 5.6, 0.2, 10.6, 0.4, 23.9, 0.9, 14000.3, 
2.3, 20370.1, 3.8, 31359.7, 6.4, 53808, 11.4, 100193.7, 22.9, 
162747.6, 43.9, 31074, 5.2, 43067.6, 8, 66533.8, 13.5, 99766.8, 
21.1, 161156.5, 36.9, 220591.3, 59.5, 32.7, 1.4, 50, 2, 76.5, 
3.3, 126.4, 5.4, 241.4, 9.8, 458.1, 16.9, 12.9, 0.5, 19.3, 0.8, 
30.9, 1.3, 69.5, 3, 173.1, 7, 420.3, 15.5, 51.7, 2.1, 90.1, 3.7, 
159.2, 6.8, 281.9, 12, 507.1, 20.5, 859.5, 31.7)), row.names = c(NA, 
-120L), class = "data.frame")

这就是我try 的:

library(ggplot2)
 
df %>%
     group_by(age) %>%
    arrange(desc(value)) %>%
     ggplot(aes(x = age, y = value, fill = disease)) +
    geom_bar(stat = "identity") + 
    theme(axis.text.x = element_text(angle = 90) )

推荐答案

enter image description here

这是一个非常不像R的解决方案,但我找不到任何其他方法来解决这个看似非常简单的问题

df_sum |>
ggplot(aes(x = age))+
  geom_col(data = df_sum |> filter(str_detect(age,"30-34")),
           aes(y = value, fill = fct_reorder(disease,value)), position = "stack")+
  geom_col(data = df_sum |> filter(str_detect(age,"35-39")),
           aes(y = value, fill = fct_reorder(disease,value)), position = "stack")+
  geom_col(data = df_sum |> filter(str_detect(age,"40-44")),
           aes(y = value, fill = fct_reorder(disease,value)), position = "stack")+
  geom_col(data = df_sum |> filter(str_detect(age,"45-49")),
           aes(y = value, fill = fct_reorder(disease,value)), position = "stack")+
  geom_col(data = df_sum |> filter(str_detect(age,"50-54")),
           aes(y = value, fill = fct_reorder(disease,value)), position = "stack")+
  geom_col(data = df_sum |> filter(str_detect(age,"55-59")),
           aes(y = value, fill = fct_reorder(disease,value)), position = "stack")+
  labs(x = "Age groups", y = "Value", fill = "Disease",
       title = "Disease spread over age groups")+
  scale_y_continuous(labels = scales::comma)+
  theme_classic(base_size = 16)+
  theme(legend.position = "top")

R相关问答推荐

如何删除多个.CSV文件的行

如何使用R Shiny中的条件面板仅隐藏和显示用户输入,同时仍允许运行基础计算?

隐藏e_mark_line的工具提示

如何在R中添加截止点到ROC曲线图?

如何编辑ggplot的图例字使用自定义对象(gtable)?'

用值序列对行进行子集化,并标识序列开始的列

使用geom_segment()对y轴排序

如何在观测缺失的地方添加零

将小数分隔符放在R中的前两位数字之后

在R中使用Scale_y_Break后更改y轴标签

多个模拟序列间的一种预测回归关系

R Select()可以测试不存在的子集列

使用Scale_*_MANUAL时在图例中保留未使用的系数级别

防止在更新SHINY中的Reactive Value的部分内容时触发依赖事件

`-`是否也用于数据帧,有时使用引用调用?

如何在AER::ivreg中指定仪器?

TidyVerse中长度不等的列结合向量

使用R、拼图和可能的网格包绘制两个地块的公共垂直线

如何用不同长度的向量填充列表?

如何编辑被动式数据表?