Change Color of ggplot2 Boxplot in R (3 Examples) | Set col & fill in Plot | Manually Specify Colors

preview_player
Показать описание
R code of this video:

group = letters[1:5])

library("ggplot2")

ggplot(data, aes(x = group, y = value)) + # ggplot2 boxplot without colors
geom_boxplot()

ggplot(data, aes(x = group, y = value, col = group)) + # Change color of borders
geom_boxplot()

ggplot(data, aes(x = group, y = value, fill = group)) + # Change filling color
geom_boxplot()

ggplot(data, aes(x = group, y = value, fill = group)) + # Manually specified filling color
geom_boxplot() +
scale_fill_manual(breaks = data$group,
values = c("#1b98e0", "#353436", "yellow", "red", "green"))

Follow me on Social Media:
Рекомендации по теме
Комментарии
Автор

Nice - I like the approach of using a) multiple ways of generating the same result and/or b) showing the incredible flexibility of packages like ggplot2 (which is virtually limitless).

haraldurkarlsson
Автор

I get an error when trying to do that last example, stating "scale for 'fill' is already present. This is what it looks like:

```
ggplot(data = TOOTH, mapping = aes(x = dose, y = len)) +
geom_boxplot() +
xlab("Dosage") +
ylab("Length") +
labs(title = "Supplement Dosage on Teeth") +
scale_fill_discrete(name = "Supplement") +
scale_fill_manual(breaks = TOOTH$supp, values = "#990000"))

```
any ideas?

abigailspencer
Автор

Can you please help me to use the new colour by the rgb code or hex code which is not available in default R environment

abhilashpatel
Автор

Great! but it seems that your manually filling does not work in my r, it only changes the color of the first box. Any idea of what is wrong?

roxirisauxiliadora
Автор

Great video as usual.
I have one question! How can we omit legend in the plot?

newcomer
join shbcf.ru