gpt4 book ai didi

r - GGP图: Different chart types in facets?

转载 作者:行者123 更新时间:2023-12-02 21:11:05 25 4
gpt4 key购买 nike

我正在尝试找到如何堆叠 2 个图表(如构面)的解决方案。第一个图表显示数据计数,第二个图表显示百分比。

这篇文章给了我启发: Stacke different plots in a facet manner

目前,我创建了两个独立的图表:

set.seed(1)
trial.id <- sample(1:6, 1000, replace = TRUE, c(".35", ".25", ".2", ".15", ".04", ".01"))
surv <- sample(0:1, 1000, replace = TRUE)

trial.df <- data.frame(cbind(trial.id, surv))

# chart with counts information
windows(height = 800, width = 1500)
plotEX1 <- ggplot(trial.df, aes(x = as.factor(trial.id) , fill = as.factor(surv))) +
geom_bar(stat = "bin") +
ggtitle("Surival by Trial\n") +
labs(x = "\nTrial", y = "Patients (#)\n") +
scale_fill_discrete(name = "Survival")
plotEX1

# chart with % information
windows(height = 800, width = 1500)
plotEX2 <- ggplot(trial.df, aes(x = as.factor(trial.id) , fill = as.factor(surv))) +
geom_bar(stat = "bin", position = "fill") +
ggtitle("Surival by Trial\n") +
labs(x = "\nTrial", y = "Patients (%)\n") +
scale_fill_discrete(name = "Survival")
plotEX2

Trial.id 代表运行的试验次数。surv代表试验成功的次数。

plotEX1 显示运行试验次数以及成功率。plotEX2 是相同的东西,但显示为百分比。

我们的目标是同时查看这两个图表,以便读者可以轻松了解试验计数以及成功百分比。

我认为让我失望的是试图融化数据。我尝试了几件事,但似乎无法让它发挥作用。任何帮助我指明正确方向的帮助将不胜感激!

谢谢!

最佳答案

根据我们的评论,我可能会做这样的事情:

new_dat <- melt(list(trial.df,trial.df),id.vars = 1:2)
new_dat$trial.id <- factor(new_dat$trial.id)
new_dat$surv <- factor(new_dat$surv)
ggplot() +
facet_wrap(~L1,nrow = 2,scales = "free_y") +
geom_bar(data = subset(new_dat,L1 == 1),
aes(x = trial.id , fill = surv),
stat = "bin",
position = "fill") +
geom_bar(data = subset(new_dat,L1 == 2),
aes(x = trial.id , fill = surv),
stat = "bin") +
ggtitle("Surival by Trial\n") +
labs(x = "\nTrial", y = "Patients (%)\n") +
scale_fill_discrete(name = "Survival")

enter image description here

(并忽略警告。)

关于r - GGP图: Different chart types in facets?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18089956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com