我试图用y轴上的百分比来构造一个条形图。我有一些表格格式的数据,想把它绘制成条形图。
library(survival)
kidney$mortality <- ifelse(kidney$status==1, "Dead", "Alive")
table1(~ disease
| mortality * sex, data=kidney )我正试图评估每种疾病类别中死亡的人的概率,以及这样的数字类别。
在这种情况下,总共将有8个酒吧。
我如何把这个变成y轴上百分比的实心柱状图?
发布于 2021-09-07 17:21:05
库
library(tidyverse)
library(survival)代码
kidney %>%
#Change status from 0/1 to Dead/Alive
mutate(status = if_else(status==1, "Dead", "Alive")) %>%
#Count number of observations for each combination of sex, status and disease
count(disease,status,sex) %>%
#Grouping by next calculation by disease and sex
group_by(disease,sex) %>%
mutate(
#Total observations for each disease and sex
N = sum(n),
#Percentage of status by disease and sex
p = 100*n/N
) %>%
#Filter only the dead
filter(status == "Dead") %>%
ggplot(aes(x = disease, y = p, fill = as.factor(sex)))+
# Adding column geometry
geom_col(position = position_dodge())+
# Adding text in the top of the columns
geom_text(aes(label = round(p)),position = position_dodge(1), vjust = 2)输出

https://stackoverflow.com/questions/69092242
复制相似问题