DEV Community

Cover image for Interactive or non-interactive visualization in Jupyter (R version)?
HongKee Moon
HongKee Moon

Posted on • Updated on

Interactive or non-interactive visualization in Jupyter (R version)?

OK, it's time to use R kernel in our jupyter notebook. Actually, this can be done easily by following the instructions in https://irkernel.github.io/installation/.
Then, let's check if our R kernel is installed well.

library(ggplot2)

p <- qplot(wt, mpg, data=mtcars, geom=c('point', 'smooth'),
     method='lm', formular=y~x, color=cyl,
     main='Regression of MPG on Weight',
     xlab='Weight', ylab='Miles per Gallon')

p

You probably see the cover picture after running the R cell which is a non-interactive chart, named ggplot2.

Non-interactive: ggplot2 (https://ggplot2.tidyverse.org)

ggplot2 is widely used for data scientists because it is fast, simple, customizable and scalable, which is a part of tidyverse. Tidyverse is an ecosystem of packages designed with common APIs and a shared philosophy for data science. You can check https://www.tidyverse.org/.

When you want to make any plots from ggplot2 interactive, you can use ggplotly() function provided by plotly.

library(plotly)

embed_notebook(ggplotly(p), height="500")

Do not forget to call embed_notebook() function. The above script changes the non-interactive chart to an interactive one.

Interactive: plotly (https://plot.ly/r/)

Plotly provides two different methods for scatter plot. One is based on svg and the other is based on webgl. We can convert our svg chart to webgl with toWebGL() function at anytime. Please, refer https://plot.ly/r/webgl-vs-svg/.

# install.packages("babynames", repos="http://cran.r-project.org")

library(babynames)
nms <- filter(babynames, name %in% c("Sam", "Jamie"))
p <- ggplot(nms) + 
  geom_line(aes(year, prop, color = sex, linetype = name))

embed_notebook(ggplotly(p, dynamicTicks = TRUE) %>%
  rangeslider() %>%
  layout(hovermode = "x"))

The above code produces the time series how frequently "Sam" and "Jamie" are used as names for male and female.

Alt Text

The gist included in this article shows several cells how to customize plotly chart and how to highlight the data cross linked with sub plots.

Finally, the code below shows a heatmap with dendrogram thanks to heatmaply.

library(heatmaply)
library(dplyr)

mtscaled <- as.matrix(scale(mtcars))
heatmaply(mtscaled, k_row = 3, k_col = 2) %>% embed_notebook(height="800")

Alt Text

Happy coding!

Top comments (0)