M. Brink
M. Brink

Reputation: 43

R: ggplot2 density plot shows wrong fill colors

I would like to plot densities of two variables ("red_variable", "green_variable") from two independent dataframes on one density plot, using red and green color for the two variables.

This is my attempt at coding:

library(ggplot2)

### Create dataframes
red_dataframe <- data.frame(red_variable = c(10,11,12,13,14))
green_dataframe <- data.frame(green_variable = c(6,7,8,9,10))
mean(red_dataframe$red_variable) # mean is 12
mean(green_dataframe$green_variable) # mean is 8

### Set colors
red_color= "#FF0000"
green_color= "#008000"

### Trying to plot densities with correct colors and correct legend entries
ggplot() +
geom_density(aes(x=red_variable, fill = red_color, alpha=0.5), data=red_dataframe) + 
geom_density(aes(x=green_variable, fill = green_color, alpha=0.5), data=green_dataframe) +
scale_fill_manual(labels = c("Density of red_variable", "Density of green_variable"), values = c(red_color, green_color)) +
xlab("X value") +
ylab("Density") +
labs(fill = "Legend") +
guides(alpha=FALSE)

Result: The legend shows correct colors, but the colors on the plot are wrong: The "red" variable is plotted with green color, the "green" variable with red color. The "green" density (mean=8) should appear left and the "red" density (mean=12) on the right on the x-axis. This behavior of the plot doesn't make any sense to me.

I can in fact get the desired result by switching red and green in the code:

### load ggplot2
library(ggplot2)

### Create dataframes
red_dataframe <- data.frame(red_variable = c(10,11,12,13,14))
green_dataframe <- data.frame(green_variable = c(6,7,8,9,10))
mean(red_dataframe$red_variable) # mean is 12
mean(green_dataframe$green_variable) # mean is 8

### Set colors
red_color= "#FF0000"
green_color= "#008000"

### Trying to plot densities with correct colors and correct legend entries
ggplot() +
geom_density(aes(x=red_variable, fill = green_color, alpha=0.5), data=red_dataframe) + 
geom_density(aes(x=green_variable, fill = red_color, alpha=0.5), data=green_dataframe) +
scale_fill_manual(labels = c("Density of red_variable", "Density of green_variable"), values = c(red_color, green_color)) +
xlab("X value") +
ylab("Density") +
labs(fill = "Legend") +
guides(alpha=FALSE)

... While the plot makes sense now, the code doesn't. I cannot really trust code doing the opposite of what I would expect it to do. What's the problem here? Am I color blind?

Upvotes: 0

Views: 1246

Answers (2)

Gregor Thomas
Gregor Thomas

Reputation: 145755

You're trying to use ggplot as if it's base graphics... the mindset shift can take a little while to get used to. dc37's answer shows how you should do it. I'll try to explain what goes wrong in your attempt:

When you put fill = green_color inside aes(), because it's inside aes() ggplot essentially creates a new column of data filled with the green_color values in your green_data_frame, i.e., "#008000", "#008000", "#008000", .... Ditto for the red color values in the red data frame. We can see this if we modify your plot by simply deleting your scale:

ggplot() +
  geom_density(aes(x = red_variable, fill = green_color, alpha = 0.5), data =
                 red_dataframe) +
  geom_density(aes(x = green_variable, fill = red_color, alpha = 0.5), data =
                 green_dataframe) +
  xlab("X value") +
  ylab("Density") +
  labs(fill = "Legend") +
  guides(alpha = FALSE)

enter image description here

We can actually get what you want by putting the identity scale, which is designed for the (common in base, rare in ggplot2) case where you actually put color values in the data.

ggplot() +
  geom_density(aes(x = red_variable, fill = green_color, alpha = 0.5), data =
                 red_dataframe) +
  geom_density(aes(x = green_variable, fill = red_color, alpha = 0.5), data =
                 green_dataframe) +
  scale_fill_identity() +
  xlab("X value") +
  ylab("Density") +
  labs(fill = "Legend") +
  guides(alpha = FALSE)

enter image description here

When you added your scale_fill_manual, ggplot was like "okay, cool, you want to specify colors and labels". But you were thinking in the order that you added the layers to the plot (much like base graphics), whereas ggplot was thinking of these newly created variables "#FF0000" and "#008000", which it ordered alphabetically by default (just as if they were factor or character columns in a data frame). And since you happened to add the layers in reverse alphabetical order, it was switched.

dc37's answer shows a couple better methods. With ggplot you should (a) work with a single, long-format data frame whenever possible (b) don't put constants inside aes() (constant color, constant alpha, etc.), (c) set colors in a scale_fill_* or scale_color_* function when they're not constant.

Upvotes: 1

dc37
dc37

Reputation: 16178

On your code, in order to have color at the right position, you need to specify fill = red_color or fill = green_color (as well as alpha as it is a constant - as pointed out by @Gregor) outside of the aes such as:

...+ 
geom_density(aes(x=red_variable), alpha=0.5, fill = red_color, data=red_dataframe) + 
  geom_density(aes(x=green_variable), alpha=0.5, fill = green_color, data=green_dataframe) + ...

Alternatively, you can bind your dataframes together, reshape them into a longer format (much more appropriate to ggplot) and then add color column that you can use with scale_fill_identity function (https://ggplot2.tidyverse.org/reference/scale_identity.html):

df <- cbind(red_dataframe,green_dataframe)

library(tidyr)
library(ggplot2)
library(dplyr)
df <- df %>% pivot_longer(.,cols = c(red_variable,green_variable), names_to = "var",values_to = "val") %>%
  mutate(Color = ifelse(grepl("red",var),red_color,green_color))

ggplot(df, aes(val, fill = Color))+
  geom_density(alpha = 0.5)+
  scale_fill_identity(guide = "legend", name = "Legend", labels = levels(as.factor(df$var)))+
  xlab("X value") +
  ylab("Density")

enter image description here

Does it answer your question ?

Upvotes: 2

Related Questions