Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R Phyloseq ggplot2对象不允许添加某些元素_R_Ggplot2_Vegan_Phyloseq - Fatal编程技术网

R Phyloseq ggplot2对象不允许添加某些元素

R Phyloseq ggplot2对象不允许添加某些元素,r,ggplot2,vegan,phyloseq,R,Ggplot2,Vegan,Phyloseq,我想修改phyloseq包生成的图(从github下载)。Phyloseq图是ggplot2对象,因此我认为可以通过向Phyloseq创建的对象添加ggplot2对象来添加元素。在某些情况下,这是可行的,但在其他情况下,我不明白为什么。例如: require(phyloseq) require(grid) require(ggplot2) require(plyr) #use the GlobalPatterns dataset from the Phyloseq package GP <

我想修改phyloseq包生成的图(从github下载)。Phyloseq图是ggplot2对象,因此我认为可以通过向Phyloseq创建的对象添加ggplot2对象来添加元素。在某些情况下,这是可行的,但在其他情况下,我不明白为什么。例如:

require(phyloseq)
require(grid)
require(ggplot2)
require(plyr)
#use the GlobalPatterns dataset from the Phyloseq package
GP <- GlobalPatterns
#do some preprocessing to the data
wh0 <- genefilter_sample(GP, filterfun_sample(function(x) x > 5), A = 0.5 * nsamples(GP))
GP1 <- prune_taxa(wh0, GP)
GP1 <- transform_sample_counts(GP1, function(x) 1e+06 * x/sum(x))
phylum.sum = tapply(taxa_sums(GP1), tax_table(GP1)[, "Phylum"], sum, na.rm = TRUE)
top5phyla = names(sort(phylum.sum, TRUE))[1:5]
GP1 <- prune_taxa((tax_table(GP1)[, "Phylum"] %in% top5phyla), GP1)
#ordination for NMDS plot using a Bray-Curtis distance
GP.ord <- ordinate(GP1, "NMDS", "bray") 
#create plot
p3 <- plot_ordination(GP1, GP.ord, type = "biplot", color = "SampleType", shape = "Phylum", title = "biplot")

错误消息已经让您知道需要在添加的层中修复什么

要修改的ggplot2对象在其某个层的
p3$data
$data
槽中有一个列变量
id.type
,如果没有覆盖它,这是一个美学映射参数,隐式传递给新层。考虑到您添加的层在这两种情况下都指定了x和y,我怀疑
id.type
是一个刻面或颜色变量。在ggplot2的最新版本中,您可以包含一个参数
inherit.aes=FALSE
,以避免此继承的映射,在这种情况下,您将丢失未指定的映射。结果是不同的,这取决于它是什么(例如,如果是facet,则层显示在两个面板中,我认为;如果是color,则层被指定为默认颜色)


或者,您可以将
id.type
列添加到新图层的数据中。这取决于您想要达到的结果。

什么是
top5phyla
?非常抱歉,我遗漏了这一点,它已经添加了。如果您想要组装“phyloseq”(而不是“phyloseq”)包,那么会缺少大量的require/library语句。在安装并加载未指定的CRAN包之前,似乎无法从源代码处编译。一个真正的PITA去经历所有这些,然后发现“'id.type'没有找到”。我不知道你的意思。您是否按照说明进行了操作?这听起来似乎最好在回购协议上提出问题
require(vegan)    
# First, lets apply envfit to the human/not human variable
    human = get_variable(GP1, "SampleType") %in% c("Feces", "Mock", "Skin", "Tongue")
    sample_data(GP1)$human <- factor(human)

    nmds.envfit <- envfit(GP.ord$points, env = as.data.frame(sample_data(GP1)$human), perm = 999) #standard envfit
    str(nmds.envfit)

    #data for the envfit arrows
    vec.sp.df<-as.data.frame(cbind((nmds.envfit$factors$centroids*sqrt(nmds.envfit$factors$r)),pvals=nmds.envfit$factors$pvals)) #this is necessary, see Gavin Simpson in the link provided above
    env.scores.nmds <- as.data.frame(vec.sp.df[vec.sp.df$pvals<0.05,]) #extracts relevant scores from envifit
    #extracts relevant scores from envifit
    env.scores.nmds <- cbind(env.scores.nmds, env.variables = c("Not Human", "Human")) #and then gives them their names
    env.scores.nmds

mult<- 1  #can change this if the arrows need to be a different length
###Now let us add these vectors to p3
p3 + geom_segment(data = env.scores.nmds,
                   aes(x = 0, xend = mult*MDS1, y = 0, yend = mult*MDS2),
                   arrow = arrow(length = unit(0.75, "cm")), colour = "black") + #arrows for envfit.  doubled the length for similarity to the plot() function. NB check ?envfit regarding arrow length if not familiar with lengths
      geom_text(data = env.scores.nmds,   #labels the environmental variable arrows * "mult" as for the arrows
                aes(x = mult*MDS1, y = mult*MDS2, label=env.variables),
                size = 6,
                hjust = -0.5) 
p3+ geom_hline(yintercept=0.75)