[在此处输入图片描述] [1]我是ggplot2中的程序R的初学者,我是这个网站的新手。我[尝试] [2]混合了我的地块,但我做不到。在数据集中,有一个捕鱼拖网数据。我在1994年和2016年完成了这两个图,现在我想像函数par(mfcol = c())一样放一个。 谢谢
ggplot(a1994, aes(x=BOTTOM_TEMPERATURE_BEGINNING, y=SHOOTING_DEPTH, colour=1)) +
geom_errorbar(aes(ymin=SHOOTING_DEPTH-se, ymax=SHOOTING_DEPTH+se), width=.0) +
geom_line() +
geom_point()+
xlab("Temperature") +
ylab("Depth")+
ggtitle("Plot relation T° and Depth year 1994")+
theme(plot.title = element_text(hjust = 0.5))+
theme(plot.title = element_text(colour = "black"))+
theme(plot.title = element_text(face = "italic"))+
theme(plot.title = element_text(size = "25"))+
scale_x_continuous(breaks=seq(0, 23, 1))+
theme(axis.text.x = element_text(angle = 90, hjust = 1),legend.position="none")
~
ggplot(a2016, aes(x=BOTTOM_TEMPERATURE_BEGINNING, y=SHOOTING_DEPTH, colour=1)) +
geom_errorbar(aes(ymin=SHOOTING_DEPTH-se, ymax=SHOOTING_DEPTH+se), width=.0) +
geom_line() +
geom_point()+
xlab("Temperature") +
ylab("Depth")+
ggtitle("Plot relation T° and Depth year 2016")+
theme(plot.title = element_text(hjust = 0.5))+
theme(plot.title = element_text(colour = "black"))+
theme(plot.title = element_text(face = "italic"))+
theme(plot.title = element_text(size = "25"))+
scale_x_continuous(breaks=seq(0, 23, 1))+
theme(axis.text.x = element_text(angle = 90, hjust = 1),legend.position="none")
~
[1]: https://i.stack.imgur.com/9XqCu.png
[2]: https://i.stack.imgur.com/E2eOh.png
答案 0 :(得分:0)
为此,我喜欢Cowplot程序包。 vignette非常清楚。
在您的情况下,请尝试以下操作:
plot1 = ggplot(a1994, aes(x=BOTTOM_TEMPERATURE_BEGINNING, y=SHOOTING_DEPTH, colour=1)) +
geom_errorbar(aes(ymin=SHOOTING_DEPTH-se, ymax=SHOOTING_DEPTH+se), width=.0) +
geom_line() +
geom_point()+
xlab("Temperature") +
ylab("Depth")+
ggtitle("Plot relation T° and Depth year 1994")+
theme(plot.title = element_text(hjust = 0.5))+
theme(plot.title = element_text(colour = "black"))+
theme(plot.title = element_text(face = "italic"))+
theme(plot.title = element_text(size = "25"))+
scale_x_continuous(breaks=seq(0, 23, 1))+
theme(axis.text.x = element_text(angle = 90, hjust = 1),legend.position="none")
plot2 = ggplot(a2016, aes(x=BOTTOM_TEMPERATURE_BEGINNING, y=SHOOTING_DEPTH, colour=1)) +
geom_errorbar(aes(ymin=SHOOTING_DEPTH-se, ymax=SHOOTING_DEPTH+se), width=.0) +
geom_line() +
geom_point()+
xlab("Temperature") +
ylab("Depth")+
ggtitle("Plot relation T° and Depth year 2016")+
theme(plot.title = element_text(hjust = 0.5))+
theme(plot.title = element_text(colour = "black"))+
theme(plot.title = element_text(face = "italic"))+
theme(plot.title = element_text(size = "25"))+
scale_x_continuous(breaks=seq(0, 23, 1))+
theme(axis.text.x = element_text(angle = 90, hjust = 1),legend.position="none")
library(cowplot)
plot_grid(plot1, plot2, labels = c('plot1', 'plot2'))
答案 1 :(得分:0)
如果绘图是唯一构建的,请考虑grid.arrange
软件包中的gridExtra
。您可以将布局调整为包括标题的行数或列数。
library(ggplot2)
library(gridExtra)
p1 <- ggplot(a1994, ...)
p2 <- ggplot(a2016, ...)
p <- grid.arrange(p1, p2)
p
或者,由于两个图看起来是相同的图层(没有循环?),只需追加两个数据集并添加一个指标字段,然后运行facet_wrap
,它甚至允许行数和列数,甚至可以调整比例:
all_data <- rbind(transform(a1994, year = 1994),
transform(a2016, year = 2016))
ggplot(all_data, ...) +
... +
facet_wrap(~year)
答案 2 :(得分:0)
也尝试patchwork
plot1 + plot2 + patchwork::plot_layout(ncol = 1)