1

[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.stack.imgur.com/9XqCu.png
  [2]: https://i.stack.imgur.com/E2eOh.png
BPDESILVA
  • 2,040
  • 5
  • 15
  • 35
Ener
  • 13
  • 3
  • 3
    bind_rows() those two dataframes together and then use facet_wrap() or look at https://stackoverflow.com/questions/1249548/side-by-side-plots-with-ggplot2?rq=1 for more suggestions – atiretoo Mar 27 '19 at 20:18

3 Answers3

1

Also try patchwork

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

https://github.com/thomasp85/patchwork

cephalopod
  • 1,826
  • 22
  • 31
0

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'))
Leo Brueggeman
  • 2,241
  • 1
  • 9
  • 5
  • No problem! Please accept it as the answer if it worked for you. – Leo Brueggeman Mar 27 '19 at 20:48
  • If i would mix the two plots in one plot?is it possible? – Ener Mar 27 '19 at 20:55
  • It should be possible to mix them. I would experiment with the group option in aes. If you can combine your dataframes into a single dataframe (using rbind), then add a grouping variable for each year (eg, df$year = 1994 or 2016, do this before the rbind), then lastly in your ggplot call try to add (aes(x=BOTTOM_TEMP... group=year)). If you want more help on this problem I would suggest opening another question (since the above question is about plotting side by side) and providing a reproducible example complete with data. – Leo Brueggeman Mar 27 '19 at 21:29
0

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)
Parfait
  • 104,375
  • 17
  • 94
  • 125