Introduction to Plotly

Andy Grogan-Kaylor

September 02, 2021

Background

There are an ever increasing number of animated, or interactive, graphical libraries for R. Choosing which library to use, and which library will be persistent and stable over time is a challenge.

plotly is a JavaScript graphing library that creates graphics for the web.

plotly for R is one of the easiest, and most richly featured libraries to use for creating graphics on the web.

install.packages("plotly")

The Best Place to Start With Plotly Is ggplotly()

library(ggplot2) # grammar of graphics

library(ggthemes) # beautiful additional themes and colors

library(plotly) # interactive graphs

library(dplyr) # data wrangling

load("WorldBankData.RData") # load the data

Construct a Simple ggplot

Remember all ggplots start simple, and then are tweaked…

# NB, I am mapping the plot into an object
# this is a tremendously useful idea, and will come in handy...

p0 <- ggplot(mydata, # the data I am using
            aes(x = GDP, # how I am graphing it
                y = life_expectancy, # NB I'm 'doubling up'
                color = life_expectancy)) + # on dimensions 
  geom_point() + # the geometries I want to use to graph
  geom_smooth()

# this graph needs better labels and titles

p0 # I am replaying the object, i.e. the plot

Construct a More Complicated ggplot

# NB, I am mapping the plot into an object
# this is a tremendously useful idea, and will come in handy...

p1 <- ggplot(mydata, # the data I am using
            aes(x = GDP, # how I am graphing it
                y = life_expectancy, # NB I'm 'doubling up'
                color = life_expectancy)) + # on dimensions 
  scale_color_gradient(low = "white", 
                       high = "red") + # CONTINUOUS palette
  geom_point() + # the geometries I want to use to graph
  geom_smooth()

# this graph needs better labels and titles

p1 # I am replaying the object, i.e. the plot

plotly-etize it

Mouse over the plot to see the interactivity.

ggplotly(p1)

Piping

Pipes %>% connect pieces of a command e.g. data to a graph command.

# just to make sure we have %>%'s

library(dplyr) # data wrangling 

Use dplyr to remove “Aggregates”

mydata <- mydata %>% filter(region != "Aggregates")

Use plotly syntax

Plotly Has Similar Logic to ggplot.

# mapping plot into an object
# to create the graph, connecting 2 components with a pipe

p2 <- mydata %>% # pipes connect the pieces
  plot_ly(x = ~GDP, # dimensions of the data we are using
          y = ~life_expectancy, # NB the '~'
          color = ~life_expectancy, # double counting a dimension
          colors = c("red", "blue")) # specifying color

p2 # replay the graph

3D Scatterplot

plotly does not have every geometry that ggplot2 has, and vice versa.

Most notably, a smoother geometry seems to be missing.

Note also, the cryptic (to me) use of (embedded) lists to style the graph.

A 3D Graph!

p3 <- mydata %>% # pipe connects data to plot
  plot_ly(x = ~GDP, # dimensions of the data we are using
          z = ~life_expectancy, # NB the '~'
          y = ~Gini,
          color = ~life_expectancy,
          colors = c("red", "blue"), 
          text = ~country_name) %>% # pipe connects to layout
  layout(title = 'Life Expectancy by GDP and Gini',
         scene = list(xaxis = list(title = 'GDP'),
                     yaxis = list(title = 'Gini Inequality Coefficient'),
                     zaxis = list(title = 'Life Expectancy')))

p3 # replay the graph

Let’s Think About the Idea of Dimension

On the previous 3D graphs, we had 3 dimensions of GDP, life expectancy and Gini coefficient of inequality.

Do the preceding 3D graph, or the 2D graph on the next slide do a better job of representing multiple dimensions? What are the best ways of representing additional dimensions of data?

A 2D Version With Some of the Same Style Elements

p4 <- mydata %>% # pipe connects data to plot
  plot_ly(x = ~GDP, # dimensions of the data we are using
          y = ~life_expectancy, # NB the '~'
          color = ~life_expectancy, 
          colors = c("red", "blue"), 
          text = ~country_name) %>% # pipe connects to layout
  layout(title = 'Life Expectancy by GDP',
         xaxis = list(title = 'GDP'),
         yaxis = list(title = 'Life Expectancy'))

p4 # replay the graph

A Bar Graph!

Because plotly does not have some features of ggplot, we need to use dplyr to manage our data (summarize scores) before sending to plotly.

mydata %>% # use mydata
  group_by(region) %>% # group by region
  filter(!is.na(life_expectancy)) %>% # filter on non-missing
  summarise(meanLIFE = mean(life_expectancy)) %>% # get averages
  plot_ly(x = ~region, # x is region
          y = ~meanLIFE) # y is MEAN life expectancy

A Map!!

plotly relies on ISO standard codes for country names in order to make maps. plotly can also use State abbreviations, and latitudes and longitudes.

mymap <- mydata %>% 
  filter(year == 2015) %>% # use dplyr to filter on year
  plot_geo(locations = ~iso3c, # ISO3 Country code
           color = ~life_expectancy,
           z = ~life_expectancy,
           text = ~country_name) %>% 
  layout(title = "Countries by Life Expectancy in 2015", 
         geo = list(showland = FALSE,
                    showcountries = TRUE)) %>%
  colorbar(title = 'life expectancy') 
         
mymap # replay mymap

A Globe!!!

myglobe <- mydata %>% 
  filter(year == 2015) %>% 
  plot_geo(locations = ~iso3c,
           color = ~life_expectancy,
           z = ~life_expectancy,
           text = ~country_name) %>% 
  layout(title = "Countries by Life Expectancy in 2015", 
         geo = list(showland = FALSE,
                    showcountries = TRUE,
                    projection = list(type = 'orthographic',
                                      rotation = list(lon = -30,
                                                      lat = 10,
                                                      roll = 0)))) %>%
  colorbar(title = 'life expectancy') 


myglobe

Questions?

Explore!!!

https://plot.ly/r/