library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

The R package plotly makes it easy to turn your ggplots into interactive plots. This can be super useful for data exploration, especially when plots are crammed with lots of data.

data("mtcars")

gp <- ggplot(mtcars, aes(x=disp, y=mpg, color=gear)) +
  geom_point() +
  theme_bw()
print(gp) 

Now let’s make it interactive!

ggp <- plotly::ggplotly(gp)
ggp

ggplotly works on most ggplot objects (though to varying degrees of success with some more complex plot types).

bp <- ggplot(mtcars, aes(x=gear, y=mpg, fill=gear)) +
  geom_bar(stat="identity") +
  theme_bw()

print(bp) 

gbp <- plotly::ggplotly(bp)
gbp

Session Info

utils::sessionInfo()
## R version 4.1.0 (2021-05-18)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.2 LTS
## 
## Matrix products: default
## BLAS/LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.8.so
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=C             
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] 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] dplyr_1.0.7       plotly_4.9.4.9000 ggplot2_3.3.5    
## 
## loaded via a namespace (and not attached):
##  [1] highr_0.9         bslib_0.2.5.1     compiler_4.1.0    pillar_1.6.1     
##  [5] jquerylib_0.1.4   tools_4.1.0       digest_0.6.27     viridisLite_0.4.0
##  [9] jsonlite_1.7.2    evaluate_0.14     lifecycle_1.0.0   tibble_3.1.2     
## [13] gtable_0.3.0      pkgconfig_2.0.3   rlang_0.4.11      DBI_1.1.1        
## [17] crosstalk_1.1.1   yaml_2.2.1        xfun_0.24         httr_1.4.2       
## [21] withr_2.4.2       stringr_1.4.0     knitr_1.33        htmlwidgets_1.5.3
## [25] generics_0.1.0    sass_0.4.0        vctrs_0.3.8       grid_4.1.0       
## [29] tidyselect_1.1.1  data.table_1.14.0 glue_1.4.2        R6_2.5.0         
## [33] fansi_0.5.0       rmarkdown_2.9     farver_2.1.0      tidyr_1.1.3      
## [37] purrr_0.3.4       magrittr_2.0.1    scales_1.1.1      ellipsis_0.3.2   
## [41] htmltools_0.5.1.1 assertthat_0.2.1  colorspace_2.0-2  labeling_0.4.2   
## [45] utf8_1.2.1        stringi_1.7.3     lazyeval_0.2.2    munsell_0.5.0    
## [49] crayon_1.4.1