2

I'm creating an overlapping bar graph with the code below:

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

Since I'm using geom_label(), I get these nice boxed labels filled with the color of the respective bar. But when using scale_fill_manual(), which I do because I didn't find a better way to customise the colours, the boxed labels get a white background. Anybody an idea why?

Any help would be greatly appreciated! Thanks :)

My sessionInfo() is a bit long, but if it's necessary I can add it later. Don't think it matters in this scenario.

fakechek
  • 239
  • 2
  • 14
  • 1
    Can you make your example [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? – Roman Luštrik Apr 03 '17 at 08:21
  • Oh crap, sorry that I forgot the previous lines. Not on purpose :/ I'll add it right away. – fakechek Apr 03 '17 at 08:25
  • 1
    Remove `alpha = 1` from your `rgb` calls. Not sure why that works... You will like to add an `alpha` to `geom_label` though. (Also note that about 2/3 of your `ggplot` code is not necessary to reproduce the problem and can be left out.) – Axeman Apr 03 '17 at 08:34
  • Thanks a lot! Also for the comment about the rest of the code. I'll keep that in mind next time. – fakechek Apr 03 '17 at 08:38
  • 1
    `alpha = 1` means that is almost completely transparent, as it is relative to `[0:maxColorValue ]`. – GGamba Apr 03 '17 at 08:40

0 Answers0