Site icon R-bloggers

Tufte style visualizations in R using Plotly

[This article was first published on R – Modern Data, 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.
This post is inspired by Lukasz Piwek’s awesome Tufte in R post. We’ll try to replicate Tufte’s visualization practices in R using Plotly. You can read more about Edward Tufte here. One easy way to replicate the graphs showcased on Lukasz’s blog would be to simply use ggplotly() on his ggplot2() code. We’ll use plot_ly() instead.

Minimal Line Plot

library(plotly)

x <- 1967:1977
y <- c(0.5,1.8,4.6,5.3,5.3,5.7,5.4,5,5.5,6,5)
hovertxt <- paste("Year: ", x, "<br>", "Exp: ", y)
  
plot_ly(x = x, y = y, mode = "line + markers", name = "",
        marker = list(color = "#737373", size = 8), 
        line = list(width = 1), showlegend = F, hoverinfo = "text", text = hovertxt) %>% 
  
  add_trace(x = c(1966.75, 1977.25), y = c(5, 5), 
            mode = "lines", line = list(dash = "5px", color = "#737373"), 
            showlegend = F, hoverinfo = "none") %>% 
  
  add_trace(x = c(1966.75, 1977.25), y = c(6, 6), 
            mode = "lines", line = list(dash = "5px", color = "#737373"), 
            showlegend = F, hoverinfo = "none") %>% 
  
  layout(xaxis = list(title = "", showgrid = F, tickmode = "array",  
                      type = "linear", autorange = F, range = c(1966.75, 1977.25),
                      tickvals = x, 
                      tick = list(family = "serif", size = 10), ticks = "outside"),
         
         yaxis = list(title = "", showgrid = F, tickmode = "array",  
                      type = "linear", 
                      tickvals = 1:6, ticktext = paste0("$", c(300, 320, 340, 360, 380, 400)), 
                      tick = list(family = "serif", size = 10), ticks = "outside"),
         
         margin = list(r = 20), 
         
         annotations = list(
           list(xref = "x", yref = "y", x = 1977.25, y = 5.5, 
                text = "5%", showarrow = F, ax = 0, ay = 0),
           
           list(xref = "x", yref = "y", x = 1976, y = 1.5, align = "right",
                text = "Per capita<br>budget expenditures<br>in constant dollars", 
                showarrow = F, ax = 0, ay = 0)
         ))

Range-frame (or quartile-frame) scatterplot

library(plotly)

x <- mtcars$wt
y <- mtcars$mpg
hovertxt <- paste("Weight:", x, "<br>", "Miles per gallon: ", y)

plot_ly(x = x, y = y, mode = "markers", marker = list(color = "#737373"), 
        hoverinfo = "text", text = hovertxt) %>% 
  layout(xaxis = list(title = "Car weight (lb/1000)", title = list(family = "serif"), 
                      showgrid = F, tickmode = "array",  
                      tickvals = summary(x), ticktext = round(summary(x), 1),
                      tick = list(family = "serif", size = 10), ticks = "outside"),
         
         yaxis = list(title = "Miles per gallon of fuel", title = list(family = "serif"),
                      showgrid = F, tickmode = "array",  
                      tickvals = summary(y), ticktext = round(summary(y), 1),
                      tick = list(family = "serif", size = 10), ticks = "outside"))

Dot-dash (Rug) plot

library(plotly)
library(dplyr)

x <- mtcars$wt
y <- mtcars$mpg
hovertxt <- paste("Weight:", x, "<br>", "Miles per gallon: ", y)


ds <- data.frame(x, y, labelsx = round(x, 0), labelsy = round(y,0))
ds <- ds %>% arrange(x)
ds$labelsx <- c(rep("", 7), 2, 
                rep("", 12), 3,
                rep("", 7), 4,
                rep("", 2), 5)

ds <- ds %>% arrange(y)
ds$labelsy <- c(rep("", 1), 10, 
                rep("", 5), 15,
                rep("", 8), 20,
                rep("", 8), 26,
                rep("", 5), 34)


plot_ly(ds, x = x, y = y, mode = "markers", marker = list(color = "#737373"), 
        hoverinfo = "text", text = hovertxt) %>% 
  layout(xaxis = list(title = "Car weight (lb/1000)", title = list(family = "serif"), 
                      showgrid = F,tickmode = "array",
                      tickvals = x, ticktext = labelsx, ticklen = 10,
                      tick = list(family = "serif", size = 10), ticks = "outside"),
         
         yaxis = list(title = "Miles Per Gallon of Fuel", title = list(family = "serif"), 
                      showgrid = F,tickmode = "array",  
                      tickvals = y, ticktext = labelsy, ticklen = 10,
                      tick = list(family = "serif", size = 10), ticks = "outside"))

Minimal Boxplot

This one needs a little bit of work. Since geom_tufteboxplot() is not yet supported, using ggplotly() won’t work either.
library(plotly)

# Empty plot
p <- plot_ly()

vec <- sort(unique(quakes$mag))

# Each mean (dot) and quartile (line - segment) will have to be added as a separate trace

for(i in vec){
  summ <- boxplot.stats(subset(quakes, mag == i)$stations)$stats
  hovertxt <- paste("Mean:", summ[3], "<br>",
                    "IQR:", IQR(subset(quakes, mag == i)$stations))

  p <- add_trace(p, x = i, y = summ[3], mode = "markers", hoverinfo = "text", text = hovertxt,
                 marker = list(color = "#737373", size = 6), evaluate = T, showlegend = F)
  
  p <- add_trace(p, x = c(i, i), y = c(summ[1], summ[2]), mode = "lines", hoverinfo = "none", 
                 marker = list(color = "#737373"),
                 line = list(width = 1), evaluate = T, showlegend = F)
  
  p <- add_trace(p, x = c(i, i), y = c(summ[4], summ[5]), mode = "lines", hoverinfo = "none",
                 marker = list(color = "#737373"),
                 line = list(width = 1), evaluate = T, showlegend = F)
  
  
}

# Layout options
p <- layout(p, 
            xaxis = list(showgrid = F, nticks = length(vec)),
            yaxis = list(showgrid = F),
            annotations = list(
              list(xanchor = "left", x = 4, y = 120, 
                   text = "Number of stations<br>reporting Richter Magnitude<br>of Fiji earthquakes (n=1000)",
                   align = "left",
                   showarrow = F, ax = 0, ay = 0)))
p

Minimal Barchart

library(psych)
library(reshape2)

ds <- melt(colMeans(msq[,c(2,7,34,36,42,43,46,55,68)],na.rm = T)*10)
ds$trait <- rownames(ds)
hovertxt <- paste(ds$trait, ":", round(ds$value,3))

plot_ly(ds, x = 1:nrow(ds), y = value, type = "bar", marker = list(color = "#737373"), 
        hoverinfo = "text", showlegend = F, text = hovertxt) %>% 
  
  add_trace(x = c(0.4, 9.6, NA, 0.4, 9.6, NA, 0.4, 9.6, NA, 0.4, 9.6, NA, 0.4, 9.6), 
            y = c(1, 1, NA, 2, 2, NA, 3, 3, NA, 4, 4, 5, 5), mode = "lines", 
            marker = list(color = "white"), showlegend = F) %>% 
  
  layout(xaxis = list(title = "", tickmode = "array", tickvals = 1:nrow(ds), ticktext = trait,
                      tick = list(family = "serif", size = 10)),
         yaxis = list(title = "", showgrid = F), 
         annotations = list(
           list(x = 1, xanchor = "left", y = 6, showarrow = F, ax = 0, ay = 0, align = "left",
                text = "Average scores<br>on negative emotion traits<br>from 3896 participants<br>(Watson et al., 1988)")))

To leave a comment for the author, please follow the link and comment on their blog: R – Modern Data.

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.