【问题标题】:R changing bar-plot to differential abundance plotR将条形图更改为差异丰度图
【发布时间】:2020-01-29 14:42:31
【问题描述】:

我在 R 中有以下图,它是条形图:

dat <- data.frame(
  FunctionClass = factor(c("A", "B", "C", "D", "E", "F", "G", "H", "I",     "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "Y", "Z"), levels=c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "Y", "Z")),
  legend = c("A: RNA processing and modification", "B: Chromatin structure and dynamics", "C: Energy production and conversion", "D: Cell cycle control, cell division, chromosome partitioning", "E: Amino acid transport and metabolism", "F: Nucleotide transport and metabolism", "G: Carbohydrate transport and metabolism", "H: Coenzyme transport and metabolism", "I: Lipid transport and metabolism", "J: Translation, ribosomal structure and biogenesis", "K: Transcription", "L: Replication, recombination and repair", "M: Cell wall/membrane/envelope biogenesis", "N: Cell motility", "O: Posttranslational modification, protein turnover, chaperones", "P: Inorganic ion transport and metabolism", "Q: Secondary metabolites biosynthesis, transport and catabolism", "R: General function prediction only", "S: Function unknown", "T: Signal transduction mechanisms", "U: Intracellular trafficking, secretion, and vesicular transport", "V: Defense mechanisms", "W: Extracellular structures", "Y: Nuclear structure", "Z: Cytoskeleton"),
  Frequency=c(360,391,897,1558,1168,448,1030,536,732,1292,2221,2098,789,117,1744,732,437,5162,1251,2191,603,216,2,14,739)
)

library(ggplot2)

p <- ggplot(data=dat, aes(x=FunctionClass, y=Frequency, fill=legend))+
  geom_bar(stat="identity", position=position_dodge(), colour="seashell")
p + guides (fill = guide_legend(ncol = 1))+
  xlab("COG Class")+
  ggtitle("COG distribution")

剧情如下: Barplot

但是,我认为我需要做一个差异丰度图,看起来像这样: Differential abundance plot

关于如何将我的情节转换为该情节,同时保持颜色等的任何建议?

这是差异丰度: 丰度=c(,2.2,3.4,3.3,2,1.1,0.1,0.1,-0.3,-0.9,3,2.1,-0.3,-0.9,-2,-1.2,-0.4,-0.5,-3, -2,-0.3,-2.1,-1.3,-2.2,-3) ) 谢谢!

【问题讨论】:

  • 您应该首先计算每个 COG 的差异丰度。您的示例数据框中没有此数据
  • 我知道,但我只需要格式,我知道该怎么做的计算。
  • 所以你应该把它添加到data.frame中。它基本上是一个带有负值的条形图
  • 我现在这样做了,但是我需要将所有内容旋转 90 度。另外,您知道如何分别为正值和负值制作标签吗?
  • 使用 coord_flip() 进行旋转

标签: r ggplot2 plot


【解决方案1】:

这样的?关键是先根据丰度对标签重新排序,看下面的因子(..)然后你做ggplot。 -ve 和 +ve,您使用 fill=Abundance > 0..

指定
dat$Abundance=c(2,2.2,3.4,3.3,2,1.1,0.1,0.1,-0.3,    -0.9,3,2.1,-0.3,-0.9,-2,-1.2,-0.4,-0.5,-3,-2,-0.3,-2.1,-1.3,-2.2,-3)

dat$legend = factor(dat$legend,levels=dat$legend[order(dat$Abundance)])
ggplot(dat,aes(x=legend,y=Abundance,fill=Abundance>0))+
geom_col() + coord_flip()+
scale_fill_manual(values=c("lightblue","red"),
labels=c("negative","positive"))

【讨论】:

    猜你喜欢
    • 2013-03-22
    • 1970-01-01
    • 2011-01-23
    • 1970-01-01
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    • 2020-09-21
    • 1970-01-01
    相关资源
    最近更新 更多