Gráficos interativos com plotly

Construindo dashboards com shinydashboard

Png Kee Seng

Researcher

Exemplo: anúncios do Airbnb em Londres

  • Imagine que trabalhamos em uma agência de turismo em Londres
  • Fizemos uma parceria com o 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 ...
...
Construindo dashboards com shinydashboard

Primeiros passos com plotly

  • Importe a biblioteca plotly

    library(plotly)
    
  • Um objeto plotly é definido envolvendo um objeto ggplot em ggplotly()

    ggplotly(<ggplot object>)
    
Construindo dashboards com shinydashboard

Gráfico de bolhas com ggplot

  • Vamos criar um gráfico de bolhas e salvar como bubble
    • Relacionar price médio e availability_365
    • Uma cor de bolha para cada room_type
    • Tamanho da bolha proporcional ao número de anúncios
    • Adicionar camadas extras para melhorar o visual
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 = "Preço médio", y = "Disponibilidade média", color = "Tipo de quarto") + theme_classic()
Construindo dashboards com shinydashboard

Gráfico de bolhas com ggplot, renderizado

Um gráfico de bolhas comum criado com ggplot.

Construindo dashboards com shinydashboard

Gráfico de bolhas interativo com plotly

  • Envolva bubble em ggplotly()
ggplotly(bubble)

Um gráfico de bolhas interativo com plotly.

Construindo dashboards com shinydashboard

Boxplots horizontais com plotly

  • Alguns gráficos não convertem corretamente

  • Crie boxplots na horizontal

    • Descrever a distribuição de availability_365 por room_type
  • A conversão com ggplotly() não funciona
boxH <- listings %>%

ggplot(aes(x = availability_365, y=room_type)) +
geom_boxplot() +
labs(x="Disponibilidade (de 365 dias)", y="Tipo de quarto") + theme_classic()
ggplotly(boxH)

Boxplots horizontais criados com ggplot.

Boxplots horizontais convertidos com ggplotly.

Construindo dashboards com shinydashboard

Boxplots verticais com plotly

  • Inverta x e y em aes()
  • Agora a conversão para plotly funciona
boxV <- listings %>%

ggplot(aes(y = availability_365, x=room_type)) + geom_boxplot() + labs(y="Disponibilidade (de 365 dias)", x="Tipo de quarto") + theme_classic()
ggplotly(boxV)

Boxplots verticais com ggplot.

Boxplots verticais com ggplotly.

Construindo dashboards com shinydashboard

Corrigindo boxplots horizontais com plotly

  • E se quisermos boxplots horizontais?
    • Adicione coord_flip()
  • Também dá para usar facet_wrap()
    • Cada painel precisa ser grande o suficiente
ggplotly(boxV + coord_flip())
ggplotly(boxV + coord_flip() + 
    facet_wrap(~neighbourhood))

Boxplots do plotly com eixos invertidos.

Boxplots facetados no plotly.

Construindo dashboards com shinydashboard

Vamos praticar!

Construindo dashboards com shinydashboard

Preparing Video For Download...