我正在对一些患有某种疾病的患者进行研究,并在 3 个不同时间点对功能状态进行有序量表评估。我想在这些时间点连接多个堆叠条形图中的组。
我查看了这些主题,并没有使用这些建议使其工作:
有没有一种有效的方法可以使用 ggplot2 在堆积条形图中的不同元素之间绘制线条?
请查看我最终希望该图如何从 R(在 PRISM 中生成)查看三个时间点上这 6 个序数值中每一个的频率的图形表示(顶部组没有序数得分为 3、5、6 的患者) ):
数据:
library(tidyverse)
mrs <-tibble(
Score = c(0,1,2,3,4,5,6),
pMRS = c(17, 2, 1, 0, 1, 0, 0),
dMRS = c(2, 3, 2, 6, 4, 2, 2),
fMRS = c(4, 4, 5, 4, 1, 1, 2)
这是我在使用geom_line
or遇到问题之前尝试过的代码geom_segment
(省略了这些行,因为它只是扭曲了当前的图形)
mrs <- mrs %>% mutate(across(-Score,~paste(round(prop.table(.) * 100, 2)))) %>%
pivot_longer(cols = c("pMRS", "dMRS", "fMRS"), names_to = "timepoint") %>%
mutate(Score=as.character(Score),
value=as.numeric(value)) %>%
mutate(timepoint = factor(timepoint,
levels= c("fMRS",
"dMRS",
"pMRS"))) %>%
mutate(Score = factor(Score,
levels = c("6","5","4","3","2","1","0")))
mrs %>% ggplot(aes(y= timepoint, x= value, fill= Score))+
geom_bar(color= "black", width = 0.6, stat= "identity") +
scale_fill_manual(name= NULL,
breaks = c("6","5","4","3","2","1","0"), values= c("#000000","#294e63", "#496a80","#7c98ac", "#b3c4d2","#d9e0e6","#ffffff"))+
scale_y_discrete(breaks=c("pMRS",
"dMRS",
"fMRS"),
labels=c("Pre-mRS, (N=21)",
"Discharge mRS, (N=21)",
"Followup mRS, (N=21)"))+
theme_classic()