在R中为多个绘图提供更多空间

在R中为多个绘图提供更多空间,r,ggplot2,R,Ggplot2,我正在尝试使用R软件包cowplot和ggplot2将6个绘图放入同一个绘图块中。但是我的大标题和两个情节不断被删掉,我不知道如何改变整个情节的边缘 我不一定需要使用cowplot包 以下是一些虚拟数据: library("cowplot") library("ggplot2") ##grab a segment of the mtcars dataframe and assign that segment to an object mtcars.one

我正在尝试使用R软件包
cowplot
ggplot2
将6个绘图放入同一个绘图块中。但是我的大标题和两个情节不断被删掉,我不知道如何改变整个情节的边缘

我不一定需要使用
cowplot

以下是一些虚拟数据:

library("cowplot")
library("ggplot2")

##grab a segment of the mtcars dataframe and assign that segment to an object
mtcars.one <- mtcars[1:21,]
mtcars.two <- mtcars[11:32,]
mtcars.three <- mtcars[5:26,]
mtcars.four <- mtcars[7:28,]
mtcars.five <- mtcars[4:25,]
mtcars.six <- mtcars[9:30,]

#using ggplot2 package, create a barplot where the coordinates are flipped, give the axes labels and overall plot a title, and assign plot to an object
one.f <- ggplot(data = mtcars.one) + 
  geom_col(mapping=aes(x= rownames(mtcars.one), y=disp)) + 
  coord_flip()+ labs(x="Cars", y="Loadings", title = "These Loadings") 

two.f <- ggplot(data = mtcars.two) + 
  geom_col(mapping=aes(x= rownames(mtcars.two), y=disp)) + 
  coord_flip()+ labs(x="Cars", y="Loadings", title = "These Loadings")

three.f <- ggplot(data = mtcars.three) + 
  geom_col(mapping=aes(x= rownames(mtcars.three), y=disp)) + 
  coord_flip()+ labs(x="Cars", y="Loadings", title = "These Loadings")

four.f <- ggplot(data = mtcars.four) + 
  geom_col(mapping=aes(x= rownames(mtcars.four), y=disp)) + 
  coord_flip()+ labs(x="Cars", y="Loadings", title = "These Loadings")

five.f <- ggplot(data = mtcars.five) + 
  geom_col(mapping=aes(x= rownames(mtcars.five), y=disp)) + 
  coord_flip()+ labs(x="Cars", y="Loadings", title = "These Loadings")

six.f <- ggplot(data = mtcars.six) + 
  geom_col(mapping=aes(x= rownames(mtcars.six), y=disp)) + 
  coord_flip()+ labs(x="Cars", y="Loadings", title = "These Loadings")
库(“cowplot”)
图书馆(“ggplot2”)
##获取mtcars数据帧的一段并将该段指定给对象

mtcars.one一个选项可以使用
拼凑
,并使用
缩放x_离散(guide=guide\u轴(n.dodge=2))
和字体大小:

library(ggplot2)
library(patchwork)
##grab a segment of the mtcars dataframe and assign that segment to an object
mtcars.one <- mtcars[1:21,]
mtcars.two <- mtcars[11:32,]
mtcars.three <- mtcars[5:26,]
mtcars.four <- mtcars[7:28,]
mtcars.five <- mtcars[4:25,]
mtcars.six <- mtcars[9:30,]

#using ggplot2 package, create a barplot where the coordinates are flipped, give the axes labels and overall plot a title, and assign plot to an object
one.f <- ggplot(data = mtcars.one) + 
  geom_col(mapping=aes(x= rownames(mtcars.one), y=disp)) + 
  coord_flip()+ labs(x="Cars", y="Loadings", title = "These Loadings") 

two.f <- ggplot(data = mtcars.two) + 
  geom_col(mapping=aes(x= rownames(mtcars.two), y=disp)) + 
  coord_flip()+ labs(x="Cars", y="Loadings", title = "These Loadings")

three.f <- ggplot(data = mtcars.three) + 
  geom_col(mapping=aes(x= rownames(mtcars.three), y=disp)) + 
  coord_flip()+ labs(x="Cars", y="Loadings", title = "These Loadings")

four.f <- ggplot(data = mtcars.four) + 
  geom_col(mapping=aes(x= rownames(mtcars.four), y=disp)) + 
  coord_flip()+ labs(x="Cars", y="Loadings", title = "These Loadings")

five.f <- ggplot(data = mtcars.five) + 
  geom_col(mapping=aes(x= rownames(mtcars.five), y=disp)) + 
  coord_flip()+ labs(x="Cars", y="Loadings", title = "These Loadings")

six.f <- ggplot(data = mtcars.six) + 
  geom_col(mapping=aes(x= rownames(mtcars.six), y=disp)) + 
  coord_flip()+ labs(x="Cars", y="Loadings", title = "These Loadings")

#Plot
Out <- wrap_plots(list(one.f,two.f,three.f,four.f,five.f,six.f),ncol = 2)+
  plot_annotation(tag_levels = c('A'))&theme(axis.text.y = element_text(size=7))&
  scale_x_discrete(guide = guide_axis(n.dodge=2))
库(ggplot2)
图书馆(拼凑)
##获取mtcars数据帧的一段并将该段指定给对象

mtcars.one一个选项可以使用
拼凑
,并使用
缩放x_离散(guide=guide\u轴(n.dodge=2))
和字体大小:

