使用 plotly 制作交互式图

使用 shinydashboard 构建仪表板

Png Kee Seng

Researcher

示例:伦敦的 Airbnb 房源

  • 设想我们在伦敦的一家旅游机构工作
  • 与 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 ...
...
使用 shinydashboard 构建仪表板

开始使用 plotly

  • 导入 plotly 库

    library(plotly)
    
  • 通过将 ggplot 对象放入 ggplotly() 来创建 plotly 对象

    ggplotly(<ggplot object>)
    
使用 shinydashboard 构建仪表板

使用 ggplot 的气泡图

  • 创建气泡图并保存为 bubble
    • 展示平均 priceavailability_365 的关系
    • room_type 使用不同颜色
    • 气泡大小与房源数量成正比
    • 添加美化层
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()
使用 shinydashboard 构建仪表板

使用 ggplot 的气泡图(渲染)

使用 ggplot 创建的常规气泡图。

使用 shinydashboard 构建仪表板

使用 plotly 的交互式气泡图

  • bubble 包裹在 ggplotly()
ggplotly(bubble)

一个交互式 plotly 气泡图。

使用 shinydashboard 构建仪表板

使用 plotly 的水平箱线图

  • 有些图无法正确转换

  • 创建一组水平箱线图

    • room_type 描述 availability_365 的分布
  • 使用 ggplotly() 转换失败
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)

使用 ggplot 创建的水平箱线图。

使用 ggplotly 转换后的水平箱线图。

使用 shinydashboard 构建仪表板

使用 plotly 的垂直箱线图

  • aes() 中交换 xy
  • 现在可用 plotly 转换
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)

使用 ggplot 的垂直箱线图。

使用 ggplotly 的垂直箱线图。

使用 shinydashboard 构建仪表板

修复在 plotly 中的水平箱线图

  • 若需要水平箱线图怎么办?
    • 添加 coord_flip()
  • 也可使用 facet_wrap()
    • 确保每个分面足够大
ggplotly(boxV + coord_flip())
ggplotly(boxV + coord_flip() + 
    facet_wrap(~neighbourhood))

翻转后的 plotly 垂直箱线图。

分面 plotly 箱线图。

使用 shinydashboard 构建仪表板

Passons à la pratique !

使用 shinydashboard 构建仪表板

Preparing Video For Download...