Natalia Resende
Natalia Resende

Reputation: 195

How to customise the colour of ggplot2 boxplots?

I want to use fill the boxplots I've created with customised colour. For example, I want them filled with a colour with the following specifications: red=116, blue=49, green=0, Hue=223, sat=240,lum=55. My code is below, but I have no idea how to change the colour of the fill for the colour I want

I've tried to use the scale_fill_manual() but the graphs displayed have no colour.

v1 = ggplot(data, aes(x=SYSTEMS, y=ADEQUACY)) + geom_boxplot() + coord_cartesian(ylim = c(1, 3))


v1 + scale_fill_manual(values = c('red','blue'))

My expected result is to print boxplots filled with the colour with the specified values. How to do that?

Upvotes: 0

Views: 179

Answers (1)

M. Schumacher
M. Schumacher

Reputation: 150

scale_fill_manual() does not work because you did not specify any fill aesthetic.

This should work:

ggplot(mtcars, aes(x = cyl, y = wt, group = cyl, fill = as.factor(cyl))) +
  geom_boxplot() +
  scale_fill_manual(values = c("red", "blue", "grey"))

However, you can also specify your fill colors inside the geom_boxplot() call:

ggplot(mtcars, aes(x = cyl, y = wt, group = cyl)) +
  geom_boxplot(fill = c("red", "blue", "grey"))

Upvotes: 1

Related Questions