0

I have a strange issue which I can't seem to figure out - the legend groups in my ggplot2 plot are flipping. I've made a reproducible example below:

I have the following dataframe, animals:

animals <- structure(list(ID = c("A_7_1", "A_7_2", "A_7_3", "A_99_1", "A_99_2", 
"D_7_1", "D_7_3", "D_99_1", "D_99_2", "D_99_3", "H_7_1", "H_7_2", 
"H_7_3", "H_20_1", "H_20_2", "H_20_3", "M_7_1", "M_7_2", "M_7_3", 
"M_99_1", "M_99_2", "M_99_3", "U_7_1", "U_7_2", "U_7_3", "U_99_1", 
"U_99_2", "U_99_3"), Val1 = c(-116, -118, -119, -123, -124, -110, 
-109, -116, -115, -115, 365, 369, 360, 353, 350, 371, -92, -93, 
-93, -91, -92, -92, -78, -77, -78, -73, -72, -74), Val2 = c(-18, 
-15, -16, -10, -9, -41, -40, -41, -40, -41, -11, -10, -12, -10, 
-9, -14, -186, -183, -184, -183, -184, -179, 235, 240, 238, 239, 
242, 243), FirstGroup = c("Armadillo", "Armadillo", "Armadillo", 
"Armadillo", "Armadillo", "Dog", "Dog", "Dog", "Dog", "Dog", 
"Hippo", "Hippo", "Hippo", "Hippo", "Hippo", "Hippo", "Monkey", 
"Monkey", "Monkey", "Monkey", "Monkey", "Monkey", "Urchin", "Urchin", 
"Urchin", "Urchin", "Urchin", "Urchin"), SecondGroup = c("Untreated", 
"Untreated", "Untreated", "Treated", "Treated", "Untreated", 
"Untreated", "Treated", "Treated", "Treated", "Untreated", "Untreated", 
"Untreated", "Treated", "Treated", "Treated", "Untreated", "Untreated", 
"Untreated", "Treated", "Treated", "Treated", "Untreated", "Untreated", 
"Untreated", "Treated", "Treated", "Treated")), row.names = c(NA, 
-28L), class = "data.frame", prop_var = c(X = 0.43806, Y = 0.24221
))

head(animals)
      ID Val1 Val2 FirstGroup SecondGroup
1  A_7_1 -116  -18  Armadillo   Untreated
2  A_7_2 -118  -15  Armadillo   Untreated
3  A_7_3 -119  -16  Armadillo   Untreated
4 A_99_1 -123  -10  Armadillo     Treated
5 A_99_2 -124   -9  Armadillo     Treated
6  D_7_1 -110  -41        Dog   Untreated

I have a function to plot the values in this dataframe:

myplot <- function(x) {
    plot <- 
        ggplot(x,
            aes_string(
                x = paste0("`Val1`"),
                y = paste0("`Val2`"))
        ) +
        geom_point(
            aes_string(
                fill = paste0("`FirstGroup`"),
                shape = paste0("`SecondGroup`")),
            size = 4, alpha = 0.9, color = "black"
        ) +
        scale_fill_manual(
            values = fgplots::brew_fg_colors(length(unique(x$FirstGroup))),
            name = "FirstGroup",
            guide = guide_legend(override.aes = list(shape = 21))
        ) +
        scale_shape_manual(
            name = "SecondGroup", values = c(21, 22)
        )
    plot
}

I can run the following to make the plot:

png("myplot1.png")
myplot(animals)
dev.off()

enter image description here

However, when I filter my animals object to only include certain groups of animals, the group order flips in the legend:

animals_filt <- animals %>%
    dplyr::filter(FirstGroup %in% c("Armadillo", "Urchin"))

png("myplot2.png")
myplot(animals_filt)
dev.off()

enter image description here

I.e., SecondGroup has moved to the top in the legend, whereas I want the FirstGroup to be at the top. Why is this happening and how can I fix it?

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
icedcoffee
  • 935
  • 1
  • 6
  • 18

1 Answers1

1

Thanks to Richard Telford's comment above, I included order = 1 within my function which has fixed it. Thank you!

myplot <- function(x) {
    plot <- 
        ggplot(x,
            aes_string(
                x = paste0("`Val1`"),
                y = paste0("`Val2`"))
        ) +
        geom_point(
            aes_string(
                fill = paste0("`FirstGroup`"),
                shape = paste0("`SecondGroup`")),
            size = 4, alpha = 0.9, color = "black"
        ) +
        scale_fill_manual(
            values = fgplots::brew_fg_colors(length(unique(x$FirstGroup))),
            name = "FirstGroup",
            guide = guide_legend(override.aes = list(shape = 21), order = 1)
        ) +
        scale_shape_manual(
            name = "SecondGroup", values = c(21, 22)
        )
    plot
}
icedcoffee
  • 935
  • 1
  • 6
  • 18