Search code examples
rggplot2colorspoint

Adjusting color of geom_point to reflect difference in sample means?


I am attempting to visualize my paired t-test with ggplot in R. I want to plot both samples against each other using ggplot + geom_point(), but I want to adjust the color of the points to show the severity in difference of sample means. With this, I mean if the point has a 0 difference in means for the paired populations, it'll lie on the regression line and just be a black point. If the point has a large difference and is in the upper left or lower right quadrants of the graph, far away from the regression line, I want it to be red. On a gradient scale. I hope this makes sense! I'll attach a picture of my graph to show:

graph

Right now the only aesthetic I have is alpha so the points are more visible.

Any help on this would be great! Thanks!


Solution

  • You didn't give us any sample data to work with, so I have simulated an approximation of yours:

    set.seed(69)
    measurement1 <- 1:1500
    measurement2 <- 1:1500 + c(rnorm(50, 1200, 50), rnorm(1400, 0, 99), rnorm(50, -1400, 50))
    
    df <- data.frame(measurement1, measurement2)
    df <- df[df$measurement1 > 0 & df$measurement2 > 0, ]
    

    So now we just need to take the absolute difference between each pair of measurements as our colouring variable:

    library(ggplot2)
    
    ggplot(df, aes(measurement1, measurement2)) +
      geom_line(aes(y = measurement1), size = 2, colour = "gold") +
      geom_point(aes(colour = abs(measurement2 - measurement1))) +
      scale_colour_gradient(low = "orange", high = "red", name = "Difference") +
      labs(y = "Adjusted.Function.Points", x = "Functional.Size")
    

    Created on 2020-06-08 by the reprex package (v0.3.0)