ggplot2

时间:2019-07-29 08:59:08

标签: r ggplot2 boxplot

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

我想为其中一个社区提供一种模式,但是我无法在ggplot2中找到实现此目的的方法(下图在导出后进行了处理)。我设法使用一种Alpha颜色来区分这两个社区,尽管这在图表上看起来不错,但您在图例中看不到两者之间的差异。

我想要实现的情节

Plot I want to achieve:

我的数据:

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

1 个答案:

答案 0 :(得分:0)

据我所知,在本机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"))

enter image description here

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