Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Specific color for each boxplot in aes

I have a set of data that can be summarized as in the example below. Some values and two other columns that represent some categorical values.

library(ggplot2)
set.seed(123)
sdata=data.frame( aicat=sample(letters[1:4], 50, replace=TRUE), site=sample(letters[5:6], 50, replace=TRUE), vals=sample(1:1000, 50) )

head(sdata)
  aicat site vals
1     c    f  409
2     c    e  308
3     c    e  278
4     b    e   89
5     c    e  928
6     b    f  537

I want to plot this data with boxplot, where on x I have the aicat variable and where I also group the data by site.
To do that I did the following:

ggplot(sdata, aes(x=aicat, y=vals, fills=site, color=site))+
  geom_boxplot()+
  scale_fill_manual()

That produces the following image:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

The problem is the following:
how can I assign to each of these boxplots the following list of colors?

values=c("brown1", "brown4", "gold", "gold4", "dodgerblue", "dodgerblue4", "gray70", "gray40")

I would like that the boxplots in "a" are brown1 (the leftmost) and brown4 (the rightmost),
the one in b gold (the leftmost) and gold4 (the rightmost), and so on. I also need to keep this structure of the plot, so solutions with facet_wrap are not what I am looking for.

Any advice?

>Solution :

You can specify manually this colors in fill parameter out of the aes()

 geom_boxplot(fill = c("brown1", "brown4", "gold", "gold4", "dodgerblue", "dodgerblue4", "gray70", "gray40"))

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading