0

Here's some reproducible code for making a barplot. I would like to customize the color of bars C, M, S and S as varying shades of blue and V as orange. How can I do that? Thank you!

IDs <- seq(1,50)
IDs <- data.frame(rep(IDs, each = 5))
names(IDs)[1] <- "ID"

tastes <- c("Strawberry", "Vanilla", "Chocolate", "Matcha", "Sesame")
tastes <- data.frame(rep(tastes, times = 50))

#random numbers for schools 
A <- runif(250, 1,5)
B <- runif(250, 1,5)
C <- runif(250, 1,5)

#merge
test <- cbind(IDs, tastes)
test <- cbind(test, A)
test <- cbind(test, B)
test <- cbind(test, C)
names(test)[2] <- "Flavour"
#make long
test_long <- melt(test, 
                  id.vars = c("ID", "Flavour"))

#plot
plot <- ggplot(test_long) +
  geom_bar(aes(x = Flavour,
               y = value), stat="summary", fun=mean) + 
  scale_x_discrete(labels=c("C","M","S","S","V")) +
  coord_cartesian(ylim=c(1,5)) +
  facet_grid(. ~ variable) + 
  labs(title = "Likeability of Different Flavours by School") +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"))
plot
jo_
  • 677
  • 2
  • 11
  • 4
    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) – Captain Hat Apr 15 '21 at 21:57
  • Hi @CaptainHat, the scale_fill_manual function doesn't map on exactly to my data. I get this error: `Aesthetics must be either length 1 or the same as the data (24): fill`. Do you know how I may work around that? – jo_ Apr 15 '21 at 22:08
  • 2
    You need to specify `fill = 'tastes'` within `aes()`. That's explained in the question I linked above - this question is a duplicate. – Captain Hat Apr 15 '21 at 22:13
  • I got it! Thank you! – jo_ Apr 15 '21 at 22:35

0 Answers0