0

I would like to add interactivity to plots I've created in ggplot2. For 3 out of 4 horizontal bar plots I've created, this works fine when I wrap a plotting function in ggplotly(). However, for the last one, the plot reneders incorrectly and plots a strange scale. I've searched through bug issues and found this one which may be related, but I'm running even more recent versions of the packages:

  • plotly 4.9.1
  • ggplot2 3.2.1
  • R 3.6.1
# Data
sitename <- c('Heckscher Plgd',
'Strawberry Fields',
'Bethesda Terrace',
'Central Park West (Zone 1)',
'Reservoir (Northeast)',
'Central Park West (Zone 2)',
'Reservoir (Southeast)',
'Central Park South',
'Reservoir (Northwest)',
'Great Lawn And Cleopatra\'s Needle',
'Pilgrim Hill & Conservatory Water',
'Wollman Rink',
'Wien Walk And Arsenal',
'North Of The Arsenal',
'Wallach Walk And East Green',
'The Pool',
'Central Park West (Zone 3)',
'Central Park West (Zone 4)',
'Cedar Hill',
'79th St Yard And Summit Rock',
'Ross Pinetum')

Unique_Squirrel_ID <- c(100,89,60,32,36,57,85,61,123,43,64,89,44,22,55,46,38,85,64,50,9)
a <- data.frame(sitename,Unique_Squirrel_ID)

# Plot function
options(repr.plot.width=10)
#' Plots a bar chart of squirrel count by park region
#'
#' @param highlight (optional) a character vector of sitenames to highlight
#'
#' @return ggplot chart
#'
#' @examples
#' plot_counts_bar(c('Ross Pinetum', 'The Ramble'))
plot_counts_bar <- function(highlight = vector()) {
    counts_full <- ggplot(a) + 
            geom_bar(aes(x = reorder(sitename, Unique_Squirrel_ID), 
                         y = Unique_Squirrel_ID, 
                         fill = Unique_Squirrel_ID), 
                     stat = 'identity') + 

            coord_flip() +
            scale_fill_gradient(low = 'white', 
                                high = 'darkgreen', 
                                limits = c(0,125),
                                name = 'Count') +
            labs(title = 'Squirrel Distribution by Park Region', y = 'Squirrel Count', x = '') +
            theme_minimal() +
            theme(panel.grid.major.y = element_blank(), legend.position = c(0.8, 0.2), plot.title = element_text(hjust = 0.5))
    if (length(highlight) == 0) {
        counts_full
        } else {
            counts_full +
                 gghighlight(sitename %in% highlight, 
                             label_key = Unique_Squirrel_ID)
    }
}

plot_counts_bar()

When I run this code as-is (i.e. plot_counts_bar()), I get the correct output: Correct

But then when I run ggplotly(plots_counts_bar()), I get the following:

enter image description here

Other, similarly coded bar charts are translated to ggplotly fine, and I have tried removing the scale_fill_continuous, coord_flip, and highlight clauses but those don't seem to be the issue. Any help is appreciated!

Cari
  • 67
  • 5

1 Answers1

0

Hmm - could this be related to your browser/renderer as the code above works fine for me. I tried it in JupyterLab Did you also try in RStudio?

Running code

We also have the same versions:

R: 3.6.1 ggplot2: 3.2.1 plotly: 4.9.1

here is my full sessionInfo():

R version 3.6.1 (2019-07-05)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Catalina 10.15.1

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

locale:
[1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8

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

other attached packages:
[1] plotly_4.9.1  ggplot2_3.2.1

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.3        later_1.0.0       pillar_1.4.2      compiler_3.6.1   
 [5] base64enc_0.1-3   tools_3.6.1       zeallot_0.1.0     digest_0.6.23    
 [9] uuid_0.1-2        viridisLite_0.3.0 jsonlite_1.6      evaluate_0.14    
[13] lifecycle_0.1.0   tibble_2.1.3      gtable_0.3.0      pkgconfig_2.0.3  
[17] rlang_0.4.2       shiny_1.2.0       IRdisplay_0.7.0   crosstalk_1.0.0  
[21] yaml_2.2.0        IRkernel_1.0.1    repr_1.0.1        withr_2.1.2      
[25] dplyr_0.8.3       httr_1.4.1        htmlwidgets_1.5.1 vctrs_0.2.0      
[29] grid_3.6.1        tidyselect_0.2.5  glue_1.3.1        data.table_1.12.6
[33] R6_2.4.1          pbdZMQ_0.3-3      farver_2.0.1      purrr_0.3.3      
[37] tidyr_1.0.0       magrittr_1.5      promises_1.1.0    backports_1.1.5  
[41] scales_1.1.0      htmltools_0.4.0   assertthat_0.2.1  xtable_1.8-4     
[45] mime_0.7          colorspace_1.4-1  httpuv_1.5.2      labeling_0.3     
[49] lazyeval_0.2.2    munsell_0.5.0     crayon_1.3.4     

And here is my jupyter --version (in a terminal/console):

jupyter --version   
jupyter core     : 4.6.1
jupyter-notebook : 6.0.1
qtconsole        : 4.5.1
ipython          : 6.2.1
ipykernel        : 5.1.1
jupyter client   : 5.3.4
jupyter lab      : 1.2.3
nbconvert        : 5.5.0
ipywidgets       : 7.5.0
nbformat         : 4.4.0
traitlets        : 4.3.2
Firas
  • 516
  • 5
  • 10
  • 1
    I have the same setup as above, but it is just an issue in my local Jupyter lab environment. The plot renders fine when deployed locally using DashR. Thanks for checking it out! – Cari Dec 15 '19 at 17:47