Home > Net >  Controlling ggplot2 legend order with new_scale_color() of ggnewscale
Controlling ggplot2 legend order with new_scale_color() of ggnewscale

Time:08-26

I can't seem to arrange the order of legend items when using the new_scale_color() feature of the ggnewscale package. Here's a minimal example:

n <- 10
sd_res <- 1
beta0 <- 0
beta1 <- 1

x <- runif(n, 0, 10)
y <- beta0   beta1*x   rnorm(n, 0, sd_res)
dat <- data.frame(x,y)

ggplot()   theme_minimal()   labs(x = '', y = '')   
  geom_abline(aes(color = 'x', linetype = 'x', slope = 1, intercept = 0))   
  geom_abline(aes(color = 'y', linetype = 'y', slope = 2, intercept = 0))  
  scale_color_manual(name = '', values = c('x' = 'orange', 'y' = 'black'))   
  scale_linetype_manual(name = '', values = c('x' = 1, 'y' = 2))   
  new_scale_color()   
  geom_point(data=dat, aes(x=x, y=y, shape = 'z', size = 'z', color = 'z'), alpha = 1)   
  scale_size_manual(name = '', values = c('z' = 1.5))  
  scale_shape_manual(name = '', values = c('z' = 16))  
  scale_color_manual(name = '', values = c('z' = 'black'))  
  theme(legend.position = 'bottom')

What I'd like is for the z-item to appear before the x- and y-items in the legend.

CodePudding user response:

You could use guides with guide_legend and specify the order of your aes. Here I set the order of your "z" to 1 which will set it as the first legend like this:

n <- 10
sd_res <- 1
beta0 <- 0
beta1 <- 1

x <- runif(n, 0, 10)
y <- beta0   beta1*x   rnorm(n, 0, sd_res)
dat <- data.frame(x,y)

library(ggplot2)
library(ggnewscale)
p <- ggplot()   theme_minimal()   labs(x = '', y = '')   
  geom_abline(aes(color = 'x', linetype = 'x', slope = 1, intercept = 0))   
  geom_abline(aes(color = 'y', linetype = 'y', slope = 2, intercept = 0))  
  scale_color_manual(name = '', values = c('x' = 'orange', 'y' = 'black'))   
  scale_linetype_manual(name = '', values = c('x' = 1, 'y' = 2))   
  new_scale_color()   
  geom_point(data=dat, aes(x=x, y=y, shape = 'z', size = 'z', color = 'z'), alpha = 1)   
  scale_size_manual(name = '', values = c('z' = 1.5))  
  scale_shape_manual(name = '', values = c('z' = 16))  
  scale_color_manual(name = '', values = c('z' = 'black'))  
  theme(legend.position = 'bottom')  
  guides(size = guide_legend(order = 1), 
         color = guide_legend(order = 1), 
         shape = guide_legend(order = 1))

p

Created on 2022-08-26 with reprex v2.0.2

  • Related