使用scale_fill_manual时如何保留geom_label填充?

时间:2017-04-03 08:16:49

标签: r ggplot2

我正在使用以下代码创建重叠的条形图:

library(ggplot2)
library(reshape)
library(scales)

x = data.frame('DB' = c('Table4annot', 'UCSC_old', 'UCSC_new'), 'all_elements' = c(595632, 605914, 711073), 'unique_loci' = c(264978, 265979, 274936), 'start_codons' = c(10661, 10714, 22815))

melted = melt(x, id="DB")

p = ggplot(melted, aes(x = DB, y = value, fill = variable))
p = p + geom_bar(stat = "identity", position = "identity", alpha = 0.7, color = 'black', size = 0.4)
p = p + geom_label(aes(label = comma(value), fill = variable), vjust = -0.2, size = 3, show.legend = FALSE, label.padding = unit(0.2, 'lines'))
p = p + scale_x_discrete(limits = c('Table4annot', 'UCSC_old', 'UCSC_new'), labels=c("Table4annot", "UCSC our archive", "UCSC current"))
p = p + scale_y_continuous(labels = comma, expand = c(0, 0), limits = c(0, 750000), breaks = seq(0, 700000, 100000))
p = p + theme(panel.border = element_rect(colour = 'black', fill = NA),
              panel.background = element_rect(fill = "white"),
              panel.grid.major.y = element_line(colour = "grey40", size = 0.3),
              panel.grid.minor.y = element_line(colour = "grey40", size = 0.1),
              panel.grid.major.x = element_blank(),
              panel.grid.minor.x = element_blank(),
              axis.title = element_blank(),
              axis.ticks.x = element_blank(),
              legend.title = element_blank(),
              legend.position = 'top')
p = p + scale_fill_manual(labels = c('All annotated elements', 'Unique loci with annotation', 'Number of annotated start codons'), values = c(rgb(86, 180, 233, maxColorValue = 255, alpha = 1), rgb(240, 228, 66, maxColorValue = 255, alpha = 1), rgb(0, 0, 0, maxColorValue = 255, alpha = 1)))
p

enter image description here

由于我使用的是geom_label(),因此我会获得这些带有相应条形颜色的漂亮盒装标签。但是当使用scale_fill_manual()时,我这样做是因为我没有找到更好的方法来自定义颜色,盒装标签会获得白色背景。有谁知道为什么?

任何帮助将不胜感激!谢谢:))

我的sessionInfo()有点长,但如果有必要,我可以稍后添加。在这种情况下,不要认为这很重要。

0 个答案:

没有答案