Ener
Ener

Reputation: 13

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

[enter image description here][1]I'm a beginner with the program R in ggplot2 and i am new on this site. I've [tried][2] to mix my plots but i can't do it. In the dataset there are a fishing trawl data. I've done the two plot in the year 1994 and 2016, now i would to put one next the other like the function par(mfcol=c()). Thanks

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.sstatic.net/9XqCu.png
  [2]: https://i.sstatic.net/E2eOh.png

Upvotes: 1

Views: 1139

Answers (3)

cephalopod
cephalopod

Reputation: 1906

Also try patchwork

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

https://github.com/thomasp85/patchwork

Upvotes: 1

Parfait
Parfait

Reputation: 107567

Consider grid.arrange from the gridExtra package if plots are uniquely built. You can adjust layout to including number of rows or columns including title.

library(ggplot2)
library(gridExtra)

p1 <- ggplot(a1994, ...)
p2 <- ggplot(a2016, ...)

p <- grid.arrange(p1, p2)
p

Alternatively, since the two plots appear to be same layers (no loop?), simply append the two data sets adding an indicator field and run facet_wrap which also allows number of rows and columns, even scales adjustment:

all_data <- rbind(transform(a1994, year = 1994),
                  transform(a2016, year = 2016))

ggplot(all_data, ...) +
  ... +
  facet_wrap(~year)

Upvotes: 1

Leo Brueggeman
Leo Brueggeman

Reputation: 2521

I like the cowplot package for this. The vignette is very clear.

In your case, try the following:

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

Upvotes: 0

Related Questions