1

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:

enter image description here

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?

zx8754
  • 52,746
  • 12
  • 114
  • 209
Komal Rathi
  • 4,164
  • 13
  • 60
  • 98
  • You could look [here](http://stackoverflow.com/questions/29068867/plotly-not-getting-geom-text-in-r-ggplot) – Miha Jun 20 '16 at 20:02
  • 1
    Produces `Error: Aesthetics must be either length 1 or the same as the data (11): label, colour, position, vjust, x, y` – dww Jun 20 '16 at 20:18
  • @dww replace content in `geom_text()` with this `geom_text(aes(label = Samples, color = Type), position=position_dodge(width=0.9), vjust=-0.25, size=5)` – Miha Jun 20 '16 at 20:18
  • Just to note: data to ink ratio is bad here. – zx8754 Sep 02 '16 at 08:20
  • @zx8754 can you please elaborate? Thanks – Komal Rathi Feb 27 '18 at 18:11

1 Answers1

2

Using your data I completed till the stage of plotly_build.(without dark theme)

If you inspect your plotly object, it has 2 categories : data and layout. The text elements of the plot are of type "scatter" having the same x,y attribute as of your bars. Hence, they are overlapping with your bars.

p$data[[1]]$type   ## Prints result as "bar" type for dat$Type "Cell Line"
p$data[[4]]$type   ## Prints result as "scatter" for dat$Type "Cell Line"
p$data[[4]]$mode   ## Prints text's mode as "text"
P$data[[1]]$mode   ## Prints bar's mode as NULL

In plotly , geom_text is not well translated to plotly charts. You need to change the $y (y-axis coordinates) for scatter types (here texts are mapped as scatter type).

p$data[[4]]$y 
[1] 26770 19822 21376 12716

Since your y-axis is scaled to dat$Genes ( ranging from 0 to 52576) , increasing/decreasing their coordinate to 1 or 10 wont result in noticeable difference. So you might want to try larger values say of range 1000, 2000.
Or, you can write code to add a uniform quantity to all y coordinates of mode type "text". Here is one way

for(i in seq(p$data)){ 
  if(!is.null(p$data[[i]]$mode)){                        ## if mode is not NULL
    if(p$data[[i]]$mode =="text"){                       ## above TRUE and if mode is "text"
      p$data[[i]]$y = p$data[[i]]$y + max(dat$Genes)/50  ## add max(dat$Genes)/50 or any addend you wish
    }
  }
}

Here is the plot : Plot from above

Nsquare
  • 91
  • 1
  • 5