有什么方法可以使用具有不同列数和行数的两个不同数据集在 ggplot 中绘制标签?
Is there any way to plot labels in ggplot using two different data sets with different number of columns and rows?
我正在尝试使用距离数据的标签 (spp) 绘制 metaMDS。
dat<-data.frame(
"site" = c("a", "a", "a",'a', "b", "b", "b", 'b', "c", "c", "c",'c'),
"sample"=c(rep(1,4),rep(2,4),rep(3,4)),
"sp1"=c(1,3,3,2,4,2,1,5,3,6,1,5),
"sp2"=c(1,3,3,2,4,2,1,5,3,6,1,5),
"sp3"=c(1,3,3,2,4,2,1,5,3,6,1,5),
"sp4"=c(2,3,4,1,5,3,1,5,5,8,9,1),
"sp5"=c(3,4,3,1,6,7,5,8,3,1,3,2)
)
我试过了
```
dat1<-dat[,3:7]
dmds<-metaMDS(dat1, distance = "bray", autotransform = FALSE)
mds1 <- dmds$points[,1]
mds2 <- dmds$points[,2]
plt<-cbind(dat,mds1,mds2)
spp<-names(dat1)
ggplot(plt,aes(mds1,mds2, shape=site,color=site))+
geom_point()+
geom_text(aes(label=spp), size=3)
```
但是,得到一个错误
错误:美学必须为长度 1 或与数据相同 (12):标签
因为dat
的行数和"spp<-names(dat1)"的行数不同
有什么方法可以解决这个错误吗?
I would like to have something like this, but with the correct spp number
作为进一步的潜在方向 - 我认为你的意思是每个站点都有一组单独的物种,而不是每个站点标记的五个 (sp1:sp5) 相同物种?这是一个可能看起来像的工作示例,传递三个数据框以允许代表三个不同物种集的不同列:
library(tidyverse)
library(vegan)
dat_a<-data.frame(
"site" = "a",
sample = c(1:4),
"sp1_a"=c(1,3,3,2),
"sp2_a"=c(1,3,3,2),
"sp3_a"=c(1,3,3,2),
"sp4_a"=c(2,3,4,1),
"sp5_a"=c(3,4,3,1)
)
dat_b<-data.frame(
"site" = "b",
sample = c(1:4),
"sp1_b"=c(4,2,1,5),
"sp2_b"=c(4,2,1,5),
"sp3_b"=c(4,2,1,5),
"sp4_b"=c(5,3,1,5),
"sp5_b"=c(6,7,5,8)
)
dat_c<-data.frame(
"site" = "c",
sample = c(1:4),
"sp1_c"=c(3,6,1,5),
"sp2_c"=c(3,6,1,5),
"sp3_c"=c(3,6,1,5),
"sp4_c"=c(5,8,9,1),
"sp5_c"=c(3,1,3,2)
)
# Using purrr::map to iterate over lists of dataframes
results <-
map(list(dat_a, dat_b, dat_c), function(x)
metaMDS(x[, 3:7], , distance = "bray", autotransform = FALSE))
results %>%
map2(c("a", "b", "c"), ~ tibble(
site = .y,
species = rownames(.x$species),
x = .x$species[,1],
y = .x$species[,2]
)) %>%
bind_rows() %>%
ggplot(aes(x, y, label = species, col = site)) +
geom_text()
由 reprex package (v2.0.0)
于 2021-05-08 创建
我正在尝试使用距离数据的标签 (spp) 绘制 metaMDS。
dat<-data.frame(
"site" = c("a", "a", "a",'a', "b", "b", "b", 'b', "c", "c", "c",'c'),
"sample"=c(rep(1,4),rep(2,4),rep(3,4)),
"sp1"=c(1,3,3,2,4,2,1,5,3,6,1,5),
"sp2"=c(1,3,3,2,4,2,1,5,3,6,1,5),
"sp3"=c(1,3,3,2,4,2,1,5,3,6,1,5),
"sp4"=c(2,3,4,1,5,3,1,5,5,8,9,1),
"sp5"=c(3,4,3,1,6,7,5,8,3,1,3,2)
)
我试过了
```
dat1<-dat[,3:7]
dmds<-metaMDS(dat1, distance = "bray", autotransform = FALSE)
mds1 <- dmds$points[,1]
mds2 <- dmds$points[,2]
plt<-cbind(dat,mds1,mds2)
spp<-names(dat1)
ggplot(plt,aes(mds1,mds2, shape=site,color=site))+
geom_point()+
geom_text(aes(label=spp), size=3)
```
但是,得到一个错误
错误:美学必须为长度 1 或与数据相同 (12):标签
因为dat
的行数和"spp<-names(dat1)"的行数不同
有什么方法可以解决这个错误吗?
I would like to have something like this, but with the correct spp number
作为进一步的潜在方向 - 我认为你的意思是每个站点都有一组单独的物种,而不是每个站点标记的五个 (sp1:sp5) 相同物种?这是一个可能看起来像的工作示例,传递三个数据框以允许代表三个不同物种集的不同列:
library(tidyverse)
library(vegan)
dat_a<-data.frame(
"site" = "a",
sample = c(1:4),
"sp1_a"=c(1,3,3,2),
"sp2_a"=c(1,3,3,2),
"sp3_a"=c(1,3,3,2),
"sp4_a"=c(2,3,4,1),
"sp5_a"=c(3,4,3,1)
)
dat_b<-data.frame(
"site" = "b",
sample = c(1:4),
"sp1_b"=c(4,2,1,5),
"sp2_b"=c(4,2,1,5),
"sp3_b"=c(4,2,1,5),
"sp4_b"=c(5,3,1,5),
"sp5_b"=c(6,7,5,8)
)
dat_c<-data.frame(
"site" = "c",
sample = c(1:4),
"sp1_c"=c(3,6,1,5),
"sp2_c"=c(3,6,1,5),
"sp3_c"=c(3,6,1,5),
"sp4_c"=c(5,8,9,1),
"sp5_c"=c(3,1,3,2)
)
# Using purrr::map to iterate over lists of dataframes
results <-
map(list(dat_a, dat_b, dat_c), function(x)
metaMDS(x[, 3:7], , distance = "bray", autotransform = FALSE))
results %>%
map2(c("a", "b", "c"), ~ tibble(
site = .y,
species = rownames(.x$species),
x = .x$species[,1],
y = .x$species[,2]
)) %>%
bind_rows() %>%
ggplot(aes(x, y, label = species, col = site)) +
geom_text()
由 reprex package (v2.0.0)
于 2021-05-08 创建