I'm fairly new to R and ggplot.
I'm trying to generate a boxplot sorted by two variables. In my case Species and Experiment. What I got so far by using
ggplot(DF, aes(Species, Protein, fill=Experiment, dodge=Experiment)) +
stat_boxplot(geom ='errorbar')+
geom_boxplot()
are boxplots of my species and each species has 2 bars, one for each experiment.
My question is now, is it possible to change the colours in this way, that I have different colours per species and lets say, different shading of those colours for the experiments?
Lets say, the first species would than have a dark and light blue bar, the second a dark and light green bar, etc.
The data is in the form of a csv file and looks like this:
Experiment,Species,Protein
Spring,D_strigosa,5.107767364
Spring,D_strigosa,8.288980741
Spring,D_strigosa,7.537376567
Spring,D_strigosa,4.811744241
Spring,D_strigosa,9.559043454
Spring,M_cavernosa,13.66759289
Spring,M_cavernosa,23.54880195
Spring,M_cavernosa,11.00790037
Spring,M_cavernosa,13.70597973
Spring,M_cavernosa,12.26970906
Spring,M_faveolata,9.559043454
Spring,M_faveolata,4.135860474
Spring,M_faveolata,4.557306615
Spring,M_faveolata,7.621872315
Spring,M_faveolata,6.016859956
Spring,M_annularis,6.294920733
Spring,M_annularis,5.163371831
Spring,M_annularis,2.177243143
Spring,M_annularis,3.55966551
Autumn,D_strigosa,3.709978392
Autumn,D_strigosa,7.192777836
Autumn,D_strigosa,5.31544441
Autumn,D_strigosa,6.578602058
Autumn,D_strigosa,5.610085559
Autumn,M_cavernosa,11.64182554
Autumn,M_cavernosa,10.44968102
Autumn,M_cavernosa,9.377389318
Autumn,M_cavernosa,10.99346107
Autumn,M_cavernosa,11.5676896
Autumn,M_faveolata,4.638054165
Autumn,M_faveolata,7.191664953
Autumn,M_faveolata,6.981827102
Autumn,M_faveolata,6.386452477
Autumn,M_annularis,4.709770404
Autumn,M_annularis,3.554071459
Autumn,M_annularis,4.596686351
Autumn,M_annularis,3.530585628
You should use interaction()
for the fill=
and provide both Species
and Experiment
inside interaction()
. Then with scale_fill_manual()
you can set values=
for the colors you need (number of colors correspond to number of box-plots). Order of colors is, first, colors for Species
in Autumn
then colors for Species
in Spring
.
ggplot(DF, aes(Species, Protein, fill=interaction(Species,Experiment), dodge=Experiment)) +
stat_boxplot(geom ='errorbar')+
geom_boxplot()+
scale_fill_manual(values=c("blue","green","red","cyan",
+ "darkblue","darkgreen","darkred","darkcyan"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With