简体   繁体   中英

Creating a composite plot using ggplot in R

I am pretty new to R and am trying to create a composite plot using ggplot. I have searched how to do this and have seen I can use the facet function, however, it seems that this is for plotting data which can be split by type eg male/female. I have a data frame and I want to plot recovery against concentration, and recovery against equilibrium time on separate plots but as a composite plot. For this I have the following code:

p1 <- ggplot(dat2, aes(x = EqmTime, y = Recovery))
limits <- aes(ymax = Recovery + RecoveryError, ymin=Recovery - RecoveryError)

p1 + geom_point(size = 4) + geom_errorbar(limits, width=4) + geom_smooth(method = "lm", se = FALSE, colour="gray", size=1.5, linetype="dashed") +
labs(x='Equilibrium Time (hrs)', y='Nitrate Recovery (%)') + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank(),
    panel.background = element_blank()) 

p2 <- ggplot(dat2, aes(x = StockConc, y = Recovery))
limits <- aes(ymax = Recovery + RecoveryError, ymin=Recovery - RecoveryError)

p2 + geom_point(size = 4) + geom_errorbar(limits, width=0.1) + geom_smooth(method = "lm", se = FALSE, colour="gray", size=1.5, linetype="dashed") +
  labs(x='Concentration (g L-1)', y='Nitrate Recovery (%)') + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank(),
    panel.background = element_blank())

Additionally, I also have a problem that I cannot get the '-1' in the x axis label of plot 2 as a superscript, and am having trouble setting axis limits. When I set, for example, xlim=20-180, the axis doesn't start and finish at these, but makes these the major tick marks.

I would greatly appreciate any help with this! I know some of these issues have been addressed in other posts but I cannot seem to use this advise to sort the issue here.

From your question, I understand that you want to plot both the ggplots in single plot window. You can do this using gridextra package as:

library(gridExtra)
grid.arrange(p1, p2, nrow=2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM