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

3 Answers 3

1

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)
Sign up to request clarification or add additional context in comments.

Comments

1

Also try patchwork

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

https://github.com/thomasp85/patchwork

Comments

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

3 Comments

No problem! Please accept it as the answer if it worked for you.
If i would mix the two plots in one plot?is it possible?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.