【问题标题】:R Visualize cca plot in ggplot - Wrong arrows lengthR在ggplot中可视化cca图 - 箭头长度错误
【发布时间】:2015-08-13 11:50:49
【问题描述】:

我正在尝试自定义在 Vegan R 包中执行的 CCA 图。我使用了底栖覆盖矩阵 (%) 与鱼类丰度矩阵(个体数量)来运行此分析。我在 2 年内收集了 4 个珊瑚礁的数据。我想让网站点以不同的颜色(对于珊瑚礁地点)和形状(年份),鱼类作为文字,底栖动物作为箭头。我几乎已经在 ggplot 中手动完成了,但是箭头长度似乎是错误的。将我的情节与“autoplot”和“plot.cca”生成的情节进行比较,我的箭头看起来太短了。我的问题是如何解决这个问题?

请从此链接复制“benthos”和“fish”数据框:http://pedromeirelles.com.br/blog)。这些数字(ggvegan、cca.plot 和 ggplot)也可以在这个链接中找到。我还不能在这里发布图片,而且数据框太大了。很抱歉给您带来不便。

非常感谢您。

在我用来绘制绘图的脚本下方。

ggplot 手动绘制

library("ggplot2")
library("vegan")
library("grid")
library("ggvegan")

benthos <- please copy this dataframe from the link above
fish <- please copy this dataframe from the link above
attach(fish)
fish.num<-fish[,3:ncol(fish)]
benthos.num <-benthos[,3:ncol(benthos)] 

mod <- cca(fish.num, benthos.num)

cca.res<-summary(mod)
cca.sites <-data.frame(cca.res$sites)
ord_df<-data.frame(Site=Site,Year=Year,CCA1=cca.sites$CCA1,CCA2=cca.sites$CCA2)
ord_df$Year <- factor(ord_df$Year)
ord_df$Site <- factor(ord_df$Site)
exp<-cca.res$concont
exp<-data.frame(exp$importance)
cca.species<-data.frame(cca.res$species)
cca.species<-data.frame(Cca1=cca.species$CCA1,Cca2=cca.species$CCA2,species=rownames(cca.species))
cca.benthos<-data.frame(cca.res$biplot)
cca.benthos<-data.frame(cca1=cca.benthos$CCA1,cca2=cca.benthos$CCA2, Species = rownames(cca.benthos))

ggplot(ord_df) +
  geom_point(mapping = aes(x=CCA1, y=CCA2, color=Site, shape=Year),size = 4)+
  geom_text(data = cca.species, 
        aes(x = Cca1, y = Cca2, label = species),
        size = 3,alpha=0.4,colour = "darkred") +
  geom_segment(data = cca.benthos,
           aes(x = 0, xend = cca1, y = 0, yend = cca2),
           arrow = arrow(length = unit(0.5, "cm")), size=1, colour = "grey") +
  geom_text(data = cca.benthos, 
        aes(x = cca1*1.5, y = cca2*1.5, label = Species),
        size = 5) +
  labs(list(title = NULL, x = paste("CCA1 (",round(exp[2,1]*100,digits=2),"%)"), y = paste("CCA2 (",round(exp[2,2]*100,digits=2),"%)"))) +
  geom_hline(yintercept = 0, colour = "gray70") +
  geom_vline(xintercept = 0, colour = "gray70") +
  scale_colour_manual(values=c("#43CD80","#9400D3","steelblue3","#DEB887"))+
  theme_bw()+
  theme(panel.border = element_rect(colour = "white", size=1))+
  theme(legend.key=element_rect(fill='white'))+
  theme(legend.key = element_rect(colour = "white"))+

  theme(axis.title.x = element_text(face="bold", size=14))+
  theme(axis.title.y = element_text(face="bold", size=14))+
  theme(axis.text.x = element_text(size=14,color="black"))+
  theme(axis.text.y = element_text(size=14,color="black"))+
  theme(axis.line.x=element_blank())+
  theme(axis.line.y=element_blank())+
  theme(
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank())

ggvegen 自动绘图

autoplot(mod)

plot.cca

plot(mod, type="n")
text(mod, dis="bp")
points(mod, display="sites",pch=21, col="red", bg="yellow", cex=1.2)
text(mod, "species", col="blue", cex=0.8)
text(mod, display = "sites", cex=0.7, col="red")

我找不到数据绘图输入的差异

fdat <- fortify(mod)
fdat
cca.benthos
vegan::scores(mod, display = "bp")

【问题讨论】:

    标签: r ggplot2 vegan


    【解决方案1】:

    您错过了plot.cca()autoplot.cca() 缩放箭头以适应绘图窗口的步骤。这是在plot.cca() 中完成的,并且在ggvegan 0.0-3 之前的autoplot.cca() 中通过实用函数ordiArrowMul() 完成。

    vegan 版本 2.3-0 开始,此函数是从 vegan 导出的。但它使用来自基本图形的信息来进行缩放。因此,从 ggvegan 的 0.0-3 版开始,有一个 non-exported function ggvegan:::arrowMul() 将与 ordiArrowMul() 完成相同的工作,但从数据来看不是当前的绘图限制。 (在此功能的未来版本中,如果您更改基础图中的 xlimylim,我计划允许您设置图的限制。)

    你应该可以做到

    mul <- ggvegan:::arrowMul(cca.res$biplot,
                              rbind(cca.sites, cca.species))
    cca.benthos <- data.frame(cca.res$biplot * mul)
    

    (未经测试,我即将出发 - 如果不起作用,请发表评论,稍后我会更详细地查看。)

    【讨论】:

    • 非常感谢@GavinSimpson 教授!我可以对底栖动物和鱼类数据执行此操作,并且情节得到了很大改善。
    猜你喜欢
    • 2016-09-26
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    • 2011-03-12
    • 2015-05-13
    • 1970-01-01
    • 1970-01-01
    • 2014-06-19
    相关资源
    最近更新 更多