Can't change the color of Bar chart in R


barchart <- ggplot(data=penguins)+
  geom_bar(mapping=aes(species))

barchart + scale_fill_manual(values = c("Adelie"="purple","Chinstrap"="orange", "Gentoo"="green"))

Hi, I am new to R, and programming in general, I am trying to designate a specific color to my bar chart, so I wrote.

But the bars are still grey in the plot, would like to know why and thanks for everyone helped.

>Solution :

You have to tell ggpplot2 that you want your bars to be filled by species by mapping on the fill aesthetic. Additionally, as the fill legend is redundant I removed it using guides(fill = "none"):

library(palmerpenguins)
library(ggplot2)

ggplot(data = penguins) +
  geom_bar(aes(species, fill = species)) +
  scale_fill_manual(
    values = c("Adelie" = "purple", "Chinstrap" = "orange", "Gentoo" = "green")
  ) +
  guides(fill = "none")

Leave a Reply