R,ggplot - 自定义我的barplot

这是我的数据结构:

      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)

这是结果: 在这里输入图像描述

我现在想要的是,对于像“Str1”这样的具有“chr”和“pl”的示例,两个条应该水平相邻(也适用于“Str2”,“Str3”,“Str7” ,“Str8”)。 但对于像“Str4”那样只有“ch”的情况,应该只有一个条。 所以酒吧不应该在彼此顶部,而是水平排列。

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

结构(列表(登录=结构(c(16L,17L,12L,13L,14L,15L,1L,2L,3L,4L,5L,6L,7L,8L,9L,10L,11L),标签= c CP000517,CP002081,CP002427,CP002429,CP002430_plasmid,CP003799,CP009907,CP009908_plasmid,CP011386,CP012381,CP016827,FAM22155,FAM22155_plasmid “,”FAM8105“,”FAM8105_plasmid“,”FAM8627“,”FAM8627_plasmid“),class =”因子“),Source = structure(c(2L,2L,2L,2L,2L,2L,1L,1L,1L, 1L,1L,1L,1L,1L,1L,1L,1L),.Label = c(“NCBI”,“我们”),class =“factor”),Name = structure(c(1L,1L,2L, 2L,3L,3L,4L,5L,6L,7L,7L,8L,9L,9L,10L,11L,12L),标签= c(“FAM8627”,“FAM22155”,“FAM8105” ,“CNRZ32”,“H9”,“H10”,“R0052”,“KLDS1.8701”,“MB2-1”,“CAUH18”,“D76”),class =“factor”),NucSource = structure (1L,2L,1L,2L,1L,2L,1L,1L,1L,1L,2L,1L,1L,2L,1L,1L,1L).Label = c(“ch”,“pl” (1L,1L,1L,1L,1L,1L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L) ,2L ),标签= c(“#1C9099”,“#A6BDDB”,“#ECE2F0”),等级=“因子”),Counts = 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.131070639219183,0.0124769699976753,0.0186807581945172,0.141920329203257,0.0310466984258061, ),.Names = c(“Accession”,“Source”,“Name”,“NucSource”,“Order”,“color”,“Counts”,“Normalized”), row.names = c(NA,17L),class =“data.frame”)


在这里输入图像描述 你需要dodge不同的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:我改变了一些位,因为我不确定你使用的是哪个主题或额外的软件包。

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

上一篇: R, ggplot — customize my barplot

下一篇: "minimum count is not zero" error for zero inflated model