How to change the order of aesthetic layers in ggplot?

后端 未结 3 416
天涯浪人
天涯浪人 2021-01-15 19:04

How can I change the order of aestetics layers? Here\'s and example

dat <- tibble (acc = rep(c(0,1), 200),
               rt = rnorm(400, 0.5, 0.1))

dat          


        
3条回答
  •  星月不相逢
    2021-01-15 19:23

    Ok, I just wanted to add an answer concerning the brewer palettes, which is more straightforward.

    So, original data:

    dat <- tibble (acc = rep(c(0,1), 200),
                   rt = rnorm(400, 0.5, 0.1))
    
    dat %>% ggplot(aes(x = rt, fill = factor(acc))) + 
      geom_density(aes(y= ..count..*0.03), alpha = 0.6)+
      scale_fill_brewer(palette = "Set1")
    

    Switch colors

    dat %>% ggplot(aes(x = rt, fill = factor(acc))) + 
      geom_density(aes(y= ..count..*0.03), alpha = 0.6)+
      scale_fill_brewer(palette = "Set1", direction = -1)
    

    Switch position

    dat %>% ggplot(aes(x = rt, fill = factor(acc, levels = c(1,0)))) + 
      geom_density(aes(y= ..count..*0.03), alpha = 0.6)+
      scale_fill_brewer(palette = "Set1", direction = -1)
    

    Switch position and color

    dat %>% ggplot(aes(x = rt, fill = factor(acc, levels = c(1,0)))) + 
      geom_density(aes(y= ..count..*0.03), alpha = 0.6)+
      scale_fill_brewer(palette = "Set1")
    

提交回复
热议问题