R,ggplot - 自定义我的条形图

时间:2017-02-03 14:42:27

标签: r ggplot2 bar-chart

这是我的数据结构:

      Accession Source   Name NucSource Order   color Counts   Normalized
1          Str1    Our   Str1        ch     1 #1C9099  66827 2.318683e-01
2  Str1_plasmid    Our   Str1        pl     2 #1C9099     26 9.021169e-05
3          Str2    Our   Str2        ch     3 #1C9099 288211 1.000000e+00
4  Str2_plasmid    Our   Str2        pl     4 #1C9099  71858 2.493243e-01
5          Str3    Our   Str3        ch     5 #1C9099  40600 1.408690e-01
6  Str3_plasmid    Our   Str3        pl     6 #1C9099  25266 8.766494e-02
7          Str4   NCBI   Str4        ch     7 #A6BDDB  21339 7.403951e-02
8          Str5   NCBI   Str5        ch     8 #A6BDDB  37776 1.310706e-01
9          Str6   NCBI   Str6        ch     9 #A6BDDB   3596 1.247697e-02
10         Str7   NCBI   Str7        ch    10 #A6BDDB   5384 1.868076e-02
11 Str7_plasmid   NCBI   Str7        pl    11 #A6BDDB  40903 1.419203e-01
12         Str8   NCBI   Str8        ch    12 #A6BDDB   8948 3.104670e-02
13         Str9   NCBI   Str9        ch    13 #A6BDDB  16557 5.744750e-02
14 Str9_plasmid   NCBI   Str9        pl    14 #A6BDDB   3738 1.296966e-02
15        Str10   NCBI  Str10        ch    15 #A6BDDB  10067 3.492927e-02
16        Str11   NCBI  Str11        ch    16 #A6BDDB   7306 2.534948e-02
17        Str12   NCBI  Str12        ch    17 #A6BDDB  10313 3.578281e-02

我在其上运行以下代码:

p<-ggplot(data=myData, aes(x=Name, y=Normalized, fill=Source)) +
  theme_few() +
  xlab("Strain") + ylab("Normalized counts") +
  geom_bar(stat="identity", aes(fill=myData$Source), colour="black", position="dodge") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.4)) +
  geom_text(aes(label=myData$NucSource), vjust=-0.5) +
  theme(legend.position="right") +
  scale_fill_manual(values=as.character(color.convert$color)[2:3])

print(p)

这就是结果: enter image description here

我现在想要的是,例如&#34; Str1&#34;我在哪里&#34; chr&#34;和&#34; pl&#34;两个条形应该水平相邻(对于&#34; Str2&#34;,&#34; Str3&#34;,&#34; Str7&#34;,&#34; Str8&#34;)。但对于像&#34; Str4&#34;我只有&#34; ch&#34;应该只有一个酒吧。 因此,条形图不应相互叠加,而应水平排列。

编辑 - 输入(head(myData,20)):

  

结构(列表(加入=结构(c(16L,17L,12L,13L,14L,15L,   1L,2L,3L,4L,5L,6L,7L,8L,9L,10L,11L),。标签= c(&#34; CP000517&#34;,   &#34; CP002081&#34;,&#34; CP002427&#34;,&#34; CP002429&#34;,&#34; CP002430_plasmid&#34;,&#34; CP003799&#34;,   &#34; CP009907&#34;,&#34; CP009908_plasmid&#34;,&#34; CP011386&#34;,&#34; CP012381&#34;,&#34; CP016827&#34;,   &#34; FAM22155&#34;,&#34; FAM22155_plasmid&#34;,&#34; FAM8105&#34;,&#34; FAM8105_plasmid&#34;,   &#34; FAM8627&#34;,&#34; FAM8627_plasmid&#34;),class =&#34; factor&#34;),Source =   结构(c(2L,2L,2L,2L,2L,2L,1L,1L,1L,1L,1L,1L,1L,1L,   1L,1L,1L),。标签= c(&#34; NCBI&#34;,&#34;我们&#34;),类=&#34;因子&#34;),名称=   结构(c(1L,1L,2L,2L,3L,3L,4L,5L,6L,7L,7L,8L,9L,9L,   10L,11L,12L),.标签= c(&#34; FAM8627&#34;,&#34; FAM22155&#34;,&#34; FAM8105&#34;,&#34; DPC   4571&#34;,&#34; CNRZ32&#34;,&#34; H9&#34;,&#34; H10&#34;,&#34; R0052&#34;,&#34; KLDS1.8701&#34 ;,&#34; MB2-1&#34;,   &#34; CAUH18&#34;,&#34; D76&#34;),class =&#34; factor&#34;),NucSource =结构(c(1L,2L,   1L,2L,1L,2L,1L,1L,1L,1L,2L,1L,1L,2L,1L,1L,1L),.标签=   c(&#34; ch&#34;,&#34; pl&#34;),class =&#34; factor&#34;),Order = 1:17,color =   结构(c(1L,1L,1L,1L,1L,1L,2L,2L,2L,2L,2L,2L,2L,2L,   2L,2L,2L),. Label = c(&#34;#1C9099&#34;,&#34;#A6BDDB&#34;,&#34;#ECE2F0&#34;),class =   &#34;因子&#34),       计数= c(66827L,26L,288211L,71858L,40600L,25266L,       21339L,37776L,3596L,5384L,40903L,8948L,16557L,3738L,       10067L,7306L,10313L),归一化= c(0.231868318697066,       9.02116851889761e-05,1,0.249324279781133,0.140869016102786,       0.0876649399224873,0.0740395057787524,0.133070639219183,       0.0124769699976753,0.0186807581945172,0.141920329203257,       0.0310466984258061,0.0574474950643799,0.0129696645860151,       0.0349292705691316,0.0253494835381023,0.0357828118982273       )),。Name = c(&#34;加入&#34;,&#34;来源&#34;,&#34;名称&#34;,&#34; NucSource&#34;,&#34;订单&# 34;,&#34;颜色&#34;,&#34;计数&#34;,&#34;标准化&#34;),row.names = c(NA,17L),   class =&#34; data.frame&#34;)

1 个答案:

答案 0 :(得分:1)

enter image description here您需要dodgefill不同的列:

ggplot(data=myData, aes(x = Name, y = Normalized, dodge = NucSource, fill = Source)) +
  geom_text(aes(label = NucSource), vjust = -0.5) +
  geom_col(colour="black", position="dodge") +
  labs(x = "Strain", y = "Normalized counts") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.4),
        legend.position = "right")

PS:我更改了一些内容,因为我不确定您使用的是哪个主题或额外的包。