rggplot2statisticsvisualization

2x2 interaction plot when you only have summary statistics for each group


Thought someone here could give me some leads. I've been looking for a good example of a 2x2 interaction (i.e., cross-over) line plot, but all the examples I find assume you have participant-level data and are calculating the means or proportions you want to plot. I have many 2x2 tables of means and proportions that I want to plot (one for each table). Setting aside issues of multiple tests, having many plots to look at, etc., can anyone point me in the right direction? I've used ggplot2 for other kinds of plots, but I don't use it a lot. Didn't know if there was a package that's better for this now.

If a little more detail helps, my 2x2 table includes rows for mode (method of data collection), and columns for year, and I want to make a plot that looks like those here

See example table and plot from table linked below:

structure(list(...1 = c("Mode 1", "Mode 2"), 
    Year 1 = c(10, 40), Year 2 = c(20, 30)), 
    class = c("tbl_df", "tbl", "data.frame" ), 
    row.names = c(NA, -2L))

2x2 Plot from Table


Solution

  • I hate to say it, but this is probably a question that ChatGPT or some other LLM could answer well ... the only tricky parts below are that (1) you have to convert your data to long format and (2) because your x-axis variable is categorical, you need to specify a group mapping explicitly.

    df <- structure(list(`...1` = c("Mode 1", "Mode 2"), 
        `Year 1` = c(10, 40), `Year 2` = c(20, 30)), 
        class = c("tbl_df", "tbl", "data.frame" ), 
        row.names = c(NA, -2L))
    
    library(ggplot2)
    theme_set(theme_bw()) ## cosmetic
    ## wide to long (and rename first column)
    dfL <- tidyr::pivot_longer(df, -`...1`,
             names_to = "year") |>
        dplyr::rename(mode = "...1")
    ## plot
    ggplot(dfL, aes(x = year, y = value)) +
       geom_line(aes(colour = mode, group = mode))
    

    If you don't want the colours you don't have to specify that mapping.

    Going some distance toward replicating the look of the linked plots:

    theme_set(theme_classic())
    ggplot(dfL, aes(year, value)) +
        geom_line(aes(group = mode)) +
        geom_point(aes(shape=mode, fill = mode), size = 4) +
        scale_shape_manual(values = c(21, 22)) +
        scale_fill_manual(values = c("white", "black"))
    

    enter image description here