Four quadrant plot, 4 different variables in R

Viewed 242

I am trying to create a coordinate plot in R that shows all four quadrants. What I need is different from anything I've seen in typical data visualizations. I have attached a picture example of what I need to achieve.

You'll notice that there are no negative values because each quadrant is representing different variable combinations with scores ranging from 0 to 25.

I have not found an example of what I need to do, so I don't have 'tried and failed' code, but I have a sample data set in wide and long format depending on what the solution would require. I looked at plots in ggplot2, but I could only plot using 2 variables. I am not sure how to represent all 4 variables using something like geom_point.

I only want to show the coordinates for one person at a time, so I do not want everyone's score from the dataset to be plotted at once.

Any ideas on how to approach this is greatly appreciated!

Sample data:

df <- data.frame(
    name = c("Donald", "Minnie", "Pluto"),
    varA = c(3, 6, 15),
    varB = c(4, 17, 6),
    varC = c(26, 21, 13),
    varD = c(10, 19, 19)
    )

library(reshape2)
dflong <- melt(df, id.vars='name', measure.vars=c("varA", "varB", "varC", "varD"))

enter image description here

1 Answers

You could try this with tidyr, dplyr and ggplot2:

The plot may need some jigging about to make it look like you want.

If you only want one plot at a time just filter the data frame in the call to ggplot.

library(dplyr)
library(tidyr)
library(ggplot2)

df <- data.frame(
  name = c("Donald", "Minnie", "Pluto"),
  varA = c(3, 6, 15),
  varB = c(4, 17, 6),
  varC = c(26, 21, 13),
  varD = c(10, 19, 19)
)

df1 <- 
  df %>% 
  pivot_longer(-name, names_to = "var", names_prefix = "var", values_to = "val") %>% 
  group_by(name) %>% 
  mutate(quad = paste(var, lead(var, default = "A"), sep = "-"),
         x = case_when(var == "A" ~ lead(val),
                       var == "B" ~ val,
                       var == "C" ~ -lead(val),
                       var == "D" ~ -val),
         y = case_when(var == "A" ~ val,
                       var == "B" ~ -lead(val),
                       var == "C" ~ -val,
                       var == "D" ~ lag(val, n = 3)))
# helper value
val_max <- round(max(df1$val), -1)

# axis data
df_ax <- data.frame(ax = rep(c("x", "y"), each = 2),
                    x = c(-val_max, val_max, 0, 0),
                    y = c(0, 0, -val_max, val_max)) 
#labels for variables
labs <- data.frame(label = LETTERS[1:4],
                   val = val_max,
                   x = c( 0, val_max, 0, -val_max),
                   y = c( val_max, 0, -val_max, 0))

ggplot(df1, aes(x, y))+
  geom_point(size = 3)+
  facet_wrap(~name)+
  geom_line(data = df_ax, aes(x, y, group = ax))+
  geom_text(data = labs, aes(x, y, label = label),
            nudge_x = c(0, 2, 0, -2),
            nudge_y = c(2, 0, -2, 0),
            size = 4)+
  geom_text(data = labs, aes(x, y, label = val),
            nudge_x = c( -2, -2, -2, 2),
            nudge_y = c( -2, -2, 2, -2),
            size = 3)+
  geom_text(aes(x = 2, y = 2, label = 0), size = 3, check_overlap = TRUE)+
  coord_fixed(xlim = c(-val_max - 2, val_max + 2),
              ylim = c(-val_max -2 , val_max + 2))+
  labs(x = NULL,
       y = NULL)+
  theme(panel.background = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank())

Created on 2021-08-24 by the reprex package (v2.0.0)

Related