Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading