R Stacked Bars plot 使用 ggplot 2

R Stacked Bars plot using ggplot 2

我想像上传的图片一样制作一个 % 堆叠图。 这是我的脚本,但出现以下错误:

gplot(data=res, aes(x=GO.term, y=up)) + geom_bar()
**Error: stat_count() must not be used with a y aesthetic.**

# GO 项的条形图

setwd("C:/Users/gmbz/Desktop/RNA analysis/Rscripts/Bar plot") #set up working directory 
res <- read.delim("GOBarPlot.txt", header=TRUE) 
head(res)
tail(res)
show(res)
library(ggplot2)
ggplot(data=res, aes(x=up, y=down, fill=GO.term)) + geom_bar()

这是我的数据:

Goterm  up  down
metal ion transmembrane transporter activity    10  90
translational elongation    22.22222222 77.77777778
metal ion transport 25  75
translation 30.08474576 69.91525424
aminoacyl-tRNA ligase activity  30.76923077 69.23076923
anchored component of membrane  33.33333333 66.66666667
apoptotic process   33.33333333 66.66666667
ribosome    34.11764706 65.88235294
regulation of cell cycle    35  65
translation elongation factor activity  35.71428571 64.28571429
cell wall   37.20930233 62.79069767
obsolete microsome  37.5    62.5
biosynthetic process    38.46153846 61.53846154
extracellular region    38.88888889 61.11111111
unfolded protein binding    42.10526316 57.89473684
protein folding 43.66197183 56.33802817
glutamine metabolic process 43.75   56.25
cellular amino acid metabolic process   46.875  53.125
fungal-type vacuole 48.61111111 51.38888889
glycolytic process  53.33333333 46.66666667
oxidoreductase activity 54.03225806 45.96774194
oxidation-reduction process 54.57875458 45.42124542
plasma membrane 55.6097561  44.3902439
regulation of cell size 57.14285714 42.85714286
cytosol 57.25806452 42.74193548
transmembrane transport 57.48792271 42.51207729
flavin adenine dinucleotide binding 62.16216216 37.83783784
metalloendopeptidase activity   63.15789474 36.84210526
transmembrane transporter activity  65.71428571 34.28571429
structural constituent of ribosome  66.34146341 33.65853659
chronological cell aging    66.66666667 33.33333333
transporter activity    69.33333333 30.66666667
mitochondrial inner membrane    72  28
substrate-specific transmembrane transporter activity   75  25
mitochondrial outer membrane    76.11940299 23.88059701
integral component of mitochondrial inner membrane  92.85714286 7.142857143
fungal-type cell wall   100 0

有反馈吗?

问题 2. 如何更改 GOterms 的字体,最好是 Times New Roman?这可能吗?

我们首先必须更改您的数据结构以使其可用: 图书馆(整洁) 图书馆(dplyr)

df %>% 
    arrange(desc(up)) %>% 
    gather(direction, value, -Goterm) -> df2

然后我们可以绘制您感兴趣的内容:

library(ggplot2)
library(scales)
df %>% 
    arrange(desc(up)) %>% 
    gather(direction, value, -Goterm) -> df2
ggplot(df2) +
    geom_col(aes(x = Goterm, y = value, fill = direction), 
             position = 'fill',
             width = .3) +
    scale_fill_manual(values = c('navy','red')) +
    scale_x_discrete(limits = df$Goterm) +
    scale_y_continuous(labels = percent_format(), expand = c(0,0)) +
    coord_flip() +
    theme(legend.position = 'bottom',
          legend.direction = 'horizontal',
          legend.title = element_blank(),
          legend.text = element_text(size = 10),
          legend.key.size = unit(.5, 'lines'),
          panel.background = element_rect(fill = 'transparent'),
          panel.grid.major.x = element_line(size = .1, 
                                            color = 'black', 
                                            linetype = 'dashed'),
          axis.title.y = element_blank(),
          axis.title.x = element_blank(),
          )

原始数据:

df <- read.table(text = '    Goterm up  down
    "metal ion transmembrane transporter activity"  10  90
                 "translational elongation" 22.22222222 77.77777778
                 "metal ion transport"  25  75
                 "translation"  30.08474576 69.91525424
                 "aminoacyl-tRNA ligase activity"   30.76923077 69.23076923
                 "anchored component of membrane"   33.33333333 66.66666667
                 "apoptotic process"    33.33333333 66.66666667
                 "ribosome" 34.11764706 65.88235294
                 "regulation of cell cycle" 35  65
                 "translation elongation factor activity"   35.71428571 64.28571429
                 "cell wall"    37.20930233 62.79069767
                 "obsolete microsome"   37.5    62.5
                 "biosynthetic process" 38.46153846 61.53846154
                 "extracellular region" 38.88888889 61.11111111
                 "unfolded protein binding" 42.10526316 57.89473684
                 "protein folding"  43.66197183 56.33802817
                 "glutamine metabolic process"  43.75   56.25
                 "cellular amino acid metabolic process"    46.875  53.125
                 "fungal-type vacuole"  48.61111111 51.38888889
                 "glycolytic process"   53.33333333 46.66666667
                 "oxidoreductase activity"  54.03225806 45.96774194
                 "oxidation-reduction process"  54.57875458 45.42124542
                 "plasma membrane"  55.6097561  44.3902439
                 "regulation of cell size"  57.14285714 42.85714286
                 "cytosol"  57.25806452 42.74193548
                 "transmembrane transport"  57.48792271 42.51207729
                 "flavin adenine dinucleotide binding"  62.16216216 37.83783784
                 "metalloendopeptidase activity"    63.15789474 36.84210526
                 "transmembrane transporter activity"   65.71428571 34.28571429
                 "structural constituent of ribosome"   66.34146341 33.65853659
                 "chronological cell aging" 66.66666667 33.33333333
                 "transporter activity" 69.33333333 30.66666667
                 "mitochondrial inner membrane" 72  28
                 "substrate-specific transmembrane transporter activity"    75  25
                 "mitochondrial outer membrane" 76.11940299 23.88059701
                 "integral component of mitochondrial inner membrane"   92.85714286 7.142857143
                 "fungal-type cell wall"    100 0', header = T, stringsAsFactors = F)