ggpairs factor not labeling as expected

91 views Asked by At

I have the following data, and I transform "method" to a factor then do a ggpairs plot. However, as you can see, the ggpairs plot does not label the two factor levels in the output as I expected. based on other examples, it should label "MBI" and "traditional" above the box-plots.

structure(list(ID = c(4L, 5L, 6L, 8L, 12L, 13L, 15L, 16L, 17L, 
18L, 21L, 22L, 24L, 25L, 28L, 29L, 30L, 31L, 33L, 34L, 35L, 36L, 
37L, 39L, 40L, 44L, 46L, 48L, 49L, 51L, 52L, 53L, 54L, 55L, 56L, 
57L, 60L), OptPost = c(50L, 67L, 61L, 92L, 59L, 16L, 42L, 69L, 
42L, 59L, 44L, 52L, 84L, 91L, 84L, 59L, 84L, 84L, 59L, 67L, 67L, 
67L, 70L, 50L, 84L, 50L, 20L, 46L, 42L, 44L, 46L, 50L, 55L, 75L, 
50L, 39L, 55L), method = c("MBI", "MBI", "MBI", "MBI", "MBI", 
"MBI", "MBI", "MBI", "MBI", "MBI", "MBI", "MBI", "MBI", "MBI", 
"MBI", "MBI", "MBI", "MBI", "MBI", "MBI", "MBI", "traditional", 
"traditional", "traditional", "traditional", "traditional", "traditional", 
"traditional", "traditional", "traditional", "traditional", "traditional", 
"traditional", "traditional", "traditional", "traditional", "traditional"
), gender = c(0L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 
0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 
1L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L), OptPre = c(50L, 50L, 30L, 
67L, 42L, 8L, 8L, 12L, 42L, 50L, 0L, 25L, 50L, 83L, 17L, 17L, 
24L, 44L, 44L, 44L, 28L, 55L, 58L, 42L, 71L, 41L, 15L, 42L, 17L, 
44L, 16L, 32L, 50L, 42L, 32L, 30L, 42L)), class = "data.frame", row.names = c(NA, 
-37L))

optics_df <- optics_df %>% mutate(method = as.factor(method))

ggpairs(optics_df[, -1],
        mapping = ggplot2::aes(color = method),)

ggpairs(optics_df[, -1], aes(color = method)) #same as above

ggpairs plot

I attempted to ggpairs plot for a dataset, expected factor to be labeled automatically yet it is not.

Note: while a legend is acceptable as a solution, we really want it to be included in the labels as with this example for gender: ggpairs plot example

edit: the answers provided so far are good and very helpful! but they don't fix the issue of labeling the actual column, they only add a legend.

I did some additional experimenting and I was able to notice that when we move the 'method' column to the end of the dataframe, the labels are generated automatically. can someone with a better understanding of ggpairs explain why this is the case? ggpairs fixed

1

There are 1 answers

0
Quinten On

Since you want to have the legend of the boxplot, you could use the legend argument:

a numeric vector of length 2 provides the location of the plot to use the legend for the plot matrix's legend. Such as legend = c(3,5) which will use the legend from the plot in the third row and fifth column

So the first row and second column should be your input. Here is some reproducible code:

library(GGally)
ggpairs(optics_df[, -1],
        mapping = ggplot2::aes(color = method),
        legend = c(1,2))

To have it more generalized you could use the legend of the barplot which shows the colors of the factor like this:

library(GGally)
ggpairs(optics_df[, -1],
        mapping = ggplot2::aes(color = method),
        legend = c(2,2))

Created on 2023-10-27 with reprex v2.0.2