# load tidyverse
library(tidyverse)
# create dataframe
sample_data <- data.frame( value = c(1,2,3,4,4,5,6,
7,9,11,1.5,2.3,2.5,3.4,
4.5,5.5,6.5,7.5,9.5,12.5),
category = c('A','B','A','B','A',
'B','A','B','A','B',
'A','B','A','B','A',
'B','A','B','A','B'),
paired = c(0,0,1,1,2,2,3,3,4,4,
5,5,6,6,7,7,8,8,9,9))
# create plot using ggplot() and geom_boxplot() functions
ggplot(sample_data, aes(category,value, fill=category)) +
geom_boxplot()+
# geom_line() joins the paired datapoints
# color and size parameters are used to customize line
geom_line(aes(group = paired), size=2, color='gray', alpha=0.6)+
# geom_point() is used to make points at data values
# fill and size parameters are used to customize point
geom_point(aes(fill=category,group=paired),size=5,shape=21)