我如何生成用于丰度的散点图和带有门索对象的数值变量?

发布于 2025-02-02 19:05:06 字数 111 浏览 2 评论 0原文

我在R中有一个Teyloseq对象,并且我想生成散点图,以显示单个分类单元与样本数据中的数字变量之间的关联(即我在x轴上的变量,y轴上的特定分类单元)。 我想我需要发挥某种循环功能,但是我对如何开始有点困扰!

I have a phyloseq object in R, and I would like to generate scatterplots to show associations between individual taxa and a numeric variable that's in my sample data (i.e. my variable on x axis, abundance of particular taxa on y axis).
I guess I need to make some sort of loop function, but I'm a bit stuck on how to get started with this!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

因为看清所以看轻 2025-02-09 19:05:06

有关tidyverse解决方案,请参见下文。我使用GlobalPatterns来自Thyloseq的数据创建一个可重复的示例。我没有足够的声誉来发布图像,但是输出应该看起来像

require("phyloseq")
require("tidyverse")

# Load the data
data(GlobalPatterns)

# Create a continuous x-variable
sample_data(GlobalPatterns)$Variable <- rnorm(nsamples(GlobalPatterns))

# select a single taxon from 
set.seed(1)
taxon <- sample(taxa_names(GlobalPatterns), size = 1)

# Function that create a scatterplot between a continuous variable (x)
# and the abundance of a taxon (y)
physeq_scatter_plot <- function(ps, variable, taxon){
  
  # Subset to taxon
  ps <- prune_taxa(x = ps, taxa = taxon)
  
  # Convert to long data format
  psdf <- psmelt(ps)
  
  # Plot
  psdf %>%
    ggplot(aes_string(x = variable,
                      y = "Abundance")) +
    geom_point()
}

physeq_scatter_plot(ps = GlobalPatterns, variable = "Variable", taxon = taxon)
#> Warning in prune_taxa(taxa, phy_tree(x)): prune_taxa attempted to reduce tree to 1 or fewer tips.
#>  tree replaced with NULL.

>

See below for a tidyverse solution. I used the GlobalPatterns data from phyloseq to create a reproducible example. I haven't got enough reputation to post images, but the output should look like this

require("phyloseq")
require("tidyverse")

# Load the data
data(GlobalPatterns)

# Create a continuous x-variable
sample_data(GlobalPatterns)$Variable <- rnorm(nsamples(GlobalPatterns))

# select a single taxon from 
set.seed(1)
taxon <- sample(taxa_names(GlobalPatterns), size = 1)

# Function that create a scatterplot between a continuous variable (x)
# and the abundance of a taxon (y)
physeq_scatter_plot <- function(ps, variable, taxon){
  
  # Subset to taxon
  ps <- prune_taxa(x = ps, taxa = taxon)
  
  # Convert to long data format
  psdf <- psmelt(ps)
  
  # Plot
  psdf %>%
    ggplot(aes_string(x = variable,
                      y = "Abundance")) +
    geom_point()
}

physeq_scatter_plot(ps = GlobalPatterns, variable = "Variable", taxon = taxon)
#> Warning in prune_taxa(taxa, phy_tree(x)): prune_taxa attempted to reduce tree to 1 or fewer tips.
#>  tree replaced with NULL.

Created on 2022-08-12 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文