Search code examples
rggplot2vegan

geom_point size for multiple variables


i find this example online because I need to plot the points of the nmds with the size of species relative abundance, similar as it is done here with richness. My question is: How can I plot the size of the points of more variables in one plot? for example: size of sp1, sp2, sp3 Thanks a lot!

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()```



Solution

  • I'm not super clear on your question, but I think what you are asking is how you can plot the sizes of the different species as well as the richness. I'll give an answer to that.

    By the way, you can read in the data much easier using data.table::fread. As in:

    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") 
    

    You can get all of the data into the same figure by using pivot.longer and then using 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()
    

    example