stackinator
stackinator

Reputation: 5819

scale_color_manual() for different geoms in ggplot

library(tidyverse)

delta <- tibble(
  type = c("alpha", "beta", "gamma"), 
  a = rnorm(3, 5), 
  b = rnorm(3, 6)
  ) %>% 
  mutate(delta = abs(a - b)) %>% 
  gather(`a`, `b`, `delta`, key = "letter", value = "value")


ggplot(delta %>% filter(letter != "delta"), aes(type, value, fill = letter)) +
  geom_col(position = "dodge") + 
  geom_col(data = delta %>% filter(letter == "delta"), width = 0.5) + 
  scale_color_manual("grey", "black", "blue")

I'd like the a and b bars to be grey and black. And the delta bar to be blue. How do I do this with scale_color_manual()? Seems my syntax above is off.

enter image description here

Upvotes: 4

Views: 1767

Answers (1)

Indrajeet Patil
Indrajeet Patil

Reputation: 4889

There are two things that need to be changed:

  1. Since you've used fill = letter, you should use scale_fill_manual instead of scale_color_manual (which would have been appropriate if you had used color = letter).

  2. The manual color values should be provided as a vector.

library(tidyverse)

delta <- tibble(
  type = c("alpha", "beta", "gamma"), 
  a = rnorm(3, 5), 
  b = rnorm(3, 6)
) %>% 
  mutate(delta = abs(a - b)) %>% 
  gather(`a`, `b`, `delta`, key = "letter", value = "value")


ggplot(delta %>% filter(letter != "delta"), aes(type, value, fill = letter)) +
  geom_col(position = "dodge") + 
  geom_col(data = delta %>% filter(letter == "delta"), width = 0.5) + 
  scale_fill_manual(values = c("grey", "black", "blue"))

Created on 2018-10-08 by the reprex package (v0.2.1)

Upvotes: 8

Related Questions