I have some data where every person is either "satisfied" or "dissatisfied". Then, every person also has two types of calculated distances. I have no issues plotting the boxplot. However, I cannot figure out how to plot a line between the two medians of the boxplots.
Reproducible Code:
group1.ids <- c('A123', 'B123', 'C123', 'D123')
Dis.data <- data.frame(id = rep(group1.ids),
satisfaction = rep('Dissatisfied', 8),
DistType = c(rep('A', 4),
rep('B', 4)),
Dist = runif(8))
group2.ids <- c('E123', 'F123', 'G123', 'H123')
Sat.data <- data.frame(id = rep(group2.ids),
satisfaction = rep('Satisfied', 8),
DistType = c(rep('A', 4),
rep('B', 4)),
Dist = runif(8))
data <- rbind(Dis.data, Sat.data)
ggplot(data) +
geom_boxplot(mapping = aes(x = satisfaction, y = Dist, fill = DistType))
One way is the convert your x-axis to a continuous scale. To do this, we'll first
factor
ize yoursatisfaction
andDistType
variables (you can control the order using this andlevels=
if needed), and then we can usegeom_line
to add your lines.