1
votes

I am trying to use plotly+ggplot to make an interactive Q-Q norm plot and customize the mouse-over text to another vector in my dataframe.

With the default tooltip, I can plot the expected Q-Q norm distribution. When aes text is mapped to customize the tooltip, the stat_qq transformation sets the theoretical quantiles to zero. How can I reproduce the mapping without overriding the stat_qq transformation?

library(ggplot2)
library(plotly)

# dataframe with 20 points normally distributed around zero
n <- 20
table <- cbind.data.frame(value = rnorm(n, ),     
                      key = c(letters[1:20]))

## plotly with default tooltip
p <- ggplot(table, aes(sample = value)) +
  geom_point(stat = "qq",
             shape = 21,
             size = 2)
ggplotly(p)

plotly with default tooltip

## plotly with customized tooltip mapping 
p <- ggplot(table, aes(sample = value,
                       text = paste0('key is ', key))) +
  geom_point(stat = "qq",
             shape = 21,
             size = 2)
ggplotly(p, tooltip = "text")

plotly with custom tooltip

sessionInfo()

R version 3.4.2 (2017-09-28) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 16.04.2 LTS

Matrix products: default BLAS: /usr/lib/atlas-base/atlas/libblas.so.3.0 LAPACK: /usr/lib/atlas-base/atlas/liblapack.so.3.0

locale: 1 LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8
[6] LC_MESSAGES=en_US.UTF-8 LC_PAPER=en_US.UTF-8 LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages: 1 stats graphics grDevices utils datasets methods base

other attached packages: 1 plotly_4.7.1 ggplot2_2.2.1

loaded via a namespace (and not attached): 1 Rcpp_0.12.13 bindr_0.1 magrittr_1.5 tidyselect_0.2.2 munsell_0.4.3 xtable_1.8-2
[7] viridisLite_0.2.0 colorspace_1.3-2 R6_2.2.2 rlang_0.1.2 httr_1.3.1 plyr_1.8.4
[13] dplyr_0.7.4 tools_3.4.2 grid_3.4.2 data.table_1.10.4-2 gtable_0.2.0 crosstalk_1.0.0
[19] htmltools_0.3.6 yaml_2.1.14 lazyeval_0.2.0 assertthat_0.2.0 digest_0.6.12 tibble_1.3.4
[25] bindrcpp_0.2 shiny_1.0.5 tidyr_0.7.2 purrr_0.2.4 htmlwidgets_0.9 mime_0.5
[31] glue_1.1.1 labeling_0.3 compiler_3.4.2 scales_0.5.0 jsonlite_1.5 httpuv_1.3.5
[37] pkgconfig_2.0.1

1

1 Answers

0
votes

UPDATE: After some research, I found a solution around the remapping issue which I adapted from Paul Rubin. Rather than work with aes mapping, I substituted my labels directly into the plotly object.

p <- ggplot(table, aes(sample = value)) +
  geom_point(stat = "qq",
             shape = 21,
             size = 2)
z <- plotly_build(p)
table <- table[order(table$value), ]
table <- table %>% mutate(text = key)
z$x$data[[1]]$text <- table$text
z          

plotly with custom text