0

Using ggplot2 I want to plot a barplot using colors included in the column of that dataframe

Reproducible example below:

df<-cbind.data.frame("bar_colors"=c("red","blue","green"), "Counts"=c(10,20,30), "Spp"=c("a","a","a"))

ggplot(data=df, aes(x=Spp,y=as.numeric(as.character(Counts)), fill=bar_colors)) +
  geom_bar(stat="identity")+scale_fill_manual(values = as.character(df$bar_colors))

But the colors are mixed-up. What do I miss?

Thanks

EDIT: Solved myself by releveling the factor:

df$bar_colors<-factor(df$bar_colors, levels = as.character(df$bar_colors))
Grant
  • 45
  • 6
  • 3
    use `+scale_fill_identity` – tjebo Feb 04 '20 at 14:20
  • Does this answer your question? [How do I get ggplot to read the colors I am feeding it](https://stackoverflow.com/questions/34801653/how-do-i-get-ggplot-to-read-the-colors-i-am-feeding-it) – tjebo Feb 04 '20 at 14:22
  • thanks, already solved myself – Grant Feb 04 '20 at 14:25
  • suggest delete the question. I'd also recommend against using this approach using `$`- because of the unexpected results. better use either scale_identity or a named vector – tjebo Feb 04 '20 at 14:25

0 Answers0