修改GGplot2对象 [英] Modify GGplot2 Object

查看:61
本文介绍了修改GGplot2对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

但是我很好奇,如果可以添加任何特定的图例或在观察到的期望图中放入对应的物种,以分别知道它是哪个圆.我现在使用的是称为finches的伪数据集.该程序包称为"cooccur",它创建一个ggplot对象.我很好奇如何进行实际编辑以在此处放置物种标签.

I was curious however, if it is possible to add any specific legend or put which species corresponds in the observed-expected plot, to know which circle it is respectively. I am using a fake dataset at the moment called finches. The package is called "cooccur" which creates a ggplot object. I was curious on how to actually edit this to put labels of species on here.

或者是提取标签和共现并使用基本图形,但这并不理想.

Alternatively is to extract the labels and co-occurrences and use base graphics, but this is not as ideal.

下面的代码片段

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,运行您的代码可以得出以下图:

I installed cooccur_1.3, and running your code gives this plot:

library(cooccur)
options(stringsAsFactors = FALSE)
data(finches)
cooccur.finches <- cooccur(mat=finches,
               type="spp_site",
               thresh=TRUE,
               spp_names=TRUE)

plot(cooccur.finches)

无论如何,如果要获取散点图,则可以转到数据框并进行ggplot,下面仅标记物种1为Geospiza magnirostris的点,否则标记80点非常疯狂:

Anyway, if you want to get a scatter plot, you can go to the dataframe and do a ggplot, below I only label the points where species 1 is Geospiza magnirostris, otherwise 80 points to label is quite insane:

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

这篇关于修改GGplot2对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