我正在尝试设计一个条形图来表示两个队列(GLP 和无 GLP)之间的并发症百分比。我正在使用下面的数据表和代码,并且我已经包含了我当前的结果以及我试图获得的图表的描述。
如有任何帮助,我们将不胜感激。下面的数据表保存为 GLPcomprate。
队列 | 并发症 | 百分比 |
---|---|---|
GLP | CSF_leak | 13.40 |
GLP | 深静脉血栓 | 5.74 |
GLP | 尿路感染 | 20.57 |
GLP | 便秘 | 22.97 |
GLP | 肺炎 | 9.09 |
GLP | 术后感染 | 6.22 |
GLP | 药物不良反应 | 4.79 |
GLP | 脑膜炎 | 4.79 |
GLP | 术后呼吸衰竭 | 4.79 |
GLP | 甲状腺毒症 | 4.79 |
GLP | 急性肾损伤 | 11.96 |
GLP | 谵妄 | 4.79 |
无良好实验室规范 | CSF_leak | 11.32 |
无良好实验室规范 | 深静脉血栓 | 5.26 |
无良好实验室规范 | 尿路感染 | 15.74 |
无良好实验室规范 | 便秘 | 24.73 |
无良好实验室规范 | 肺炎 | 9.81 |
无良好实验室规范 | 术后感染 | 5.26 |
无良好实验室规范 | 药物不良反应 | 0.65 |
无良好实验室规范 | 脑膜炎 | 7.12 |
无良好实验室规范 | 术后呼吸衰竭 | 1.71 |
无良好实验室规范 | 甲状腺毒症 | 1.00 |
无良好实验室规范 | 急性肾损伤 | 10.78 |
无良好实验室规范 | 谵妄 | 2.46 |
当前代码如下:
GLPcomprate <- read_excel("IIH_GLP_Years.xlsx", sheet = 6)
head(GLPcomprate)
ggplot(GLPcomprate, aes(x=Complication, y=Percentage, fill = Cohort)) +
geom_bar(stat = "identity", position = position_stack(), width = 1) +
geom_text(aes(label = paste(Percentage, "%")), vjust = -1) +
facet_wrap(~Cohort, strip.position = "bottom", scales = "free_x") +
theme(panel.spacing = unit(0, "lines"),
strip.background = element_blank(),
strip.placement = "outside ")
电流输出: 在此输入图片描述
所需输出(在 Excel 上创建): 在此输入图片描述
不需要
facet_wrap()
,只需使用position_dodge()
。默认值的结果很混乱,所以我添加了一些调整。必要时进行调整。另外,您的示例数据中存在拼写错误,请记住 R 区分大小写。
library(ggplot2)
# Data
GLPcomprate <- read.table(text = "Cohort,Complication,Percentage
GLP,CSF_leak,13.40
GLP,DVT,5.74
GLP,UTI,20.57
GLP,Constipation,22.97
GLP,Pneumonia,9.09
GLP,Post-operative Infection,6.22
GLP,Adverse Drug Effects,4.79
GLP,Meningitis,4.79
GLP,Post-operative Respiratory Failure,4.79
GLP,Thyrotoxicosis,4.79
GLP,Acute Kidney Injury,11.96
GLP,Delirium,4.79
No GLP,CSF_leak,11.32
No GLP,DVT,5.26
No GLP,UTI,15.74
No GLP,Constipation,24.73
No GLP,Pneumonia,9.81
No GLP,Post-operative Infection,5.26
No GLP,Adverse Drug Effects,0.65
No GLP,Meningitis,7.12
No GLP,Post-operative Respiratory Failure,1.71
No GLP,Thyrotoxicosis,1.00
No GLP,Acute Kidney Injury,10.78
No GLP,Delirium,2.46", header = TRUE, sep = ",")
ggplot(GLPcomprate,
aes(x = Complication, y = Percentage, fill = Cohort)) +
geom_bar(stat = "identity", position = position_dodge(), width = 1) +
geom_text(aes(label = paste0(Percentage, "%")),
size = 2,
hjust = 0.5,
position = position_dodge(width = .9)) +
coord_cartesian(clip = "off") +
theme(panel.spacing = unit(0, "lines"),
strip.background = element_blank(),
strip.placement = "outside ",
axis.text.x = element_text(angle = 45, hjust = 1, size = 6))