R's plotly ggplotly removing legend from a geom_point

39 Views Asked by At

I'm generating a simple geom_point plot with R ggplot2:

library(dplyr)
library(ggplot2)
set.seed(1)
df <- matrix(runif(16, 0, 10), nrow = 4, ncol = 4, dimnames = list(paste0("R", 1:4), paste0("C",1:4))) %>%
  reshape2::melt()
ggplot(df, aes(x = Var2, y = Var1)) + 
  geom_point(aes(size = value), shape = 21, fill = "black", color = "black") +
  theme_minimal() + theme(axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x = element_text(angle = 45,vjust = 1, hjust = 1))

enter image description here

But when I wrap it with plotly::ggplotly the legend is gone: enter image description here

adding plotly::layout(showlegend = T) does't change it and neither does removing the theme_minimal nor the theme parts.

Any idea?

P.S., my ggplot2 and plotly packages are updated.

> sessionInfo()
R version 4.2.3 (2023-03-15)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS 14.3

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib

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

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

other attached packages:
[1] plotly_4.10.4 ggplot2_3.4.4 dplyr_1.1.2   Matrix_1.6-5 

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.10         plyr_1.8.8          pillar_1.9.0        compiler_4.2.3      BiocManager_1.30.20 tools_4.2.3         digest_0.6.31      
 [8] jsonlite_1.8.4      lifecycle_1.0.3     tibble_3.2.1        gtable_0.3.3        viridisLite_0.4.2   lattice_0.21-8      pkgconfig_2.0.3    
[15] rlang_1.1.1         cli_3.6.1           rstudioapi_0.14     crosstalk_1.2.0     yaml_2.3.7          fastmap_1.1.1       stringr_1.5.0      
[22] withr_2.5.0         httr_1.4.6          generics_0.1.3      vctrs_0.6.5         htmlwidgets_1.6.2   grid_4.2.3          tidyselect_1.2.0   
[29] glue_1.6.2          data.table_1.14.8   R6_2.5.1            fansi_1.0.4         sessioninfo_1.2.2   farver_2.1.1        purrr_1.0.1        
[36] tidyr_1.3.0         reshape2_1.4.4      magrittr_2.0.3      ellipsis_0.3.2      scales_1.2.1        htmltools_0.5.7     colorspace_2.1-0   
[43] labeling_0.4.2      utf8_1.2.3          stringi_1.7.12      lazyeval_0.2.2      munsell_0.5.0      
0

There are 0 best solutions below