Search code examples
rplot

Need help plotting all possible combinations of 2 variables as a grid


I have a data set where one column is names, and the other is a series of colors. There are repeats in both columns. Along these lines:

X1       X2

Jon      Red
Brandon  Blue
Jared    Yellow
Aaron    Pink
Layton   Black
Colby    White
Brandon  Yellow
Aaron    Black
Zach     Red
Sean     Blue
Jared    Pink
Jon      White

I would like to plot these values as a grid, where on one axis are all unique names and hte other has all unique colors. Within the body of the plot, I would be marking which combinations of Name and Color have already occurred. Sort of like a bingo card. Rough example:

          Red    Blue    Yellow    Pink    Black    White

Jon        X                                          X
Brandon            X       X
Aaron                                X       X
Jared                      X         X
Colby                                                 X
Layton                                       X
Sean               X
Zach       X

Any ideas of how I would should approach this?

I don't have a lot of experience with plotting in R so I have been unsure where to start.


Solution

  • Here is a ggplot2 approach to achieve your desired result:

    dat <- structure(list(
      X1 = c(
        "Jon", "Brandon", "Jared", "Aaron", "Layton",
        "Colby", "Brandon", "Aaron", "Zach", "Sean", "Jared", "Jon"
      ),
      X2 = c(
        "Red", "Blue", "Yellow", "Pink", "Black", "White",
        "Yellow", "Black", "Red", "Blue", "Pink", "White"
      )
    ), class = "data.frame", row.names = c(
      NA,
      -12L
    ))
    
    library(ggplot2)
    library(dplyr, warn = FALSE)
    library(forcats)
    
    dat |> 
      mutate(across(c(X1, X2), forcats::fct_inorder)) |> 
      ggplot(aes(X2, X1)) +
      geom_point(shape = 4, size = 4) +
      scale_x_discrete(position = "top") +
      scale_y_discrete(limits = rev) +
      labs(x = NULL, y = NULL)