3

我想修改 phyloseq 包生成的图(从 gi​​thub 下载)。Phyloseq 图是 ggplot2 对象,所以我认为我可以通过将 ggplot2 对象添加到 phyloseq 创建的对象来添加元素。在某些情况下,这有效,但在其他情况下无效,我不明白为什么。例如:

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

现在我将尝试从 vegan 包中向图中添加一些 envfit() 箭头,请参见此处的上一个问题:

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) 

但是,这会返回一个错误:“eval 中的错误(expr,envir,enclos):找不到对象 'id.type'”

如果我们尝试添加另一种类型的 ggplot2 元素,它将起作用:

p3+ geom_hline(yintercept=0.75)
4

1 回答 1

0

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

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

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

于 2017-04-21T20:06:10.503 回答