I'm trying to change the color of the individual bars of my ggplot
using scale_fill_manual
, but am having trouble because the bars are within group (here "condition"). How can I overcome this issue?
Here is my dataset:
df <- data.frame(
"condition" = c("A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B"),
"type" = c("Apple", "Apple", "Apple", "Apple", "Pear", "Pear", "Pear", "Pear", "Orange", "Orange", "Orange", "Orange", "Tomato", "Tomato", "Tomato", "Tomato", "Tomato", "Berry", "Berry", "Berry"),
"ripeness" = c("bad","bad", "good", "good", "bad", "bad", "good", "good", "bad","bad", "good", "good", "bad", "bad", "good", "good", "bad","bad", "good", "good"),
count = c(19, 4, 7, 2, 7, 7, 1, 13, 16, 16, 31, 13, 30, 12, 39, 17, 1, 36, 4, 27)
)
and the code that I am using to plot it:
plot <- ggplot(df, aes(x=type, y=count, fill=ripeness)) +
geom_bar(stat="summary", width = .7, position = position_dodge(width = .75)) +
theme_light()
#the below is non-working code
plot + scale_fill_manual(values=c("black","blue","black","green","black","stripes-2","black","stripes-3","black", "yellow"))
How can I specify the colors of the individual bars? For explanatory purposes, I wrote in color names but will swap these out for html color codes. Later, I am also aiming to texture two of my bars using ggpattern (https://coolbutuseless.github.io/package/ggpattern/articles/geom-gallery-geometry.html#colour-example-1) but that doesn't need to be solved in this post, if too difficult.
I am trying to recreate this plot in r: