ggplot2 箱线图的两种不同 colour/pattern 方案

Two different colour/pattern schemes for boxplots with ggplot2

我正在尝试将我的地块着色为下图,使用横断面位置以及两个不同社区之间的配色方案:所有物种和没有 Clidemia hirta

我想要一个社区的模式,但我想不出在 ggplot2 中执行此操作的方法(下图是在导出后进行处理的)。我已经设法应用 alpha 颜色来区分这两个社区,虽然这在图表上看起来不错,但您在图例中看不到两者之间的区别。

我要实现的情节

我的数据:

    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"))

我的代码:

    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))

据我所知,在原生 ggplot.

中无法做到这一点

如果你真的非常想要模式(示例 here),你可以稍微破解它。

你也可以玩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"))

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)

这应该可行,您的绘图代码很好,但只需要稍作调整即可包含模式代码。所以我添加了一行 geom_boxplot_pattern 并删除了你的 scale_alpha_manual

我用pattern = Community

替换了alpha = factor(Community)

带模式代码的箱线图:

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

如果您还想为“Without Clidemia hirta”指定一个模式,您需要包含一行 scale_pattern_manual 但很抱歉我无法让它工作。这可能是由于名称“Without Clidemia hirta”中的空格,但它会是这样的:

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

我真的希望这会有所帮助,我一直在努力寻找具有手动模式和手动颜色的箱线图以便轻松复制和粘贴,所以我希望这对我以前的职位的其他人来说也能达到这个目的!