library(ggplot2)
library(patchwork)
##grab a segment of the mtcars dataframe and assign that segment to an object
mtcars.one <- mtcars[1:21,]
mtcars.two <- mtcars[11:32,]
mtcars.three <- mtcars[5:26,]
mtcars.four <- mtcars[7:28,]
mtcars.five <- mtcars[4:25,]
mtcars.six <- mtcars[9:30,]

#using ggplot2 package, create a barplot where the coordinates are flipped, give the axes labels and overall plot a title, and assign plot to an object
one.f <- ggplot(data = mtcars.one) + 
  geom_col(mapping=aes(x= rownames(mtcars.one), y=disp)) + 
  coord_flip()+ labs(x="Cars", y="Loadings", title = "These Loadings") 

two.f <- ggplot(data = mtcars.two) + 
  geom_col(mapping=aes(x= rownames(mtcars.two), y=disp)) + 
  coord_flip()+ labs(x="Cars", y="Loadings", title = "These Loadings")

three.f <- ggplot(data = mtcars.three) + 
  geom_col(mapping=aes(x= rownames(mtcars.three), y=disp)) + 
  coord_flip()+ labs(x="Cars", y="Loadings", title = "These Loadings")

four.f <- ggplot(data = mtcars.four) + 
  geom_col(mapping=aes(x= rownames(mtcars.four), y=disp)) + 
  coord_flip()+ labs(x="Cars", y="Loadings", title = "These Loadings")

five.f <- ggplot(data = mtcars.five) + 
  geom_col(mapping=aes(x= rownames(mtcars.five), y=disp)) + 
  coord_flip()+ labs(x="Cars", y="Loadings", title = "These Loadings")

six.f <- ggplot(data = mtcars.six) + 
  geom_col(mapping=aes(x= rownames(mtcars.six), y=disp)) + 
  coord_flip()+ labs(x="Cars", y="Loadings", title = "These Loadings")

#Plot
Out <- wrap_plots(list(one.f,two.f,three.f,four.f,five.f,six.f),ncol = 2)+
  plot_annotation(tag_levels = c('A'))&theme(axis.text.y = element_text(size=7))&
  scale_x_discrete(guide = guide_axis(n.dodge=2))
库(ggplot2)
图书馆(拼凑)
##获取mtcars数据帧的一段并将该段指定给对象

mtcars.one除非有人想出更好的答案,否则我可以使用
ggpubr
,它有一个名为
ggarrange
的函数,而不是使用包
cowplot
。这个功能允许我在多个页面上绘图,在实际使用中,这实际上不会扩展到其他页面。我只能够扩展绘图区域,而不会扭曲grand plot中的多个绘图

下面是我的代码,使用问题中的虚拟数据在同一页面上绘制所有6个图,而不失真:

library(ggpubr)
multi.page <- ggpubr::ggarrange(one.f, two.f, three.f, four.f, five.f, six.f, 
                                nrow = 3, ncol = 2) +
                      draw_plot_label(label=LETTERS[1:6], 
                           x=c(0, 0.5, 0, 0.5, 0, 0.5), 
                           y=c(1, 1, 0.667, 0.667, 0.335, 0.335))
ggexport(multi.page, filename = "This is my file name.pdf")
库(ggpubr)

multi.page除非有人想出更好的答案,否则我可以使用
ggpubr
,它有一个名为
ggarrange
的函数,而不是使用包
cowplot
。这个功能允许我在多个页面上绘图,在实际使用中,这实际上不会扩展到其他页面。我只能够扩展绘图区域,而不会扭曲grand plot中的多个绘图

下面是我的代码,使用问题中的虚拟数据在同一页面上绘制所有6个图,而不失真:

library(ggpubr)
multi.page <- ggpubr::ggarrange(one.f, two.f, three.f, four.f, five.f, six.f, 
                                nrow = 3, ncol = 2) +
                      draw_plot_label(label=LETTERS[1:6], 
                           x=c(0, 0.5, 0, 0.5, 0, 0.5), 
                           y=c(1, 1, 0.667, 0.667, 0.335, 0.335))
ggexport(multi.page, filename = "This is my file name.pdf")
库(ggpubr)

我把n.dodge改为1,所以我只会给汽车贴上一次标签。我不确定我是否理解scale_x_离散函数的功能?您的代码可以在同一个grand plot上获得所有6个plot。但是,6个图中的每一个都是水平拉伸的,在这个补丁包中,我可以为整个内容添加标题吗?@simpson Yeah您可以尝试在末尾添加
&ggtitle(“我的标题”)
知道如何使用您建议的补丁包显式选择6个图中每个图的大小吗?或者如何取消6个情节的延伸?@simpson我添加了一个更新。播放
plot\u布局(宽度=c(0.5,1))
可用于调整大小!我希望这对你有帮助!我把n.dodge改为1,所以我只会给汽车贴上一次标签。我不确定我是否理解scale_x_离散函数的功能?您的代码可以在同一个grand plot上获得所有6个plot。但是,6个图中的每一个都是水平拉伸的,在这个补丁包中,我可以为整个内容添加标题吗?@simpson Yeah您可以尝试在末尾添加
&ggtitle(“我的标题”)
知道如何使用您建议的补丁包显式选择6个图中每个图的大小吗?或者如何取消6个情节的延伸?@simpson我添加了一个更新。播放
plot\u布局(宽度=c(0.5,1))
可用于调整大小!我希望这对你有帮助!