R中CCA图的修改

时间:2016-08-24 14:36:23

标签: r ggplot2 vegan

如何根据“使用”因素着色或更改“物种”形状。

library(vegan)
data(dune, dune.env)
mod = cca(dune ~ A1 + Moisture + Manure,dune.env)
library(ggvegan)
autoplot(mod)

所以我想快速区分“使用”因素,将ordihull放在这些因素之间也是很好的。

2 个答案:

答案 0 :(得分:0)

您无法通过Use更改物种点数,因为这是一个样本级变量。如果您想更改网站积分,则需要使用fortify方法自行完成。

这将使你到目前为止:

fmod <- fortify(mod)
size <- 1.8
ggplot(fmod, aes(x = Dim1, y = Dim2)) +
  geom_point(data = subset(fmod, Score == "species"),
             aes(colour = "species"), size = size) +
  geom_point(data = cbind(subset(fmod, Score == "sites"), Use = dune.env$Use),
             aes(shape = Use, colour = "sites"), size = size) +
  scale_colour_brewer("Score", palette = "Set1") +
  coord_fixed() +
  theme(legend.position = "top")

这会让你:

enter image description here

您需要查看ggvegan:::autoplot.cca的代码,了解如何处理和绘制双标图箭头和质心。

答案 1 :(得分:0)

下面的最终答案,感谢Gavin的协助;

   fmod <- fortify(mod)
    size <- 1.8
    ggplot(fmod, aes(x = Dim1, y = Dim2)) +
      geom_point(data = subset(fmod, Score == "species"),
                 aes(colour = "species"), size = size) +
      geom_point(data = cbind(subset(fmod, Score == "sites"), Use = dune.env$Use),
                 aes(shape = Use, colour = "sites"), size = size) +
      scale_colour_brewer("Score", palette = "Set1") +
      coord_fixed() +
      theme(legend.position = "top")+
      geom_text(data = subset(fmod, Score == "biplot"), 
              aes(x=Dim1,y=Dim2,label=Label), size=2) + xlab("CCA1") + ylab("CCA2") +
      geom_segment(data = subset(fmod, Score == "biplot"),
                   aes(x = 0, y = 0, xend = Dim1, yend = Dim2), arrow = arrow(length = unit(1/2, 'picas')), colour = "red")