簡體   English   中英

如何使用ggplot2在一個繪圖中合並兩個或多個繪圖

[英]How to combine two or more plots in one plot with ggplot2

[在此處輸入圖片描述] [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

我喜歡這種牛頭包。 小插圖非常清晰。

對於您的情況,請嘗試以下操作:

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'))

考慮grid.arrangegridExtra包,如果地塊是唯一建。 您可以將布局調整為包括標題的行數或列數。

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)

也嘗試patchwork

plot1 + plot2 + patchwork::plot_layout(ncol = 1)

https://github.com/thomasp85/patchwork

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM