gpt4 book ai didi

从 ggplot 中删除 y 标签

转载 作者:行者123 更新时间:2023-12-04 16:08:38 27 4
gpt4 key购买 nike

我想组合 3 个 ggplot 直方图。为此,我使用了 gridExtra 包。因为所有地 block 都在一行中,所以我想从右侧数的 2 个地 block 中删除 y 标题和比例。

我编写了与往常一样的代码,但它不起作用。你们知道可能是什么问题吗?我的代码:

plot1 <- ggplot(testing, aes(x=residualtotal))+
geom_histogram(aes(y = ..density..), binwidth = 100) +
geom_density(aes(y = ..density..*(2)))+
xlab("Residuals Model 1 [MW]")+
theme(panel.background=element_rect(fill = "white") )+
theme_minimal()
plot2 <- ggplot(testing, aes(x=residualtotal1))+
geom_histogram(aes(y = ..density..), binwidth = 100) +
geom_density(aes(y = ..density..*(2)))+
xlab("Residuals Model 2 [MW]")+
theme(axis.text.y = element_blank(), axis.title.y = element_blank(), axis.ticks.y = element_blank(), panel.background=element_rect(fill = "white") )+
theme_minimal()
plot3 <- ggplot(testing, aes(x=residualtotal2))+
geom_histogram(aes(y = ..density..), binwidth = 100) +
geom_density(aes(y = ..density..*(2)))+
xlab("Residuals Model 3 [MW]")+
theme(axis.text.y = element_blank(), axis.title.y = element_blank(), axis.ticks.y = element_blank(), panel.background=element_rect(fill = "white") )+
theme_minimal()
grid.arrange(plot1, plot2, plot3, ncol = 3, nrow=1)

我的数据集示例。

    Load residualtotal1 prognosis2 residualtotal2 residualtotal
89 20524 -347.6772 20888.75 -364.7539 -287.82698
99 13780 -133.8496 13889.52 -109.5207 -6.60009
100 13598 -155.9950 13728.77 -130.7729 -27.18835
103 13984 -348.4080 14310.12 -326.1226 -213.68816
129 14237 -3141.5591 17375.82 -3138.8188 -3077.32236
130 14883 -3142.0134 18026.02 -3143.0183 -3090.52193

最佳答案

另一种方法:

library(tidyverse)

res_trans <- c(`residualtotal`="Residuals Model 1 [MW]",
`residualtotal1`="Residuals Model 2 [MW]",
`residualtotal2`="Residuals Model 3 [MW]")

select(testing, starts_with("resid")) %>%
gather(which_resid, value) %>%
mutate(label=res_trans[which_resid]) %>%
ggplot(aes(x=value, group=label)) +
geom_histogram(aes(y = ..density..), binwidth = 100) +
geom_density(aes(y = ..density..*(2))) +
facet_wrap(~label, ncol=3) +
labs(x=NULL, y=NULL) +
theme_minimal() +
theme(panel.background=element_rect(fill = "white"))

enter image description here

关于从 ggplot 中删除 y 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41048767/

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