NB: Plotly and GGPlotly

Programming for Data Science

Related to GGPlot are two other visualization libraries that are worth looking into.

Plotly is a cross platform visualization library that is great for making interactive plots.

And GGPlotly provides a convenient way to translate GGPlot plots into Ploty plots.

Let quickly take a look at these.

First, lets install the Tidyverse and turn off warnings.

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.3     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ ggplot2   3.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.0
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
options(warn = -1) # Turn off warnings

Plotly in R

Plotly runs on R, Python, JavaScript, and Matlab.

It uses the same model in each language, so learning it in R can help you use it in these other languages.

We first install it and then import it.

# install.packages("plotly")
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

Here is a list of the kinds of plots you can create in Plotly:

Types of plots

'bar', 'barpolar', 'box', 'candlestick', 'carpet', 'choropleth', 'choroplethmapbox', 'cone', 'contour', 'contourcarpet', 'densitymapbox', 'funnel', 'funnelarea', 'heatmap', 'heatmapgl', 'histogram', 'histogram2d', 'histogram2dcontour', 'icicle', 'image', 'indicator', 'isosurface', 'mesh3d', 'ohlc', 'parcats', 'parcoords', 'pie', 'pointcloud', 'sankey', 'scatter', 'scatter3d', 'scattercarpet', 'scattergeo', 'scattergl', 'scattermapbox', 'scatterpolar', 'scatterpolargl', 'scatterternary', 'splom', 'streamtube', 'sunburst', 'surface', 'table', 'treemap', 'violin', 'volume', 'waterfall'

Let’s create a scatter plot.

Note how we just specify the type and set the relevant arguments. Also note also the ~ prefix to feature names.

fig1 <- mpg %>%
    plot_ly(
        x         = ~hwy, 
        y         = ~cty, 
        type      = 'scatter', 
        size      = ~displ, 
        color     = ~class,
        hovertext = ~manufacturer,
        mode      = "markers",
        height    = 500, width = 750)
fig1

Here we visualize the iris data set.

fig2 <- iris %>%
    plot_ly(
        x         = ~Petal.Length, 
        y         = ~Petal.Width, 
        size      = ~Sepal.Length, 
        color     = ~Sepal.Width,
        symbol    = ~Species,
        type      = "scatter", 
        mode      = "markers",
        height    = 500, width = 750)
fig2

GGPlotly

GGPlotly by Plotly lets you can convert your GGPlot figures into interactive ones powered by the Plotly library.

It works by calling ggplotly() with no argument right after a GGPlot definition.

Here’s a quick example.

df <- data.frame(
    x = rnorm(100, mean = 0, sd = 1),
    y = rnorm(100, mean = 5, sd = 10)
)
df %>% ggplot() + 
    aes(x, y) +
    geom_point()

ggplotly()
ggplotly(height=500, width=500)

You also pass it a plot that was assigned to a variable.

In both cases, you can then add ploty features to your graph.

p <- df %>% ggplot() + 
    aes(x, y) +
    geom_point(color='red')
ggplotly(p, height=500, width=500)