r - 修改 GGplot2 对象

标签 r ggplot2 graphics data-visualization

但是,我很好奇,是否可以添加任何特定的图例或将哪个物种对应于观察到的预期绘图中,以分别知道它是哪个圆圈。我目前使用的是一个名为 finches 的假数据集。该包称为“cooccurr”,它创建一个 ggplot 对象。我很好奇如何实际编辑它以将物种标签放在这里。

另一种方法是提取标签和共现并使用基础图形,但这并不理想。

enter image description here

下面的代码片段

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)

enter image description here

无论如何,如果你想得到一个散点图,你可以去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()

enter image description here

关于r - 修改 GGplot2 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60993880/

相关文章:

r - 忽略未知的美学 : text in ggplot (using numbers or letters as points)

java - 如何使用 Graphics 类从一个顶点到另一个顶点绘制一条线?

.net - 在矩形内填充文本

matlab - 在 MATLAB 中使用 'fill' 函数时如何更改边线颜色?

r - 为什么 blogdown 不断重建 RMarkdown 文件?

r - Dplyr 计数/多个过滤器计数

r - 如何使用 ggplot 更改轴上数字的格式?

R包安装

r - 如何在 R 中找到某个观察结果最后一次出现后的日期?

r - 将图例标题放置在已位于条形图顶部的水平图例之上