r - 堆积条形图
问题描述
我有一张表格,其中包含有关患者生存/复发的分类变量。
数据如下所示(约 500 行):
肿瘤类型 | 差异化 | 阶段 | 倍性 | 复发 | 死亡 |
---|---|---|---|---|---|
子宫内膜样 | G2 | IA | 二倍体 | 无复发 | 没有死亡 |
浆液 | G1 | 国际文凭组织 | 非整倍体 | 复发 | 没有死亡 |
我此时卡在某个地方:
#Importing the libraries
# Packages
library(ggplot2)
library(gridExtra)
#Drawing the plots
p1 <- ggplot(df, aes(Relapse, group = Stage)) +
geom_bar(aes(y = ..prop.., fill = factor(..x..)), stat="count") +
scale_y_continuous(labels=scales::percent) +
ylab("Relative frequencies") +
facet_grid(~Stage) +
labs(title = "Stage", tag = "A") +
theme(plot.title = element_text(hjust = 0.5),
plot.tag = element_text())
p2 <- ggplot(df, aes(Relapse, group = Ploidy)) +
geom_bar(aes(y = ..prop.., fill = factor(..x..)), stat="count") +
scale_y_continuous(labels=scales::percent) +
ylab("Relative frequencies") +
facet_grid(~Ploidy) +
labs(title = "Ploidy", tag = "B") +
theme(plot.title = element_text(hjust = 0.5),
plot.tag = element_text())
p3 <- ggplot(df, aes(Relapse, group = Differentiation)) +
geom_bar(aes(y = ..prop.., fill = factor(..x..)), stat="count") +
scale_y_continuous(labels=scales::percent) +
ylab("Relative frequencies") +
facet_grid(~Differentiation) +
labs(title = "Differentiation", tag = "C") +
theme(plot.title = element_text(hjust = 0.5),
plot.tag = element_text())
p4 <- ggplot(df, aes(Relapse, group = Tumor_Type)) +
geom_bar(aes(y = ..prop.., fill = factor(..x..)), stat="count") +
scale_y_continuous(labels=scales::percent) +
ylab("Relative frequencies") +
facet_grid(~Tumor_Type) +
labs(title = "Tumor_Type", tag = "D") +
theme(plot.title = element_text(hjust = 0.5),
plot.tag = element_text())
#Arranging them into one
grid.arrange(p1, p2, p3, p4)
所以问题是:
- 如何为“复发”制作百分比堆积条形图,并为“死亡”制作并排分隔,使它们具有相似的高度?
- 如示例图像所示,如何绘制单个图例?
解决方案
推荐阅读
- python - 为什么即使在对我的时间序列进行差分之后,我也会得到“计算出的初始 AR 系数不是固定的”?
- google-cloud-platform - 将实例从美国移动到欧洲
- javascript - Reactjs - 通过按钮更改随机数组中的项目
- python-3.x - 如何将带有文件和参数的python3发布请求转换为curl
- android-studio - 如何在 android studio 中设置 2 个 android 模拟器来测试反应原生应用程序
- amazon-web-services - 对二级索引 (GSI) 进行 DynamoDB 扫描
- git - Git 已安装,但 .git 目录不存在,所以我无法配置它
- pandas - 想要删除 panda 中的列,但如果列已经存在,则不应显示错误
- mongodb - 我需要在 mongoDb 中附加聚合结果,其中包含不同列的平均值和总和
- django - Django 上传多个文件