简体   繁体   English

DCA:使用自动绘图或 ggplot2 标记点

[英]DCA : Labelling points with autoplot or ggplot2

I find very difficult to put labels for sites with a DCA in a autoplot or ggplot.我发现很难在 autoplot 或 ggplot 中为带有 DCA 的站点添加标签。
I also want to differentiate the points on the autoplot/ggplot according to their groups.我还想根据它们的组来区分 autoplot/ggplot 上的点。
This is the data and the code I used and it went well until the command for autoplot/ggplot:这是我使用的数据和代码,它运行良好,直到自动绘图/ggplot 的命令:

library(vegan)
data(dune)
d <- vegdist(dune)
csin <- hclust(d, method = "single")
cl <- cutree(csin, 3)
dune.dca <- decorana(dune)
autoplot(dune.dca)

This is the autoplot obtained:这是获得的自动绘图:

dca_autoplot

I am using simple coding and I tried these codes but they didn't led me anywhere:我正在使用简单的编码,我尝试了这些代码,但它们并没有引导我到任何地方:

autoplot(dune.dca, label.size = 3, data = dune, colour = cl)
ggplot(dune.dca(x=DCA1, y=DCA2,colour=cl))
ggplot(dune.dca, display = ‘site’, pch = 16, col = cl)
ggrepel::geom_text_repel(aes(dune.dca))

If anyone has a simple suggestion, it could be great.如果有人有一个简单的建议,那就太好了。

With the added information (package) I was able to go and dig a bit deeper.通过添加的信息(包),我能够 go 并深入挖掘。

The problem is (in short) that autoplot.decorana adds the data to the specific layer (either geom_point or geom_text ).问题是(简而言之) autoplot.decorana将数据添加到特定图层( geom_pointgeom_text )。 This is not inherited to other layers, so adding additional layers results in blank pages.这不会继承到其他层,因此添加其他层会导致空白页。

Basically notice that one of the 2 code strings below results in an error, and note the position of the data argument:基本上注意到下面的 2 个代码字符串之一会导致错误,并注意data参数的 position:

# Error: 
ggplot() + 
  geom_point(data = mtcars, mapping = aes_string(x = 'hp', y = 'mpg')) +
  geom_label(aes(x = hp, y = mpg, label = cyl))
# Work:
ggplot(data = mtcars) + 
  geom_point(mapping = aes_string(x = 'hp', y = 'mpg')) +
  geom_label(aes(x = hp, y = mpg, label = cyl))

ggvegan:::autoplot.decorana places data as in the example the returns an error. ggvegan:::autoplot.decorana放置数据,如示例中返回错误。

I see 2 ways to get around this problem:我看到了解决这个问题的两种方法:

  1. Extract the layers data using ggplot_build or layer_data and create an overall or single layer mapping.使用ggplot_buildlayer_data提取图层数据并创建整体或单层映射。
  2. Extract the code for generating the data, and create our plot manually (not using autoplot ).提取生成数据的代码,并手动创建我们的 plot (不使用autoplot )。

I honestly think the second is simpler, as we might have to extract more information to make our data sensible.老实说,我认为第二个更简单,因为我们可能必须提取更多信息才能使我们的数据变得合理。 By looking at the source code of ggvegan:::autoplot.decorana (simply printing it to console by leaving out brackets) we can extract the below code which generates the same data as used in the plot通过查看ggvegan:::autoplot.decorana的源代码(只需省略括号将其打印到控制台),我们可以提取以下代码,该代码生成的数据与 plot 中使用的数据相同

ggvegan_data <- function(object, axes = c(1, 2), layers = c("species", "sites"), ...){
  obj <- fortify(object, axes = axes, ...)
  obj <- obj[obj$Score %in% layers, , drop = FALSE]
  want <- obj$Score %in% c("species", "sites")
  obj[want, , drop = FALSE]
}

With this we can then generate any plot that we desire, with appropriate mappings rather than layer-individual mappings有了这个,我们可以生成我们想要的任何 plot,使用适当的映射而不是层单独的映射

dune.plot.data <- ggvegan_data(dune.dca)
p <- ggplot(data = dune.dca, aes(x = DCA1, DCA2, colour = Score)) + 
  geom_point() + 
  geom_text(aes(label = Label), nudge_y = 0.3)
p

Which gives us what I hope is your desired output这给了我们我希望是你想要的 output 点+标签图(来自上面的代码)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM