Interactive plots with plotly

Building Dashboards with shinydashboard

Png Kee Seng

Researcher

An example: Airbnb listings in London

  • Imagine we are working at a tour agency based in London
  • A collaboration was set up with Airbnb
    library(tidyverse)
    listings <- read_csv("../data/listings.csv")
    
...
 $ host_name                     : chr [1:69351] "Susie" "Alina" "Luca" "Simon" ...
 $ neighbourhood_group           : logi [1:69351] NA NA NA NA NA NA ...
 $ neighbourhood                 : chr [1:69351] "Tower Hamlets" "Islington" "Tower Hamlets" "Islington" ...
 $ latitude                      : num [1:69351] 51.5 51.6 51.5 51.5 51.5 ...
 $ longitude                     : num [1:69351] -0.054 -0.1127 -0.0743 -0.1048 -0.2 ...
 $ room_type                     : chr [1:69351] "Private room" "Private room" "Entire home/apt" "Private room" ...
 $ price                         : num [1:69351] 55 50 90 180 297 75 204 379 90 30 ...
 $ minimum_nights                : num [1:69351] 3 1 5 4 14 3 2 4 3 7 ...
 $ number_of_reviews             : num [1:69351] 65 30 42 493 6 89 581 51 104 32 ...
 $ last_review                   : Date[1:69351], format: "2016-06-10" "2022-07-15" "2022-01-04" "2022-09-02" ...
 $ reviews_per_month             : num [1:69351] 0.47 0.2 0.34 3.59 0.05 0.57 4.26 0.36 0.86 0.29 ...
 $ calculated_host_listings_count: num [1:69351] 1 2 1 5 1 1 1 5 1 1 ...
 $ availability_365              : num [1:69351] 74 343 222 236 180 70 193 249 318 251 ...
...
Building Dashboards with shinydashboard

Getting started with plotly

  • Import the plotly library

    library(plotly)
    
  • A plotly object is defined by placing a ggplot object within ggplotly()

    ggplotly(<ggplot object>)
    
Building Dashboards with shinydashboard

Bubble plot with ggplot

  • Let's make a bubble plot and save it as bubble
    • Show relationship between average price and availability_365
    • Differently colored bubble for each room_type
    • Size of bubble is proportional to the number of listings
    • Add in additional layers for beautification
bubble <- listings %>%

group_by(room_type, neighbourhood) %>% summarise(avg_price = mean(price, na.rm=TRUE), avg_availability = mean(availability_365, na.rm=TRUE),
Count = n(), .groups = "drop") %>%
ggplot(aes(x = avg_price, y = avg_availability,
color = room_type,
size = Count)) +
geom_point() +
labs(x = "Average price", y = "Average availability", color = "Room type") + theme_classic()
Building Dashboards with shinydashboard

Bubble plot with ggplot, rendered

A regular bubble plot that was created with ggplot.

Building Dashboards with shinydashboard

Interactive bubble plot with plotly

  • Encase bubble in ggplotly()
ggplotly(bubble)

An interactive plotly bubble plot.

Building Dashboards with shinydashboard

Horizontal boxplots with plotly

  • Some plots cannot be converted correctly

  • Create a series of horizontally oriented boxplots

    • Describe availability_365 distribution with respect to room_type
  • Conversion with ggplotly() does not work
boxH <- listings %>%

ggplot(aes(x = availability_365, y=room_type)) +
geom_boxplot() +
labs(x="Availbility (out of 365 days)", y="Room type") + theme_classic()
ggplotly(boxH)

Horizontally oriented boxplots created with ggplot.

Horizontally oriented boxplots converted using ggplotly.

Building Dashboards with shinydashboard

Vertical boxplots with plotly

  • Switch x and y in aes()
  • Plotly conversion will now work
boxV <- listings %>%

ggplot(aes(y = availability_365, x=room_type)) + geom_boxplot() + labs(y="Availbility (out of 365 days)", x="Room type") + theme_classic()
ggplotly(boxV)

Vertically oriented boxplots with ggplot.

Vertically oriented boxplots with ggplotly.

Building Dashboards with shinydashboard

Fixing horizontal boxplots with plotly

  • What if we want horizonal box plots?
    • Add coord_flip()
  • We can also use facet_wrap()
    • Each panel must be large enough
ggplotly(boxV + coord_flip())
ggplotly(boxV + coord_flip() + 
    facet_wrap(~neighbourhood))

Flipped vertically oriented plotly boxplots.

Faceted plotly boxplots.

Building Dashboards with shinydashboard

Let's practice!

Building Dashboards with shinydashboard

Preparing Video For Download...