Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 从ggplot中删除y标签_R_Ggplot2 - Fatal编程技术网

R 从ggplot中删除y标签

R 从ggplot中删除y标签,r,ggplot2,R,Ggplot2,我想结合3个ggplot直方图。为此,我使用gridExtra包。因为所有的绘图都在一行中,所以我想从右边开始的两个绘图中删除y标题和比例 我像往常一样写了同样的代码,但不起作用。你们知道有什么问题吗?我的代码: plot1 <- ggplot(testing, aes(x=residualtotal))+ geom_histogram(aes(y = ..density..), binwidth = 100) + geom_density(aes(y = ..density..

我想结合3个ggplot直方图。为此,我使用gridExtra包。因为所有的绘图都在一行中,所以我想从右边开始的两个绘图中删除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)

我认为您正在寻找的是
+ylab(NULL)
,并将
theme()
移动到
theme\u minimal()
之后。我还将宽度规范添加到了
grid.arrange
,因为最左边的图形的宽度需要更宽,以便为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_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]")+
  ylab(NULL) +
  theme_minimal() +
  theme(axis.text.y = element_blank())
plot3 <- ggplot(testing, aes(x=residualtotal2))+
  geom_histogram(aes(y = ..density..), binwidth = 100) + 
  geom_density(aes(y = ..density..*(2)))+
  xlab("Residuals Model 3 [MW]")+
  ylab(NULL) +
  theme_minimal() +
  theme(axis.text.y = element_blank())
grid.arrange(plot1, plot2, plot3, ncol = 3, nrow=1, widths = c(1.35, 1, 1))
plot1另一种方法:

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"))
库(tidyverse)
res_trans%
聚集(哪个剩余,值)%>%
变异(label=res_trans[which_resid])%>%
ggplot(aes(x=值,组=标签))+
几何图形柱状图(aes(y=…密度…),binwidth=100)+
几何密度(aes(y=…密度…*(2)))+
面_包裹(~标签,ncol=3)+
实验室(x=NULL,y=NULL)+
主题_极小值()+
主题(panel.background=element_rect(fill=“white”))

theme
语句放在
theme\u minimal()
之后,否则,
theme\u minimal
将覆盖它。此外,您可以将所有三个绘图中使用的任何绘图元素放入列表对象中,这样您就不必每次重复它们。例如:
my_plot=list(geom_density(aes(y=…density..*(2))),theme_minimal(),theme(axis.text.y=element_blank(),axis.title.y=element_blank(),axis.ticks.y=element_blank(),panel.background=element_rect(fill=“white”))
。然后在您的
ggplot
通话中加入
+my_plot
。@eipi10谢谢,效果很好:)
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"))