#load packages library(reshape2) library(ggplot2) library(ggsignif) #set directory to the working directory setwd("c:/Users/(username)/Desktop") experiment <- read.csv("experiment.csv") #open csv View(experiment) #view experiment #get imformation on data class(experiment) str(experiment) colnames(experiment) dim(experiment) #change from wide format to long format experiment_long <- melt(experiment, id.vars=c("subject", "sex"), variable.name="condition", value.name="measurement" ) View(experiment_long) #view experiment_long #get imformation on data max(experiment_long$measurement) min(experiment_long$measurement) mean(experiment_long$measurement) unique(experiment_long$condition) length(experiment_long$sex[which(experiment_long$sex=="Female")]) #plot initial graph ggplot(data=experiment_long, aes(x=condition, y=measurement, fill=condition)) + geom_boxplot() #add scale breaks ggplot(data=experiment_long, aes(x=condition, y=measurement, fill=condition)) + geom_boxplot() + scale_x_discrete(labels=c("Control", "Condition 1", "Condition 2")) #add titles ggplot(data=experiment_long, aes(x=condition, y=measurement, fill=condition)) + geom_boxplot() + scale_x_discrete(labels=c("Control", "Condition 1", "Condition 2")) + labs(x="Treatment", y="Measurment") + ggtitle("Measurment per Condition") #change graph theme ggplot(data=experiment_long, aes(x=condition, y=measurement, fill=condition)) + geom_boxplot() + scale_x_discrete(labels=c("Control", "Condition 1", "Condition 2")) + labs(x="Treatment", y="Measurment") + ggtitle("Measurment per Condition") + theme_classic() #design titles ggplot(data=experiment_long, aes(x=condition, y=measurement, fill=condition)) + geom_boxplot() + scale_x_discrete(labels=c("Control", "Condition 1", "Condition 2")) + labs(x="Treatment", y="Measurment") + ggtitle("Measurment per Condition") + theme_classic() + theme(axis.text=element_text(size = 15), axis.title = element_text(size=20, face = "bold", color = "black"), plot.title = element_text(size=25, face = "bold", hjust = 0.5, color = "black")) #design legend ggplot(data=experiment_long, aes(x=condition, y=measurement, fill=condition)) + geom_boxplot() + scale_x_discrete(labels=c("Control", "Condition 1", "Condition 2")) + labs(x="Treatment", y="Measurment") + ggtitle("Measurment per Condition") + theme_classic() + theme(axis.text=element_text(size = 15), axis.title = element_text(size=20, face = "bold", color = "black"), plot.title = element_text(size=25, face = "bold", hjust = 0.5, color = "black")) + theme(legend.title = element_text(size=20, face = "bold", colour = "black"), legend.text=element_text(size=15), legend.background = element_rect(fill = "white", colour = "black"), legend.position="right") #change legend labels ggplot(data=experiment_long, aes(x=condition, y=measurement, fill=condition)) + geom_boxplot() + scale_x_discrete(labels=c("Control", "Condition 1", "Condition 2")) + labs(x="Treatment", y="Measurment") + ggtitle("Measurment per Condition") + theme_classic() + theme(axis.text=element_text(size = 15), axis.title = element_text(size=20, face = "bold", color = "black"), plot.title = element_text(size=25, face = "bold", hjust = 0.5, color = "black")) + theme(legend.title = element_text(size=20, face = "bold", colour = "black"), legend.text=element_text(size=15), legend.background = element_rect(fill = "white", colour = "black"), legend.position="right") + scale_fill_discrete(labels=c("Control", "Condition 1", "Condition 2")) #add statistical information ggplot(data=experiment_long, aes(x=condition, y=measurement, fill=condition)) + geom_boxplot() + scale_x_discrete(labels=c("Control", "Condition 1", "Condition 2")) + labs(x="Treatment", y="Measurment") + ggtitle("Measurment per Condition") + theme_classic() + theme(axis.text=element_text(size = 15), axis.title = element_text(size=20, face = "bold", color = "black"), plot.title = element_text(size=25, face = "bold", hjust = 0.5, color = "black")) + theme(legend.title = element_text(size=20, face = "bold", colour = "black"), legend.text=element_text(size=15), legend.background = element_rect(fill = "white", colour = "black"), legend.position="right") + scale_fill_discrete(labels=c("Control", "Condition 1", "Condition 2")) + stat_signif(map_signif_level = TRUE, comparisons = list(c("control", "cond1")), test="t.test", textsize=7, y_position = 14) + stat_signif(map_signif_level = TRUE, comparisons = list(c("control", "cond2")), test="t.test", textsize=7, y_position = 15) #add facets ggplot(data=experiment_long, aes(x=condition, y=measurement, fill=condition)) + geom_boxplot() + scale_x_discrete(labels=c("Control", "Condition 1", "Condition 2")) + labs(x="Treatment", y="Measurment") + ggtitle("Measurment per Condition") + theme_classic() + theme(axis.text=element_text(size = 15), axis.title = element_text(size=20, face = "bold", color = "black"), plot.title = element_text(size=25, face = "bold", hjust = 0.5, color = "black")) + theme(legend.title = element_text(size=20, face = "bold", colour = "black"), legend.text=element_text(size=15), legend.background = element_rect(fill = "white", colour = "black"), legend.position="right") + scale_fill_discrete(labels=c("Control", "Condition 1", "Condition 2")) + stat_signif(map_signif_level = TRUE, comparisons = list(c("control", "cond1")), test="t.test", textsize=7, y_position = 14) + stat_signif(map_signif_level = TRUE, comparisons = list(c("control", "cond2")), test="t.test", textsize=7, y_position = 15) + facet_wrap(~sex) + theme(strip.text.x = element_text(size=13, face = "bold", colour = "white"), strip.background = element_rect(colour="black", fill="#1c8593"))