但是,我很好奇,是否可以添加任何特定的图例或将哪个物种对应于观察到的预期绘图中,以分别知道它是哪个圆圈。我目前使用的是一个名为 finches 的假数据集。该包称为“cooccurr”,它创建一个 ggplot 对象。我很好奇如何实际编辑它以将物种标签放在这里。
另一种方法是提取标签和共现并使用基础图形,但这并不理想。
下面的代码片段
library(devtools)
#install_github("griffithdan/cooccur")
library(cooccur)
options(stringsAsFactors = FALSE)
data(finches)
cooccur.finches <- cooccur(mat=finches,
type="spp_site",
thresh=TRUE,
spp_names=TRUE)
summary(cooccur.finches)
plot(cooccur.finches)
p <- obs.v.exp(cooccur.finches)
# the ggplot2 object can be edited directly and then replotted
p
# alternatively, use base graphics, This is what I am currently doing but it is not correct
cooc.exp <- cooccur.finches$results$exp_cooccur
cooc.obs <- cooccur.finches$results$obs_cooccur
sp1 <- cooccur.finches$results$sp1_name
sp2 <- cooccur.finches$results$sp2_name
plot(cooc.obs ~ cooc.exp)
text(x = cooc.exp[1], y = cooc.obs[1], labels = sp1[1]) # plots only one name
最佳答案
我安装了cooccur_1.3,运行你的代码给出了这个图:
library(cooccur)
options(stringsAsFactors = FALSE)
data(finches)
cooccur.finches <- cooccur(mat=finches,
type="spp_site",
thresh=TRUE,
spp_names=TRUE)
plot(cooccur.finches)
无论如何,如果你想得到一个散点图,你可以去dataframe并做一个ggplot,下面我只标记物种1是Geospiza magnirostris的点,否则80个点来标记是相当疯狂的:
library(ggrepel)
library(ggplot2)
df = cooccur.finches$results
df$type = "random"
df$type[df$p_lt<0.05] = "negative"
df$type[df$p_gt<0.05] = "positive"
ggplot(df,aes(x=exp_cooccur,y=obs_cooccur)) +
geom_point(aes(color=type)) + geom_abline(linetype="dashed") +
geom_label_repel(data=subset(df,sp1_name=="Geospiza magnirostris"),
aes(label=paste(sp1_name,sp2_name,sep="\n")),
size=2,nudge_x=-1,nudge_y=-1) +
scale_color_manual(values=c("#FFCC66","light blue","dark gray")) +
theme_bw()
关于r - 修改 GGplot2 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60993880/