Plot interaktif dengan plotly

Membangun Dashboard dengan shinydashboard

Png Kee Seng

Researcher

Contoh: listing Airbnb di London

  • Bayangkan kita bekerja di agen tur berbasis di London
  • Ada kolaborasi dengan 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 ...
...
Membangun Dashboard dengan shinydashboard

Mulai dengan plotly

  • Impor pustaka plotly

    library(plotly)
    
  • Objek plotly dibuat dengan menempatkan objek ggplot dalam ggplotly()

    ggplotly(<ggplot object>)
    
Membangun Dashboard dengan shinydashboard

Plot gelembung dengan ggplot

  • Buat plot gelembung dan simpan sebagai bubble
    • Tampilkan hubungan antara rata-rata price dan availability_365
    • Warna gelembung berbeda untuk tiap room_type
    • Ukuran gelembung proporsional dengan jumlah listing
    • Tambahkan lapisan untuk mempercantik
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()
Membangun Dashboard dengan shinydashboard

Plot gelembung dengan ggplot, dirender

Plot gelembung biasa yang dibuat dengan ggplot.

Membangun Dashboard dengan shinydashboard

Plot gelembung interaktif dengan plotly

  • Bungkus bubble dalam ggplotly()
ggplotly(bubble)

Plot gelembung plotly interaktif.

Membangun Dashboard dengan shinydashboard

Boxplot horizontal dengan plotly

  • Beberapa plot tidak dapat dikonversi dengan benar

  • Buat serangkaian boxplot horizontal

    • Jelaskan distribusi availability_365 terhadap room_type
  • Konversi dengan ggplotly() tidak berhasil
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)

Boxplot horizontal dibuat dengan ggplot.

Boxplot horizontal yang dikonversi dengan ggplotly.

Membangun Dashboard dengan shinydashboard

Boxplot vertikal dengan plotly

  • Tukar x dan y di aes()
  • Konversi plotly kini akan berfungsi
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)

Boxplot vertikal dengan ggplot.

Boxplot vertikal dengan ggplotly.

Membangun Dashboard dengan shinydashboard

Memperbaiki boxplot horizontal dengan plotly

  • Bagaimana jika kita ingin boxplot horizontal?
    • Tambahkan coord_flip()
  • Kita juga dapat memakai facet_wrap()
    • Setiap panel harus cukup besar
ggplotly(boxV + coord_flip())
ggplotly(boxV + coord_flip() + 
    facet_wrap(~neighbourhood))

Boxplot plotly yang diputar.

Boxplot plotly berfasil.

Membangun Dashboard dengan shinydashboard

Ayo berlatih!

Membangun Dashboard dengan shinydashboard

Preparing Video For Download...