I have two boxplots each with its own values, but a common legend and y axis. I tried combining them using the patchwork function but how can I get them into the same plot along with the Tukeys letters on top of them? I have attached my code below and also the data files that I used for my analysis.
library(dplyr)
library(multcompView)
library(ggplot2)
library(patchwork)
#load data and create the locyear column
totalam = read.csv("barleyaap1.csv", header = TRUE, stringsAsFactors = T)
totalam$locyr = as.factor(paste(totalam$location," ",totalam$year, sep=''))
View(totalam)
str(totalam)
#use Tukeys to find pairwise comparison letters
summary(aov(adry ~ locyr, data = totalam))
letters.df= data.frame(multcompLetters(TukeyHSD(aov(adry ~ locyr, data =
totalam))$locyr[,4])$Letters)
colnames(letters.df)[1] <- "Letter"
letters.df$locyr <- rownames(letters.df) #Create column based on rownames
placement <- totalam %>% #We want to create a dataframe to assign the letter position.
group_by(locyr) %>%
summarise(quantile(adry)[4])
colnames(placement)[2] <- "Placement.Value"
letters.df <- left_join(letters.df, placement) #Merge dataframes
letters.df
#condensed data set load for ggplot
totalam2 = read.csv("barleyaap2.csv", header = TRUE, stringsAsFactors = T)
View(totalam2)
str(totalam2)
#draw box plot with whiskers
a= ggplot(totalam2,aes(x=locyr, y=avgam, fill=locyr))+
geom_boxplot(aes(lower=avgam-sdam,upper=avgam+sdam, middle=avgam,
ymin=minam,ymax=maxam), stat="identity")+
scale_y_continuous("% (dry basis)",limits = c(0,100), breaks = seq(0,100,5))+
theme_classic()+
scale_x_discrete("Location-Year")+
guides(fill=guide_legend(title="Location-Year"))+
theme(axis.title = element_text(size=14, color="black"),
axis.text = element_text(size=10, color="black"),
legend.title = element_text(size=14),
legend.text = element_text(size=10))+
ggtitle("Amylose content")+
geom_text(data = letters.df, aes(x = locyr, y = Placement.Value, label = Letter), size =
4, color = "black", hjust = -1, vjust = -2, fontface = "bold")
a
#plot2
#use Tukeys to find pairwise comparison letters
summary(aov(apdry ~ locyr, data = totalam))
letters.df= data.frame(multcompLetters(TukeyHSD(aov(apdry ~ locyr, data =
totalam))$locyr[,4])$Letters)
colnames(letters.df)[1] <- "Letter"
letters.df$locyr <- rownames(letters.df) #Create column based on rownames
placement <- totalam %>% #We want to create a dataframe to assign the letter position.
group_by(locyr) %>%
summarise(quantile(adry)[4])
colnames(placement)[2] <- "Placement.Value"
letters.df <- left_join(letters.df, placement) #Merge dataframes
letters.df
#draw box plot with whiskers
b=ggplot(totalam2,aes(x=locyr, y=avgap, fill=locyr))+
geom_boxplot(aes(lower=avgap-sdap,upper=avgap+sdap, middle=avgap,
ymin=minap,ymax=maxap),
stat="identity")+
scale_y_continuous("Total amylopectin % (dry basis)",limits = c(0,100), breaks =
seq(0,100,5))+
theme_classic()+
scale_x_discrete("Location-Year")+
guides(fill=guide_legend(title="Location-Year"))+
theme(axis.title = element_text(size=14, color="black"),
axis.text = element_text(size=10, color="black"),
legend.title = element_text(size=14),
legend.text = element_text(size=10))+
ggtitle("Amylopectin content")+
geom_text(data = letters.df, aes(x = locyr, y = Placement.Value, label = Letter), size =
4, color = "black", hjust = -1, vjust = -38, fontface = "bold")
b
#patch plots a and b
patchwork1 = a+b
# Remove title and legend from second subplot
patchwork1[[2]] = patchwork1[[2]] + theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.title.y = element_blank() )
patchwork1[[1]] = patchwork1[[1]] + theme(legend.position = "none")
patchwork1
Files I used: 3 data files