将静态 ggplot2 图转换为交互式 ggplotly 图时,条形图呈现不正确

Bar plots render incorrectly when translating static ggplot2 graphs to interactive ggplotly graphs

我想为我在 ggplot2 中创建的绘图添加交互性。对于我创建的 4 个水平条形图中的 3 个,当我将绘图函数包装在 ggplotly() 中时,这工作正常。然而,对于最后一个,情节重新渲染不正确并绘制了一个奇怪的比例。我搜索了错误问题并找到了 this one 这可能是相关的,但我正在 运行 更新软件包的最新版本:

# 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()

当我按原样 运行 这段代码时(即 plot_counts_bar()),我得到了正确的输出:

但是当我 运行 ggplotly(plots_counts_bar()) 时,我得到以下信息:

其他类似编码的条形图可以很好地转换为 ggplot,我尝试删除 scale_fill_continuous、coord_flip 和突出显示子句,但这些似乎不是问题所在。感谢您的帮助!

嗯 - 这是否与您的 browser/renderer 有关,因为上面的代码对我来说工作正常。我在 JupyterLab 里试过了 你在 RStudio 里也试过吗?

我们也有相同的版本:

R: 3.6.1 ggplot2:3.2.1 剧情:4.9.1

这是我的全部 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     

这是我的 jupyter --version(在 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