Home > Software design >  How to add groups in connecting lines connecting dots in boxplots in R
How to add groups in connecting lines connecting dots in boxplots in R

Time:12-21

I made the following dataset:

before <- c(100, 120, 140, 130, 120, 100, 100)  
after <- c(140, 100, 160, 120, 90, 70, 70)  
pain_group <- c(1, 0, 1, 0, 0, 0, 0)  
d <- data.frame(before=before, after=after, pain_group=pain_group)

d$id <- 1:nrow(d)
d <- tidyr::gather(d, Measurement, quantity, -id)

I have plotted the data in boxplots with seperate dots and connecting lines:

ggplot(d2, aes(Measurement, quantity_cluster2))   
  geom_boxplot()    
  geom_point()  
  geom_line(aes(group = id), color = 'grey')  
  scale_x_discrete(limits = c('before', 'after'))

enter image description here

However I want the pain_group seperated with different color lines (and dots). How can I make this possible?

Thanks in advance!

CodePudding user response:

You need to exclude pain_group from your gather so it is still a column in the long-form data, then add color = factor(pain_group) to aes():

d2 <- tidyr::gather(d, Measurement, quantity, -id, -pain_group)

ggplot(d2, aes(Measurement, quantity))   
  geom_boxplot()    
  geom_point(aes(color = factor(pain_group)))  
  geom_line(aes(group = id, color = factor(pain_group)))  
  scale_x_discrete(limits = c('before', 'after'))

enter image description here

You can customize the legend title with labs(), or the legend title, labels, and colors with scale_color_continuous().

CodePudding user response:

Not a direct answer to your question, but a suggestion for a (probably) more compelling visualisation. You're dealing with paired data, so bring it into two dimensions. A scatter plot is perfect for that purpose. Your data is already in the right shape, and you can add a line of equality to make the comparison easier.

library(ggplot2)
before <- c(100, 120, 140, 130, 120, 100, 100)  
after <- c(140, 100, 160, 120, 90, 70, 70)  
pain_group <- c(1, 0, 1, 0, 0, 0, 0)  
d <- data.frame(before=before, after=after, pain_group=pain_group)

lims <- range(c(before, after))

ggplot(d)  
  geom_point(aes(before, after, color = as.character(pain_group)))  
  geom_abline(intercept = 0, slope = 1)  
  coord_cartesian(xlim = lims, ylim = lims)

Created on 2021-12-20 by the reprex package (v2.0.1)

  • Related