首页 > 解决方案 > 多个变量的 geom_point 大小

问题描述

我在网上找到了这个例子,因为我需要用物种相对丰度的大小来绘制 nmds 的点,类似于这里的丰富度。我的问题是:如何在一个图中绘制更多变量的点的大小?例如:sp1、sp2、sp3 的大小 非常感谢!

library(tidyverse)
library(vegan)df <- strsplit("Group   Estacion    Richness    Especie1    Especie2    Especie3    Especie4    Especie5    Especie6    Especie7
Agosto  E1  6   87  87  89  91  87  94  0
Agosto  E2  7   77  78  78  77  95  45  45
Agosto  E3  7   85  87  89  89  78  89  95
Agosto  E4  3   57  56  54  0   0   0   0
Diciembre   E1  6   77  78  78  77  95  45  0
Diciembre   E2  7   65  64  68  69  65  64  69
Diciembre   E3  7   74  71  75  75  76  81  75
Diciembre   E4  2   38  39  0   0   0   0   0
Abril   E1  7   81  82  79  82  78  79  82
Abril   E2  7   69  71  72  71  68  69  73
Abril   E3  7   74  79  78  75  79  75  76
Abril   E4  3   51  52  49  0   0   0   0", "\n") %>%
  unlist() %>%
  strsplit("\t") %>%
  unlist() %>%
  matrix(ncol = 10, byrow = TRUE) %>%
  {`colnames<-`(data.frame(.[-1,], stringsAsFactors = FALSE), .[1,])} %>%
  mutate_at(vars(-Group, -Estacion), as.numeric)

nmds <- metaMDS(select(df, starts_with("Especie")))

scores(nmds) %>%
  cbind(df) %>%
  ggplot(aes(x = NMDS1, y = NMDS2)) +
  geom_point(aes(size = Richness, color = Group)) +
  stat_ellipse(geom = "polygon", aes(group = Group, color = Group, fill = Group), alpha = 0.3) +
  annotate("text", x = -2, y = 0.95, label = paste0("stress: ", format(nmds$stress, digits = 4)), hjust = 0) +
  theme_bw()```


标签: rggplot2vegan

解决方案


我对您的问题不是很清楚,但我认为您要问的是如何绘制不同物种的大小以及丰富度。对此我会给出答案。

顺便说一句,您可以更轻松地使用data.table::fread. 如:

library(tidyverse)
library(vegan)
df <- data.table::fread("Group   Estacion    Richness    Especie1    Especie2    Especie3    Especie4    Especie5    Especie6    Especie7
Agosto  E1  6   87  87  89  91  87  94  0
Agosto  E2  7   77  78  78  77  95  45  45
Agosto  E3  7   85  87  89  89  78  89  95
Agosto  E4  3   57  56  54  0   0   0   0
Diciembre   E1  6   77  78  78  77  95  45  0
Diciembre   E2  7   65  64  68  69  65  64  69
Diciembre   E3  7   74  71  75  75  76  81  75
Diciembre   E4  2   38  39  0   0   0   0   0
Abril   E1  7   81  82  79  82  78  79  82
Abril   E2  7   69  71  72  71  68  69  73
Abril   E3  7   74  79  78  75  79  75  76
Abril   E4  3   51  52  49  0   0   0   0") 

您可以通过使用pivot.longer然后使用将所有数据放入同一个图中facet.wrap

nmds <- metaMDS(select(df, starts_with("Especie")))

scores(nmds) %>%
  cbind(df) %>% 
  pivot_longer(cols = -c(NMDS1,NMDS2,Group, Estacion),names_to = "Type", values_to = "Size") %>% 
  ggplot(aes(x = NMDS1, y = NMDS2)) +
  geom_point(aes(size = Size, color = Group)) +
  facet_wrap(~Type) +
  stat_ellipse(geom = "polygon", aes(group = Group, color = Group, fill = Group), alpha = 0.3) +
  annotate("text", x = -2, y = 0.95, label = paste0("stress: ", format(nmds$stress, digits = 4)), hjust = 0) +
  theme_bw()

例子


推荐阅读