User33268
User33268

Reputation: 159

How to change the order of aesthetic layers in ggplot?

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 %>% ggplot(aes(x = rt, fill = factor(acc))) + 
  geom_density(aes(y= ..count..*0.03), alpha = 0.6)

enter image description here

This code plots this image. Here, the green (1) layer is above the red (0) layer. How can I place the red (0) layer on top of the green (1)?

I have tried

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

but this results in switching colors and positions!

enter image description here

Upvotes: 1

Views: 4351

Answers (3)

tjebo
tjebo

Reputation: 23737

Interesting. Usually it's very simple, just adding direction= -1 to your scale_fill function. But this does not currently work.

If you want to keep the ggplot2 default palette (which I think may not be the best idea), then the following approach works (inspired by this answer)

With the latest scale package v.1.1.0 , the direction = -1 argument seems to break the scale::hue_pal() function (see this SO thread or this github bug report), so here would be a workaround with explicitely calling scales::hue_pal in order to create your palette.

library(tidyverse)

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_manual(values = rev(scales::hue_pal()(length(unique(dat$acc)))))

I generally recommend rather to use other colors than the default palette. A good choice is colorbrewer - very good is colorbrewer2.org which helps you finding good palettes.

An example using "Dark2":


dat %>% ggplot(aes(x = rt, fill = factor(acc))) +
  geom_density(aes(y = ..count.. * 0.03), alpha = 0.6) +
scale_fill_brewer(type = 'qual', palette = 'Dark2')

Created on 2020-02-04 by the reprex package (v0.3.0)

Upvotes: 1

User33268
User33268

Reputation: 159

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")

enter image description here

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)

enter image description here

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)

enter image description here

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")

enter image description here

Upvotes: 1

Jrm_FRL
Jrm_FRL

Reputation: 1413

You could re-order the levels of your factor and add the color adjustment:

dat %>% ggplot(aes(x = rt, 
                   fill = factor(acc, levels = c(1,0)))) + 
  geom_density(aes(y= ..count..*0.03), alpha = 0.6)+
scale_fill_manual(values = c("1" = "#00BFC4", "0" = "#F8766D"))

Upvotes: 6

Related Questions