Prepare required packages:
# if(!require(readr)) install.packages("readr")
# if(!require(dplyr)) install.packages("dplyr") #data_frame, %>%, filter, summarise, group_by
# if(!require(emmeans)) install.packages("emmeans") #package to run follow-up comparisons
# if(!require(phia)) install.packages("phia") #testInteractions
# if(!require(tidyr)) install.packages("tidyr") #spread
# if(!require(ARTool)) install.packages("ARTool") #art, artlm
# if(!require(ggplot2)) install.packages("ggplot2") #ggplot, stat_…, geom_…, etc
# if(!require(ggsignif)) install.packages("ggsignif")
# if(!require(tidyverse)) install.packages("tidyverse") # tidyverse for data manipulation and visualization
# if(!require(ggpubr)) install.packages("ggpubr") # ggpubr for creating easily publication ready plots
# if(!require(rstatix)) install.packages("rstatix") # rstatix provides pipe-friendly R functions for easy statistical analyses
# if(!require(lmerTest)) install.packages("lmerTest")
# if(!require(afex)) install.packages("afex") #package to run ANOVAs
# if(!require(plotly)) install.packages("plotly") #creating subplot
library(readr)
library(dplyr) #data_frame, %>%, filter, summarise, group_by
library(emmeans) #package to run follow-up comparisons
library(phia) #testInteractions
library(tidyr) #spread
library(ARTool) #art, artlm
library(ggplot2) #ggplot, stat_…, geom_…, etc
library(ggsignif)
library(tidyverse) # tidyverse for data manipulation and visualization
library(ggpubr) # ggpubr for creating easily publication ready plots
library(rstatix) # rstatix provides pipe-friendly R functions for easy statistical analyses
library(lmerTest)
library(afex) #package to run ANOVAs
library(plotly) #creating subplot
#== create a dummy dataset as an example
mydata <- data.frame(score = c(45 ,85, 66, 78, 92, 94, 91, 85, 62, 60,
40, 56, 70, 80, 90, 88, 95, 90, 45, 55,
84, 88, 88, 90, 92, 93, 91, 85, 80, 73,
97, 100, 93, 91, 90, 87, 94, 83, 92, 93,
45 ,85, 66, 78, 92, 94, 91, 85, 62, 60,
97, 100, 93, 91, 90, 87, 94, 83, 92, 93),
frequency = c(1 ,2, 3, 1, 2, 4, 1, 5, 2, 6,
1 ,2, 3, 1, 2, 4, 1, 5, 2, 6,
8, 8, 8, 9, 9, 9, 9, 8, 8, 7,
9, 10, 9, 9, 9, 7, 9, 8, 9, 9,
4, 6, 7, 8, 9, 8, 9, 9, 5, 5,
5 ,5, 6, 7, 2, 4, 1, 5, 2, 6),
condition = c(rep('condition1', 20), rep('condition2', 20), rep('condition3', 20)),
version = c(rep('versionA', 30), rep('versionB', 30)))
Customize color, font size, box width, outlier size, add mean on boxplot
Add stat significance notation
Customize font size, color, position, comparison test in geom_signif
Check more details about geom_signif here: https://rdrr.io/cran/ggsignif/man/stat_signif.html
### Boxplot ###
score_fig <- ggplot(mydata, aes(x=condition, y=score, fill=condition)) +
geom_boxplot(width=0.3, outlier.size=0.5) + #change width of the box and line
stat_summary(fun = mean, geom = "point", col = "black", shape=17) + # Add points to plot
stat_summary(fun = mean, geom = "text", col = "black", size = 2.7, # Add text to plot
vjust = 3, aes(label = paste("M:", round(..y.., digits = 2)))) +
#xlab("Conditions")+
theme(axis.text.x = element_text(angle = 0, vjust = 0.5, size=8))+
ylab("Score")+
#ylim(1, 7)+
ggtitle("Fig title here")+
theme(legend.position="top", plot.title = element_text(hjust = 0.5, size=10)) +
#scale_fill_brewer(palette="Dark2")+
scale_fill_manual(values=c("#6fa8dc", "#f1c232", "#bcbcbc"))+
# add sig notation
geom_signif(comparisons=list(c("condition1", "condition2")),
test = "wilcox.test",
na.rm = FALSE,
map_signif_level = TRUE,
col = "#5b5b5b",
size = 0.4, #change line size
y_position = 100)+
geom_signif(comparisons=list(c("condition2", "condition3")),
test = "wilcox.test",
na.rm = FALSE,
map_signif_level = TRUE,
col = "#5b5b5b",
textsize = 3, # adjust font size of NS
size = 0.4, #change line size
y_position = 98)+
geom_signif(comparisons=list(c("condition1","condition3")),
test = "wilcox.test",
na.rm = FALSE,
map_signif_level = TRUE,
col = "#5b5b5b",
size = 0.4, #change line size
y_position = 103)
score_fig
#ggsave(filename="myFig.pdf", plot=score_fig)
### Boxplot ###
frequency_fig <- ggplot(mydata, aes(x=version, y=frequency, color=version)) + #change the color of the border
geom_boxplot(width=0.3, outlier.size=0.5) + #change width of the box and line
stat_summary(fun = mean, geom = "point", col = "black", shape=17) + # Add points to plot
stat_summary(fun = mean, geom = "text", col = "black", size = 2.7, # Add text to plot
vjust = 3, aes(label = paste("M:", round(..y.., digits = 2)))) +
#xlab("Conditions")+
theme(axis.text.x = element_text(angle = 0, vjust = 0.5, size=8))+
ylab("Frequency")+
#ylim(1, 7)+
ggtitle("Fig title here")+
theme(legend.position="top", plot.title = element_text(hjust = 0.5, size=10)) +
#scale_fill_brewer(palette="Dark2")+
scale_color_manual(values=c("#6fa8dc", "#f1c232", "#bcbcbc"))+
# add sig notation
geom_signif(comparisons=list(c("versionA", "versionB")),
test = "wilcox.test",
na.rm = FALSE,
map_signif_level = TRUE,
col = "black",
size = 0.4, #change line size
y_position = 10)
frequency_fig
Combine multiple plots into one plot
Export the figure as PDF, PNG, etc.
figure <- ggarrange(score_fig, frequency_fig,
labels = c("A", "B"),
ncol = 2, nrow = 2,
common.legend = TRUE, legend = "top",
align = "hv",
font.label = list(size = 10, color = "black", face = "bold", family = NULL, position = "top"))
figure
#ggexport(figure, filename = "myplot.pdf")