r - 多个变量的 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()```
解决方案
我对您的问题不是很清楚,但我认为您要问的是如何绘制不同物种的大小以及丰富度。对此我会给出答案。
顺便说一句,您可以更轻松地使用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()
推荐阅读
- javascript - 实现 Vue 可拖动
- c# - 如何在 .net 核心类库中使用 Automapper?
- pandas - 神经网络:TypeError:fitness() 得到了一个意外的关键字参数“嵌入”
- ios - 当用户点击 iOS 本地通知上的查看按钮时,我将如何设置视图?
- python - 如何使用日期时间 x 变量设置多个 x 轴
- python - 我无法在 mac 中安装 PyGMO
- mongodb - 如何使用 libcurl4 安装 mongod?
- c# - 如何将带有值的视图类传递给控制器并添加到列表中?
- rust - 使用错误枚举变体时“无法推断 `T` 的类型”
- javascript - DataTables 将源数组包含在 JSON“数据”属性中