1

I am using 'mpg' data set and ggplot2 package to create a bar plot, and it is required that the colors of bars are defined by the 'class' variable such that the color for the value '2seater' is red, for the value 'pickup' is green, and all others are white. Here is my code:

    mpg %>% ggplot(aes(class), fill = x) + geom_bar(width = 0.7, color = "black", size = 0.8) +
  scale_fill_manual(values = c("2seater" = "red",
                               "compact" = "white",
                               "midsize" = "white",
                               "minivan" = "white",
                               "pickup" = "green",
                               "subcompact" = "white",
                               "suv" = "white"))

The problem is, when I execute this code, the colors of bars remain as default. Could anyone tell me what goes wrong with my code? Thank you very much

  • 1
    Why are you setting ``fill = x`` if you want them to be filled by ``class`` variable? And fill should be inside the ``aes`` function call. – user438383 Jun 12 '22 at 12:40
  • Does this answer your question? [Change bar plot colour in geom\_bar with ggplot2 in r](https://stackoverflow.com/questions/38788357/change-bar-plot-colour-in-geom-bar-with-ggplot2-in-r) – harre Jun 12 '22 at 13:00

3 Answers3

3

As @user438383 advised in the comments, you need to specify the fill variable in the aes() call.

library(ggplot2)

mpg |> ggplot(aes(class, fill = class)) +
  geom_bar(width = 0.7) +
  scale_fill_manual(values = c("2seater" = "red",
                               "compact" = "white",
                               "midsize" = "white",
                               "minivan" = "white",
                               "pickup" = "green",
                               "subcompact" = "white",
                               "suv" = "white")) +
  theme(legend.position = "none")

Created on 2022-06-12 by the reprex package (v2.0.1)

Andrea M
  • 2,314
  • 1
  • 9
  • 27
2

Here is an alternative by creating a new my_color column and then applying to fill:

mpg1 <- mpg %>% 
  mutate(my_color = case_when(class == "2seater" ~ 'red',
                           class == "pickup"  ~ 'green', 
                           TRUE ~ 'white')) %>% 
  count(class, my_color) 

ggplot(mpg1, aes(x = class, y = n)) +
  geom_col(fill = mpg1$my_color, width = 0.7)

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66
1

If you have many categories, you might want to specify the color for a handful of categories and a default color for the rest.

It's also handy to keep argument settings (such as color specs in this case) separate from the code to generate plots.

library("tidyverse")

special_colors <- list(
  "2seater" = "red",
  "pickup" = "green"
)
default_color <- "white"

mpg %>%
  ggplot(aes(class, fill = class)) +
  geom_bar(width = 0.7) +
  scale_fill_manual(
    values = unlist(special_colors),
    na.value = default_color
  ) +
  theme(legend.position = "none")

Created on 2022-06-12 by the reprex package (v2.0.1)

dipetkov
  • 3,380
  • 1
  • 11
  • 19