【问题标题】:geom_point size for multiple variables多个变量的 geom_point 大小
【发布时间】:2021-10-04 07:59:47
【问题描述】:

我在网上找到了这个例子,因为我需要用物种相对丰度的大小来绘制 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()```


【问题讨论】:

    标签: r ggplot2 vegan


    【解决方案1】:

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

    顺便说一句,您可以使用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()
    

    【讨论】:

    • 嗨,丹,代码运行良好,但我的想法是在一个情节中拥有所有物种的点大小,这可能吗? scores(nmds) %&gt;% cbind(df) %&gt;% ggplot(aes(x = NMDS1, y = NMDS2)) + geom_point(aes(size = Richness, color = Group)) + geom_point(aes(size= Especies1, color=pink) + geom_point(aes(size=Especies2, color= red) stat_ellipse(geom = "polygon", aes(group = Group, color = Group, fill = Group), alpha = 0.3) + theme_bw()
    • 这将很难看到不同的点,因为它们会相互重叠。一种解决方案是使用不同的点字符,如下所示:scores(nmds) %&gt;% cbind(df) %&gt;% pivot_longer(cols = -c(NMDS1,NMDS2,Group, Estacion),names_to = "Type", values_to = "Size") %&gt;% ggplot(aes(x = NMDS1, y = NMDS2)) + stat_ellipse(geom = "polygon", aes(group = Group, fill = Group), alpha = 0.3) + geom_point(aes(size = Size, color = Type), pch = 1) + annotate("text", x = -2, y = 0.95, label = paste0("stress: ", format(nmds$stress, digits = 4)), hjust = 0) + theme_bw()
    猜你喜欢
    • 2015-11-19
    • 1970-01-01
    • 2022-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多