I am creating a data summary bar plot using ggplot2
, plotly
and ggthemes
. Here is my code:
# data
dput(dat)
structure(list(Source = structure(1:11, .Label = c("GTEx", "Target",
"RNASeq", "Microarray", "CNA", "Mutation", "HI51", "IH250", "IH251",
"NB88", "OBER649"), class = "factor"), Type = c("External", "External",
"Cell Line", "Cell Line", "Cell Line", "Cell Line", "Patient Sample",
"Patient Sample", "Patient Sample", "Patient Sample", "Patient Sample"
), Samples = c(1641, 162, 41, 29, 34, 29, 51, 250, 251, 88, 649
), Genes = c(52576, 25818, 26770, 19822, 21376, 12716, 21118,
17768, 17768, 21118, 19858)), .Names = c("Source", "Type", "Samples",
"Genes"), class = "data.frame", row.names = c(NA, -11L))
library(ggplot2)
library(ggthemes)
library(plotly)
p <- ggplot(data = dat, aes(x = Source, y = Genes)) +
geom_bar(stat="identity",aes(color = Type)) +
theme_bw() +
geom_text(aes(label = Samples, color = Type), position=position_dodge(width=0.9), vjust=-0.25, size=5) +
theme_hc(bgcolor = "darkunica") +
scale_colour_hc("darkunica") +
theme(axis.title = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank())
p <- plotly_build(p)
p$layout$plot_bgcolor <- p$layout$paper_bgcolor
p$layout$annotations[[1]]$text <- ""
This is the plot that is created:
The labels over the bars show the number of samples. When I hover over - it shows the Source
, Genes
, and Type
. Even when I am using position
and vjust
arguments in geom_text
, it is not working. How can I adjust the labels over the bars such that they are visible i.e. entirely above the bars or below the bars?