简体   繁体   English

在 x 轴上加倍条,同时在 ggplot 中保留 1 个常见图例

[英]Doubling bars on x-axis, while keeping 1 common legend in ggplot

I have the following plot in R:我在 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")


My goal is to have two bars for "A", two bars for "B" etc, instead of just one.我的目标是为“A”设置两个小节,为“B”等设置两个小节,而不仅仅是一个。 I will of course have to feed R, with data containing the other frequencies.我当然必须用包含其他频率的数据来馈送 R。 They look like this:它们看起来像这样:

Frequency2=c(523,900,400,155,168,428,1050,516,742,129,221,2698,7829,1147,144,7132,4437,562,1551,2691,103,516,22,12,939)

However, I cant seem to figure how to incorporate them.但是,我似乎无法弄清楚如何合并它们。 Furthermore, I'd like to just keep 1 common legend (description of what the different letters represent).此外,我只想保留 1 个常见的图例(不同字母代表的描述)。

Can anyone nudge me in the right direction?谁能把我推向正确的方向? I can't seem to find information on it, on ggplots site.我似乎无法在 ggplots 网站上找到有关它的信息。

Does this help?这有帮助吗?

library(tidyverse)

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

dat <- cbind(
    dat
    , Frequency2=c(523,900,400,155,168,428,1050,516,742,129,221,2698,7829,1147,144,7132,4437,562,1551,2691,103,516,22,12,939))

dat %>%
    gather("variable", "value", -FunctionClass, -legend) %>%
    ggplot(aes(FunctionClass, value, fill = legend, group = variable)) +
    geom_bar(stat="identity", position=position_dodge(), colour="seashell") +
    guides (fill = guide_legend(ncol = 1)) +
    xlab("COG Class") +
    ggtitle("COG distribution")

在此处输入图片说明

What is happening here is that I add the additional column to the dataframe ( cbind() ) and then turn it from a wide format into a long format ( gather() ).这里发生的事情是我将附加列添加到数据帧( cbind() ),然后将其从宽格式转换为长格式( gather() )。 Last thing that is needed then is that I enter the new column variable as a group aesthetic.然后需要做的最后一件事是我输入新的列variable作为group美学。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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