Search code examples
rplotscatter

R Pre/Post Drop Lines Scatterplot


Is there a way to create a scatter plot with paired points. For example, I have 50 patients with preoperative and postoperative BMI values. I want to visualize each of these points on a vertical line so the change is clear. I have tried ggpaired function but this just creates a chart with differing conditions. The conditions would be the same.

Hybrid ScatterPlot:

enter image description here


Solution

  • I'm guessing your data looks similar to this random patients example

    library(tidyverse)
    
    patients <- tibble(
        id = rep(1:50, each = 2),
        group = rep(c("Placebo", "Treatment"), each = 50),
        stage = rep(c("Pre", "Post"), times = 50),
        measurement = rnorm(100)
    )
    
    patients
    #> # A tibble: 100 x 4
    #>       id group   stage measurement
    #>    <int> <chr>   <chr>       <dbl>
    #>  1     1 Placebo Pre       -0.710 
    #>  2     1 Placebo Post      -1.20  
    #>  3     2 Placebo Pre       -0.513 
    #>  4     2 Placebo Post      -0.0675
    #>  5     3 Placebo Pre       -0.346 
    #>  6     3 Placebo Post       0.467 
    #>  7     4 Placebo Pre        0.626 
    #>  8     4 Placebo Post       0.884 
    #>  9     5 Placebo Pre        0.0290
    #> 10     5 Placebo Post       1.43  
    #> # ... with 90 more rows
    

    A simple approach with ggplot2 would be something like this

    ggplot(patients, aes(id, measurement)) +
        geom_line(aes(group = id, color = group))
    

    Created on 2021-06-22 by the reprex package (v2.0.0)