简体   繁体   中英

R changing bar-plot to differential abundance plot

I have the following plot in R, which is a bar-plot:

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

The plot looks like this: Barplot

However, i figured that I need to do a differential abundance plot instead, which looks kind of like this: Differential abundance plot

Any suggestions for how i can convert my plot to that, while keeping the colors etc?

Here is the is the differential abundance: Abundance=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) ) Thanks!

Something like this? The key is to reorder the labels according to the abundance first, see the factor(..) below and then you do ggplot. The -ve and +ve, you specify with 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"))

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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