Search code examples
rggplot2boxplotmagrittrggpubr

ggpaired (boxplot) pairs incorrectly the data points


I have a data frame for which I want to plot a boxplot to compare observations at different conditions.

But when I plot it, the lines that connect the data points across both boxplots are completely wrong:

if(!require(ggpubr)) {install.packages('ggpubr')} else {library(ggpubr)}

df = structure(list(y = c(1, 1, 1, 1, 2, 2), 
               group = c("a", "a" , "b", "b", "c", "c"), 
               x = c("alpha", "beta", "alpha", "beta","alpha", "beta")),
          class = "data.frame", 
          row.names = c(NA, -6L))

ggpaired(data = df, x = 'x', y = 'y')

enter image description here

As you can see, the lines connecting the lines should be horizontal, as the values do not change across conditions; but instead, they connect randomly to each other.


Solution

  • You forgot to add id = "group".

    ggpaired(data = df, x = 'x', y = 'y', id = "group")
    

    enter image description here