R, ggplot — customize my barplot

This is my datastructure:

      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

I run the following code on it:

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)

And this is the result: 在这里输入图像描述

What I would like to have now, is that for examples like "Str1" where I have "chr" and "pl" the two bars should be horizontally next to each other (Also for "Str2", "Str3", "Str7", "Str8"). But for cases like "Str4" where I have only "ch" there should be only one bar. So the bars should not be on top of each other but horizontally arranged.

EDIT -- dput(head(myData, 20)):

structure(list(Accession = structure(c(16L, 17L, 12L, 13L, 14L, 15L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L), .Label = c("CP000517", "CP002081", "CP002427", "CP002429", "CP002430_plasmid", "CP003799", "CP009907", "CP009908_plasmid", "CP011386", "CP012381", "CP016827", "FAM22155", "FAM22155_plasmid", "FAM8105", "FAM8105_plasmid", "FAM8627", "FAM8627_plasmid"), class = "factor"), Source = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L ), .Label = c("NCBI", "Our"), class = "factor"), Name = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 5L, 6L, 7L, 7L, 8L, 9L, 9L, 10L, 11L, 12L), .Label = c("FAM8627", "FAM22155", "FAM8105", "DPC 4571", "CNRZ32", "H9", "H10", "R0052", "KLDS1.8701", "MB2-1", "CAUH18", "D76"), class = "factor"), NucSource = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L), .Label = c("ch", "pl"), class = "factor"), Order = 1:17, color = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L ), .Label = c("#1C9099", "#A6BDDB", "#ECE2F0"), class = "factor"), Counts = c(66827L, 26L, 288211L, 71858L, 40600L, 25266L, 21339L, 37776L, 3596L, 5384L, 40903L, 8948L, 16557L, 3738L, 10067L, 7306L, 10313L), Normalized = c(0.231868318697066, 9.02116851889761e-05, 1, 0.249324279781133, 0.140869016102786, 0.0876649399224873, 0.0740395057787524, 0.131070639219183, 0.0124769699976753, 0.0186807581945172, 0.141920329203257, 0.0310466984258061, 0.0574474950643799, 0.0129696645860151, 0.0349292705691316, 0.0253494835381023, 0.0357828118982273 )), .Names = c("Accession", "Source", "Name", "NucSource", "Order", "color", "Counts", "Normalized"), row.names = c(NA, 17L), class = "data.frame")


在这里输入图像描述 You need to dodge on a different column than fill :

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: I changed some bits, because I was not sure which theme or extra packages you are using.

链接地址: http://www.djcxy.com/p/64572.html

上一篇: 轴与小平面

下一篇: R,ggplot - 自定义我的barplot