Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two different colour/pattern schemes for boxplots with ggplot2

Tags:

r

ggplot2

boxplot

I am trying to colour my plot to be the image below with a colour scheme across Transect locations but also between the two different Communities: All species and Without Clidemia hirta.

I would like a pattern for one of the communities, but I can't figure out a way to do this in ggplot2 (the image below was manipulated after exporting). I have managed to apply an alpha colour to differentiate the two communities, and although this looked good on the graph, you could not see a difference between the two in the legend.

Plot I want to achieve

Plot I want to achieve:

My data:

    Transect.location = c("Forest disturbed", "Forest-oil palm edge", "Forest less disturbed", "Oil palm", "Forest disturbed", "Forest-oil palm edge", "Forest less disturbed", "Oil palm", "Forest disturbed", "Forest-oil palm edge", "Forest less disturbed", "Oil palm", "Forest disturbed", "Forest-oil palm edge", "Forest less disturbed", "Oil palm", "Forest disturbed", "Forest-oil palm edge", "Oil palm", "Forest disturbed", "Forest-oil palm edge", "Forest less disturbed", "Oil palm", "Forest disturbed", "Forest-oil palm edge", "Forest less disturbed", "Oil palm", "Forest disturbed", "Forest-oil palm edge", "Forest disturbed", "Forest disturbed", "Forest-oil palm edge", "Forest-oil palm edge", "Forest-oil palm edge", "Oil palm", "Oil palm", "Forest disturbed", "Oil palm", "Forest-oil palm edge", "Oil palm", "Forest-oil palm edge", "Oil palm", "Oil palm", "Forest-oil palm edge", "Forest disturbed", "Forest-oil palm edge", "Forest-oil palm edge", "Oil palm", "Forest-oil palm edge", "Oil palm", "Oil palm", "Forest disturbed", "Forest-oil palm edge", "Forest-oil palm edge", "Oil palm", "Oil palm", "Forest disturbed", "Oil palm", "Forest-oil palm edge", "Forest disturbed")
    woodiness = c(1, 0.605128205, 1, 0.230538922, 1, 0.891891892, 1, 0.169014085, 1, 0.417624521, 1, 0.234513274, 1, 0.317073171, 1, 0.597484277, 0.695238095, 0.236151603, 0.064516129, 1, 0.667655786, 1, 0.285714286, 1, 0.96, 1, 0.974025974, 0.732142857, 0.293929712, 1, 0.346153846, 0.127659574, 0.613793103, 0.2265625, 0.210045662, 0.025, 0.196581197, 0.254385965, 0, 0.05952381, 0.330434783, 0.051660517, 0.056179775, 0.126760563, 1, 0.571428571, 0, 0, 0.126213592, 0.116666667, 0.015384615, 0.53968254, 0.733333333, 0.417085427, 0.092307692, 0.041666667, 0.482758621, 0.018181818, 0.169172932, 0)
    Community = c("All species", "All species", "All species", "All species", "All species", "All species", "All species", "All species", "All species", "All species", "All species", "All species", "All species", "All species", "All species", "All species", "All species", "All species", "All species", "All species", "All species", "All species", "All species", "All species", "All species", "All species", "All species", "All species", "All species", "Without Clidemia hirta", "Without Clidemia hirta", "Without Clidemia hirta", "Without Clidemia hirta", "Without Clidemia hirta", "Without Clidemia hirta", "Without Clidemia hirta", "Without Clidemia hirta", "Without Clidemia hirta", "Without Clidemia hirta", "Without Clidemia hirta", "Without Clidemia hirta", "Without Clidemia hirta", "Without Clidemia hirta", "Without Clidemia hirta", "Without Clidemia hirta", "Without Clidemia hirta", "Without Clidemia hirta", "Without Clidemia hirta", "Without Clidemia hirta", "Without Clidemia hirta", "Without Clidemia hirta", "Without Clidemia hirta", "Without Clidemia hirta", "Without Clidemia hirta", "Without Clidemia hirta", "Without Clidemia hirta", "Without Clidemia hirta", "Without Clidemia hirta", "Without Clidemia hirta", "Without Clidemia hirta")

    data = data.frame(Transect.location, woodiness, Community)
    data$Transect.location<-factor(data$Transect.location, levels=c("Oil palm", "Forest-oil palm edge", "Forest disturbed", "Forest less disturbed"))

My code:

    ggplot(data, aes(x = Transect.location, y = woodiness, alpha = factor(Community), fill = factor(Transect.location))) + 
    geom_boxplot(aes(fill = Transect.location))+
    scale_fill_manual(name = "Transect.location", values = c("#FDECCD", "#BAE4B3", "#329A55", "#075507"))+
    scale_alpha_manual(name = "Community", values = c(1, 0.5))
like image 825
Emily Waddell Avatar asked Jun 02 '26 01:06

Emily Waddell


2 Answers

This should work, the code for your plot was good but it just needed a little tweaking to include code for the pattern. So I included a line with geom_boxplot_pattern and removed your scale_alpha_manual

I replaced alpha = factor(Community) with pattern = Community

Boxplot with pattern code:

library(ggplot2)
remotes::install_github("coolbutuseless/ggpattern")
library(ggpattern)

    myplot <-
ggplot(data, aes(x = Transect.location, y = woodiness, pattern = Community, fill = Transect.location)) +
geom_boxplot(aes(fill = Transect.location))+
scale_fill_manual(name = "Transect.location", values = c("#FDECCD", "#BAE4B3", "#329A55", "#075507")) +
geom_boxplot_pattern(position = position_dodge(preserve = "single"), color = "black", pattern_fill = "white", pattern_angle = 45, pattern_density = 0.1, pattern_spacing = 0.025, pattern_key_scale_factor = 0.6) +
guides(pattern = guide_legend(override.aes = list(fill = "white")), fill = guide_legend(override.aes = list(pattern = "none")))

myplot

Here is the picture of the plot

If you also want to specify a pattern just for "Without Clidemia hirta" you need to include a line with scale_pattern_manual but I'm sorry I couldn't get this to work. It might be due to the spaces in the name "Without Clidemia hirta" but it would be something like this:

    scale_pattern_manual(values = c(All_species = "stripe", Without_Clidemia_hirta = "none"))

I really hope this helps, I've struggled to find boxplots with manual patterns and manual colours to easily copy + paste so I hope this fulfils that purpose for others in my old position!

like image 157
MariaOuvarova Avatar answered Jun 05 '26 00:06

MariaOuvarova


As far as I'm aware, there's no way to do this within native ggplot.

You can somewhat hack it if you really, really want patterns (examples here).

You can also play with the linetype.

ggplot(data, aes(x = Transect.location, y = woodiness, linetype = factor(Community), fill = factor(Transect.location))) + 
  geom_boxplot(aes(fill = Transect.location))+
  scale_fill_manual(name = "Transect.location", values = c("#FDECCD", "#BAE4B3", "#329A55", "#075507"))+
  scale_alpha_manual(name = "Community", values = c(1, 0.5)) +
  scale_linetype_manual(name = "Community", values = c("solid", "dotted"))

enter image description here

Or facet_grid.

ggplot(data, aes(x = Transect.location, y = woodiness, fill = factor(Transect.location))) + 
  geom_boxplot(aes(fill = Transect.location))+
  scale_fill_manual(name = "Transect.location", values = c("#FDECCD", "#BAE4B3", "#329A55", "#075507"))+
  scale_alpha_manual(name = "Community", values = c(1, 0.5)) +
  facet_grid(~Community)

enter image description here

like image 30
Arienrhod Avatar answered Jun 05 '26 01:06

Arienrhod



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!