Hi,
I would like to create a matrix of plots using ggplot2. I could obtain all the plots close together by removing margin, axes and so on... However, I would like to have some axes and titles on the edges. Here is the 2x2 plot matrix I get. At the bottom is what I would like it to look like. I know there is the possibility to add annotation. However, as I am applying the same ggplot functions for all plots I cannot choose to apply different parameters for the one on the borders. Is there a way to do so whilst still being able to use the code for other size of matrix? Also, I am open to any suggestions concerning my code, as I am a beginner.
Best
#-------------------------------------------------------------------------------
library(dplyr)
library(tidyr)
library(ggplot2)
library(gridExtra)
# generating example data
Compound<-c('A','B','C','D','A','B','C','D','A','B','C','D','A','B','C','D',
'A','B','C','D','A','B','C','D','A','B','C','D','A','B','C','D',
'A','B','C','D','A','B','C','D','A','B','C','D','A','B','C','D',
'A','B','C','D','A','B','C','D','A','B','C','D','A','B','C','D')
Sample <-c('140A1','140A1','140A1','140A1','140A1','140A1','140A1','140A1',
'140A1','140A1','140A1','140A1','140A1','140A1','140A1','140A1',
'140AW1','140AW1','140AW1','140AW1','140AW1','140AW1','140AW1','140AW1',
'140AW1','140AW1','140AW1','140AW1','140AW1','140AW1','140AW1','140AW1',
'140A2','140A2','140A2','140A2','140A2','140A2','140A2','140A2',
'140A2','140A2','140A2','140A2','140A2','140A2','140A2','140A2',
'140AW2','140AW2','140AW2','140AW2','140AW2','140AW2','140AW2','140AW2',
'140AW2','140AW2','140AW2','140AW2','140AW2','140AW2','140AW2','140AW2')
Time <-c(0,0,0,0,10,10,10,10,20,20,20,20,30,30,30,30,
0,0,0,0,10,10,10,10,20,20,20,20,30,30,30,30,
0,0,0,0,10,10,10,10,20,20,20,20,30,30,30,30,
0,0,0,0,10,10,10,10,20,20,20,20,30,30,30,30)
Area <-c(187.77,1.41445,2.94158,914.76,203.196,58.5594,174.663,490.947,193.526,65.828,209.102,
375.491,200.875,75.1088,252.319,315.408,151.246,5.67439,7.00333,930.658,182.185,269.221,
20.7183,6.67913,168.81,150.134,14.8289,8.76722,170.37,88.3018,12.5605,8.51715,184.348,
3.76239,17.4488,899.331,183.332,3.71532,18.7058,899.331,182.192,3.90458,18.5483,884.261,
191.143,3.38503,16.9406,934.068,170.954,2.13313,3.62187,960.455,171.759,188.635,254.459,
8.01105,164.464,186.912,193.351,7.50136,188.963,219.482,167.927,9.90597)
df1 <-data.frame(Compound,Sample,Time,Area)
list<-list("140A1","140AW1",
"140A2","140AW2")
cn <- unique(df1$Compound)
# for loop calculation done on the data to obtain "Yield" from "Area"
for (i in list)
{
r1 <- filter(df1,Sample==i,Compound %in% c("A","D") & Time == 0) %>% arrange(desc(Compound))
r2 <- r1$Area[[1]] / r1$Area[[2]]
assign(paste0("df", i),
pivot_wider(filter(df1, Sample==i), id_cols=c("Sample","Time"),
names_from = "Compound", values_from="Area") %>%
arrange(Sample,Time) %>% group_by(Sample) %>%
mutate(across(.cols=all_of(unique(filter(df1, Sample==i)$Compound)),
.fns =~{./A/r2})) %>%
mutate(across(.cols=all_of(setdiff(unique(filter(df1, Sample==i)$Compound),"D")),
.fns =~{.-first(.)})) %>%
select(-A) %>% ungroup() %>%
pivot_longer(cols = -c("Sample","Time")) %>%
select(Compound=name,Sample,Time,Yield=value) %>%
mutate(Yield=Yield*100) %>%
arrange(Sample,Compound))
}
# ggplots functions
b <- theme(axis.line=element_blank(),axis.text.x=element_blank(),
axis.text.y=element_blank(),axis.ticks=element_blank(),
axis.title.x=element_blank(),axis.title.y=element_blank(),
legend.position="none",panel.background=element_blank(),
panel.border=element_rect(colour = "black", fill=NA, size=1),
panel.grid.major=element_blank(),panel.grid.minor=element_blank(),
plot.background=element_blank(),plot.margin = unit(c(-0.05,-0.05,-0.05,-0.05),"cm"))
c <- scale_x_continuous(expand = c(0, 0))
d <- scale_y_continuous(expand = c(0, 0))
e <- coord_cartesian(xlim = c(0, 30),ylim=c(0,100))
colour<-c("#01b0f1","#2800d0","#ffc000")
f <- scale_color_manual(values=colour)
# for loop to create each plot
for (i in list)#annotate("text", x = c(2,4,6,8), y=10, label = c("two", "ship", "six", "boat"))
{
assign(paste0("df2", i), ggplot(get(paste0("df", i)), aes(x=Time, y=Yield, group=Compound)) + geom_path(size =0.5,aes(color = Compound)) + b + c + d + e + f )
}
# assemble all the plots
grid.arrange(df2140A1,df2140AW1,df2140A2,df2140AW2,nrow=2)