interactive ggplot with tooltip using plotly

[This article was first published on Category R on Roel's R-tefacts, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

A quick Random R thing I use a lot, recently learned, and I want you to know it too.

In this post I’ll show you how to make a quick interactive plot with ggplot and plotly, so that values are displayed when you hover your mouse over it. Why would you want this? If you are exploring the data, you want some quick insights into which values are where.

We first load the libraries and dataset.

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
mtcars2 <- mtcars # I know, you've seen this set before
mtcars2$car <- rownames(mtcars) 
mtcars2$cyl <- as.factor(mtcars$cyl)

Then we plot a simple plot (non interactive). Also note that ggplot2 doesn’t know what to do with name = car, but doesn’t complain either

p1<- ggplot(mtcars2, aes(mpg, wt, name = car, color = cyl)) + geom_point()
p1

Now me make it interactive, on mouseover we want to see something. Everything we added to the ggplot aesthetics are now available under the tooltip (you can change that).

ggplotly(p1)

And that’s it. It’s just a simple trick. Wrap your ggplot in ggplotly and it works.

State of the machine

At the moment of creation (when I knitted this document ) this was the state of my machine: click here to expand

sessioninfo::session_info()
## ─ Session info ──────────────────────────────────────────────────────────
##  setting  value                       
##  version  R version 3.5.1 (2018-07-02)
##  os       macOS High Sierra 10.13.6   
##  system   x86_64, darwin15.6.0        
##  ui       X11                         
##  language (EN)                        
##  collate  en_US.UTF-8                 
##  tz       Europe/Amsterdam            
##  date     2018-09-13                  
## 
## ─ Packages ──────────────────────────────────────────────────────────────
##  package     * version date       source                               
##  assertthat    0.2.0   2017-04-11 CRAN (R 3.5.0)                       
##  backports     1.1.2   2017-12-13 CRAN (R 3.5.0)                       
##  bindr         0.1.1   2018-03-13 CRAN (R 3.5.0)                       
##  bindrcpp      0.2.2   2018-03-29 CRAN (R 3.5.0)                       
##  blogdown      0.8     2018-07-15 CRAN (R 3.5.0)                       
##  bookdown      0.7     2018-02-18 CRAN (R 3.5.0)                       
##  clisymbols    1.2.0   2017-05-21 CRAN (R 3.5.0)                       
##  colorspace    1.3-2   2016-12-14 CRAN (R 3.5.0)                       
##  crayon        1.3.4   2017-09-16 CRAN (R 3.5.0)                       
##  crosstalk     1.0.0   2016-12-21 CRAN (R 3.5.0)                       
##  data.table    1.11.4  2018-05-27 CRAN (R 3.5.0)                       
##  digest        0.6.15  2018-01-28 CRAN (R 3.5.0)                       
##  dplyr         0.7.6   2018-06-29 CRAN (R 3.5.1)                       
##  evaluate      0.11    2018-07-17 CRAN (R 3.5.0)                       
##  ggplot2     * 3.0.0   2018-07-03 CRAN (R 3.5.0)                       
##  glue          1.3.0   2018-09-04 Github (tidyverse/glue@4e74901)      
##  gtable        0.2.0   2016-02-26 CRAN (R 3.5.0)                       
##  htmltools     0.3.6   2017-04-28 CRAN (R 3.5.0)                       
##  htmlwidgets   1.2.1   2018-08-06 Github (ramnathv/htmlwidgets@29ca4f7)
##  httpuv        1.4.5   2018-07-19 CRAN (R 3.5.0)                       
##  httr          1.3.1   2017-08-20 CRAN (R 3.5.0)                       
##  jsonlite      1.5     2017-06-01 CRAN (R 3.5.0)                       
##  knitr         1.20    2018-02-20 CRAN (R 3.5.0)                       
##  labeling      0.3     2014-08-23 CRAN (R 3.5.0)                       
##  later         0.7.3   2018-06-08 CRAN (R 3.5.0)                       
##  lazyeval      0.2.1   2017-10-29 CRAN (R 3.5.0)                       
##  magrittr      1.5     2014-11-22 CRAN (R 3.5.0)                       
##  mime          0.5     2016-07-07 CRAN (R 3.5.0)                       
##  munsell       0.5.0   2018-06-12 CRAN (R 3.5.0)                       
##  pillar        1.3.0   2018-07-14 CRAN (R 3.5.0)                       
##  pkgconfig     2.0.1   2017-03-21 CRAN (R 3.5.0)                       
##  plotly      * 4.8.0   2018-07-20 CRAN (R 3.5.1)                       
##  plyr          1.8.4   2016-06-08 CRAN (R 3.5.0)                       
##  promises      1.0.1   2018-04-13 CRAN (R 3.5.0)                       
##  purrr         0.2.5   2018-05-29 CRAN (R 3.5.0)                       
##  R6            2.2.2   2017-06-17 CRAN (R 3.5.0)                       
##  Rcpp          0.12.18 2018-07-23 CRAN (R 3.5.0)                       
##  rlang         0.2.2   2018-08-16 cran (@0.2.2)                        
##  rmarkdown     1.10    2018-06-11 CRAN (R 3.5.0)                       
##  rprojroot     1.3-2   2018-01-03 CRAN (R 3.5.0)                       
##  scales        1.0.0   2018-08-09 cran (@1.0.0)                        
##  sessioninfo   1.0.0   2017-06-21 CRAN (R 3.5.0)                       
##  shiny         1.1.0   2018-05-17 CRAN (R 3.5.0)                       
##  stringi       1.2.4   2018-07-20 CRAN (R 3.5.0)                       
##  stringr       1.3.1   2018-05-10 CRAN (R 3.5.0)                       
##  tibble        1.4.2   2018-01-22 CRAN (R 3.5.0)                       
##  tidyr         0.8.1   2018-05-18 CRAN (R 3.5.0)                       
##  tidyselect    0.2.4   2018-02-26 CRAN (R 3.5.0)                       
##  viridisLite   0.3.0   2018-02-01 CRAN (R 3.5.0)                       
##  withr         2.1.2   2018-03-15 CRAN (R 3.5.0)                       
##  xfun          0.3     2018-07-06 CRAN (R 3.5.0)                       
##  xtable        1.8-2   2016-02-05 CRAN (R 3.5.0)                       
##  yaml          2.2.0   2018-07-25 CRAN (R 3.5.0)

To leave a comment for the author, please follow the link and comment on their blog: Category R on Roel's R-tefacts.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)