Home > Net >  Change ggplot2 legend order without changing the manually specified aesthetics
Change ggplot2 legend order without changing the manually specified aesthetics

Time:05-27

I need to make a graph with multiple kinds of data on it, and I'm plotting one type of data with lines and one type with points. I've added a manually specified legend to show which type is points and which is lines (admittedly, my approach is a bit hacky), and that's working except for the legend order. Here's a dummy example:

DF1 <- data.frame(X = 1:10, 
              Y = c(1:10*0.5, 1:10*0.25),
              Fruit = rep(c("mango", "kiwi"), each = 10))
DF2 <- data.frame(X = 1:10, 
                  Y = c(1:10*2, 1:10*4),
                  Cat = rep(c("tabby", "calico"), each = 10))
Empty <- data.frame(X = mean(DF$X),
                    Y = as.numeric(NA), 
                    # Q = c(0, 1))
                    Type = c("Cat", "Fruit"))
Mygraph <- ggplot(DF1, aes(x = X, y = Y, color = Fruit))  
    geom_point()   
    geom_line(data = DF2, aes(x = X, y = Y, linetype = Cat), 
              inherit.aes = F)  
    labs(color = NULL, linetype = NULL)  
    geom_point(data = Empty, aes(x = X, y = Y, alpha = Type),
               inherit.aes = F)  
    geom_line(data = Empty, aes(x = X, y = Y, alpha = Type),
              inherit.aes = F)  
    scale_alpha_manual(
        name = "Type of item", values = c(1, 1),
        breaks = c("Fruit", "Cat"),
        guide = guide_legend(override.aes =
                                 list(linetype = c("blank", "solid"),
                                      shape = c(16, NA))))  
    theme_bw()
Mygraph

This graph looks pretty good:

graphoriginal

But check out what happens to the "Type of item" bit when I try to specify the order:

 Mygraph   
     guides(alpha = guide_legend(order = 1),
            linetype = guide_legend(order = 2),
            color = guide_legend(order = 3))

graph2

Why do my specified aesthetics go away? How can I both specify what that part of the legend should look like and also specify that the order of the three parts of the legend should be 1. alpha, 2. linetype, and then 3. color?

CodePudding user response:

You were attempting to override aesthetics for alpha in two places (ie guides() and scale_alpha...()), and ggplot was choosing to just interpret one of them. I suggest including your shape override with your legend order override, like this:

library(ggplot2) 

ggplot(DF1, aes(x = X, y = Y, color = Fruit))  
  geom_point()   
  geom_line(data = DF2, aes(x = X, y = Y, linetype = Cat),  inherit.aes = F)  
  labs(color = NULL, linetype = NULL)  
  geom_point(data = Empty, aes(x = X, y = Y, alpha = Type), inherit.aes = F)  
  geom_line(data = Empty, aes(x = X, y = Y, alpha = Type), inherit.aes = F)  
  scale_alpha_manual(name = "Type of item", values = c(1, 1), breaks = c("Fruit", "Cat"))  
  guides(alpha = guide_legend(order = 1,
                              override.aes=list(linetype = c("blank", "solid"),
                                                shape = c(16,NA))),
         linetype = guide_legend(order = 2),
         color = guide_legend(order = 3))  
  theme_bw()

enter image description here

data:

DF1 <- data.frame(X = 1:10, 
                  Y = c(1:10*0.5, 1:10*0.25),
                  Fruit = rep(c("mango", "kiwi"), each = 10))
DF2 <- data.frame(X = 1:10, 
                  Y = c(1:10*2, 1:10*4),
                  Cat = rep(c("tabby", "calico"), each = 10))
Empty <- data.frame(X = mean(DF1$X),
                    Y = as.numeric(NA), 
                    Type = c("Cat", "Fruit"))
  • Related