【问题标题】:R, ggplot — customize my barplotR, ggplot — 自定义我的条形图
【发布时间】:2017-06-20 23:24:02
【问题描述】:

这是我的数据结构:

      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”的情况应该只有一个酒吧。 因此,条形不应相互重叠,而应水平排列。

编辑 -- dput(head(myData, 20)):

结构(列表(加入 = 结构(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_质粒”、“CP003799”、 “CP009907”、“CP009908_质粒”、“CP011386”、“CP012381”、“CP016827”、 “FAM22155”、“FAM22155_质粒”、“FAM8105”、“FAM8105_质粒”、 “FAM8627”、“FAM8627_plasmid”)、class= “因子”)、来源 = 结构(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 = 结构(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= “因子”),NucSource = 结构(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 = 结构(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("#1C9099", "#A6BDDB", "#ECE2F0"), class= “因素”), 计数 = 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, 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")

【问题讨论】:

  • 能否请您dput(head(myData, 20)) 并将其复制/粘贴到您的问题中?这使我们可以更轻松地为您提供帮助,而不必按照您提供的方式创建数据框。

标签: r ggplot2 bar-chart


【解决方案1】:

你需要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:我更改了一些内容,因为我不确定您使用的是哪个主题或额外的软件包。

【讨论】:

  • 不错!现在,如何使只有“ch”的条与具有“ch”和“pl”的条具有相同的宽度。我怎样才能让“ch”和“pl”出现在各个条的顶部而不是中心?
  • 我建议在栏和文本中使用position = position_dodge()。这样 OP 可以设置 position = position_dodge(width = 0.9) 使文本居中。 geom_col() 也是 geom_bar(stat = "identity") 的缩写形式。
猜你喜欢
  • 2013-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-21
相关资源
最近更新 更多