简体   繁体   English

向图例添加元素时遇到问题 (iNEXT / ggplot2)

[英]Trouble adding elements to legend (iNEXT / ggplot2)

I have made a rarefaction curves using the iNEXT package in R and added two horizontal lines for the asymptotes of both curves manually (the package does not do that, so I tried to utilize ggplot functions and it seemed to work), furthermore I extended the x/y axis. I have made a rarefaction curves using the iNEXT package in R and added two horizontal lines for the asymptotes of both curves manually (the package does not do that, so I tried to utilize ggplot functions and it seemed to work), furthermore I extended the x/y 轴。 Unfortunately, my skills are apparently too limited to figure out how to add the asymptotes in the legend of the graph under, labelled as "Asymptotes: North and South" (or something like that).不幸的是,我的技能显然太有限,无法弄清楚如何在下图的图例中添加渐近线,标记为“渐近线:北方和南方”(或类似的东西)。 I would very much appreciate the help.我将非常感谢您的帮助。 I added my code below and the graph as a picture, if something is missing, let me know!我在下面添加了我的代码并将图表作为图片添加,如果缺少某些内容,请告诉我!

Best wishes and stay healthy!最良好的祝愿并保持健康!

### Rarefaction with iNEXT -> Species X Visits incidence_frequency data

# List for both regions
incidence_freq_north <- c(7,2,0,0,4,0,2,0,2,1,0,0,0,6,0,1,1,0,0,0)
incidence_freq_south <- c(23,0,0,1,9,2,0,4,1,1,1,2,6,1,4,1,0,8,2,7,1)

list_rarefaction_freq = list(North = incidence_freq_north, South = incidence_freq_south)

## create output file
out_freq <- iNEXT(list_rarefaction_freq, q=0, datatype="incidence_freq", endpoint=NULL,
             size=NULL, knots=400, se=TRUE, conf=0.95, nboot=400)

# Sample-size-based R/E curves, separating plots by "order"
ggiNEXT(out_freq, type=1, facet.var="order") +
  ylim(c(0,40)) + xlim(c(0,70)) + 
  theme_bw(base_size = 18) + 
  geom_hline(yintercept=24, linetype="solid", color = "darkslategray2") + 
  geom_hline(yintercept=10, linetype="solid", color = "coral1")

Graph as image图为图像

I think one possible solution will be to draw your ggiNEXT graph on your own (here they provide a tutorial to do it: https://cran.r-project.org/web/packages/iNEXT/vignettes/Introduction.html )我认为一种可能的解决方案是自己绘制 ggiNEXT 图(这里他们提供了一个教程: https://cran.r-project.org/web/packages/iNEXT/vignettes/Introduction.html

Then, you can add a new dataframe corresponding to asymptope values and use new_color_scale function from ggnewscale package to add a new color scale on your graph:然后,您可以添加对应于渐近线值的新 dataframe 并使用来自new_color_scale package 的ggnewscale function 来添加新的色标:

df <- fortify(out_freq, type =1)

df.point <- df[which(df$method=="observed"),]
df.line <- df[which(df$method!="observed"),]
df.line$method <- factor(df.line$method, 
                         c("interpolated", "extrapolated"),
                         c("interpolation", "extrapolation"))

df.asympote <- data.frame(y = c(24,10),
                          Asymptote = c("North","South"))


library(ggnewscale)

ggplot(df, aes(x=x, y=y, colour=site)) + 
  geom_point(aes(shape=site), size=5, data=df.point) +
  geom_line(aes(linetype=method), lwd=1.5, data=df.line) +
  geom_ribbon(aes(ymin=y.lwr, ymax=y.upr,
                  fill=site, colour=NULL), alpha=0.2) +
  labs(x="Number of individuals", y="Species diversity") +
  scale_color_discrete(name = "Site")+
  scale_shape_discrete(name = "Site")+
  scale_fill_discrete(name = "Site")+
  scale_linetype_discrete(name = "Method")+
  theme_bw(base_size = 18)+
  new_scale_color()+
  geom_hline(data = df.asympote, aes(yintercept = y, color = Asymptote))

在此处输入图像描述

Does it answer your question?它回答了你的问题吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM