Search code examples
rggplot2lineboxplot

How to add lines to connect certain data points across multiple boxplots in R?


I am trying to create a graph similar to this, where one specific data point for each boxplot is connected to the next through a red line.

My current code is:

p <- ggplot(melt_opt_base, aes(factor(variable), value))
p + geom_boxplot() + labs(x = "Variable", y = "Value")

And the current graphs looks like this. Assuming the data points to connect are:

points = c(0, 0.1, 0.2, 0.3, 0, 0.2, 0.2, 0.1, 0.3)

Does anyone know how I could add a line connecting these points across the nine adjacent boxplots, so that it would look like this instead?


Solution

  • This could be achieved like so:

    1. Make a dataframe with your categories and the point values
    2. Pass this dataframe as data to geom_line and or geom_point

    Making use of ggplot2::diamonds as example data:

    library(ggplot2)
    
    points <- aggregate(price ~ cut, FUN = mean, data = diamonds)
    
    points
    #>         cut    price
    #> 1      Fair 4358.758
    #> 2      Good 3928.864
    #> 3 Very Good 3981.760
    #> 4   Premium 4584.258
    #> 5     Ideal 3457.542
    
    ggplot(diamonds, aes(cut, price)) + 
      geom_boxplot() +
      geom_point(data = points, color = "red") +
      geom_line(data = points, aes(group = 1), color = "red")