Utilizing color, symbol and size

Intermediate Interactive Data Visualization with plotly in R

Adam Loy

Statistician, Carleton College

Scatterplot of national happiness against life expectancy.

Intermediate Interactive Data Visualization with plotly in R

World happiness data

dplyr::glimpse(happy)
Rows: 141
Columns: 11
$ country         <chr> "Afghanistan", "Albania", "Algeria", ...
$ happiness       <dbl> 2.661718, 4.639548, 5.248912, 6.039330, ...
$ region          <chr> "South Asia", "Central and Eastern Europe", ...
$ population      <dbl> 35530081, 2873457, 41318142, 44271041, ...
$ log.gdp         <dbl> 7.460144, 9.373718, 9.540244, 9.843519, ...
$ income          <fct> low, upper-middle, upper-middle, high, ...
$ life.expectancy <dbl> 52.33953, 69.05166, 65.69919, 67.53870, ...
$ social.support  <dbl> 0.4908801, 0.6376983, 0.8067539, 0.9066991, ...
$ freedom         <dbl> 0.4270109, 0.7496110, 0.4366705, 0.8319662, ...
$ generosity      <dbl> -0.106340349, -0.035140377, -0.194670126, -0.18629...
$ corruption      <dbl> 0.9543926, 0.8761346, 0.6997742, 0.8410525, ...
Intermediate Interactive Data Visualization with plotly in R

Glyph color

happy %>%
  plot_ly(x = ~life.expectancy, y = ~happiness) %>%
  add_markers(color = ~income)

Scatterplot of national happiness against life expectancy colored by income category.

Intermediate Interactive Data Visualization with plotly in R

Glyph symbol

happy %>%
  plot_ly(x = ~life.expectancy, y = ~happiness) %>%
  add_markers(symbol = ~income)

Scatterplot of national happiness against life expectancy. Both color and shape represent the country's income category.

Intermediate Interactive Data Visualization with plotly in R

Color based on a quantitative variable

happy %>%
  plot_ly(x = ~life.expectancy, y = ~happiness) %>%
  add_markers(color = ~population)

Scatterplot of national happiness against life expectancy colored by population.

Intermediate Interactive Data Visualization with plotly in R

Transformations

happy %>%
  plot_ly(x = ~life.expectancy, y = ~happiness) %>%
  add_markers(color = ~log10(population))

Scatterplot of national happiness against life expectancy colored by the logarithm of population.

Intermediate Interactive Data Visualization with plotly in R

Glyph size

happy %>%
  plot_ly(x = ~life.expectancy, y = ~happiness) %>%
  add_markers(size = ~population)

Scatterplot of national happiness against life expectancy. The size of the glyph corresponds to population.

Intermediate Interactive Data Visualization with plotly in R

Polishing labels

happy %>%
  plot_ly(
    x = ~life.expectancy, y = ~happiness,
    hoverinfo = "text",
    text = ~paste("Country: ", country,
                  "</br> Population: ", population)
  ) %>%
  add_markers(size = ~population) %>%
  layout(
    xaxis = list(title = "Healthy life expectancy"),
    yaxis = list(title = "National happiness score")
  )
Intermediate Interactive Data Visualization with plotly in R

GIF of hover interaction.

Intermediate Interactive Data Visualization with plotly in R

Let's practice!

Intermediate Interactive Data Visualization with plotly in R

Preparing Video For Download...