Formation R

Visualisaton intéractive

Contexte

Rapport html se couple parfaitement avec les graphiques intéractifs .

Problématiques

  • Plusieurs packages? Lequel choisir?

Plotly

Avantage: Reprendre les visualisations réalisées avec ggplot2

  • p <- ggplot(data=stocks_long ) +
  • geom_line(aes(x=date, y=indice, color=stocks))
  • ggplotly(p)

Line chart

ggplot2

library(ggplot2)

p <- ggplot(data=stocks_long )+
  geom_line(aes(x=date, y=indice, color=stocks))+
  xlab("Date") +
  ylab("Valeur ($)") 

plotly

library(plotly)

ggplotly(p)

Line chart

Evolution du cours d’Aethlon Medical Inc à la bourse entre 2013 et 2018

ggplot2

plotly

Highcharter

  • Avantage : Visualisations interactives de grandes qualités
  • Désavantage : Syntaxe différente
library(highcharter)

hchart(
  stocks_long, #data
  "line", #type of chart
  hcaes(x = date, y = indice)) 

Line chart

Evolution du cours d’Aethlon Medical Inc à la bourse entre 2013 et 2018

ggplot2 + plotly

library(ggplot2)

p <- ggplot(data=stocks_long )+
  geom_line(aes(x=date, y=indice))+
  # ggtitle("Evolution du cours d'Aethlon Medical Inc \n à la bourse entre 2013 et 2018") +
  xlab("Date") +
  ylab("Valeur ($)") 

ggplotly(p)

highcharter

library(highcharter)

hchart(
  stocks_long, #data
  "line", #type of chart
  hcaes(x = date, y = indice)) 

Line chart

Evolution du cours d’Aethlon Medical Inc à la bourse entre 2013 et 2018

ggplot2 + plotly

highcharter

Scatter plot

ggplot2 + plotly

graph_points <- ggplot(data=athome )+
  geom_point(aes(x=surface, y=prix, color=quartier))

ggplotly(graph_points, width= 500, height= 280) %>% config(displayModeBar=FALSE)

highcharter

hchart(athome, 
       'scatter',
       hcaes(x=surface, y=prix, group=quartier)) %>% 
  hc_size(height = 280, width=500)

Scatter plot

ggplot2 + plotly

highcharter

Bar plot

ggplot2 + plotly

graph_bar <- ggplot(data= pop)+
  geom_bar(aes(x= county, y= popdensity), stat="identity") +
  ggtitle("Comparaison de la densité de population pour 5 comtés de l'Illinois")+ # The Graph title
  xlab("Comté") + # If you want something different 
  ylab("densité")

ggplotly(graph_bar)

highcharter

hchart(pop, 
       'column',
       hcaes(x=county, y= popdensity)) %>% 
    hc_title(text = "Comparaison de la densité de population pour 5 comtés de l'Illinois") %>% 
  hc_xAxis(title=list(text = "Comté")) %>% 
  hc_yAxis(title=list(text = "Densité")) 

Bar plot

ggplot2 + plotly

highcharter

Pie chart

plotly

plot_ly(data=count_loc,
        type="pie"
        labels=~nb_pct, 
        values=~pct)

highcharter

hchart(count_loc, 
       'pie',
       hcaes(name= nb_pct, y= pct)) #%>% 
  #hc_title(text="Répartition du nombre de location<br>disponible par quartier en 2019")

Pie chart

Répartition du nombre de location disponible par quartier en 2019

plotly

highcharter

Modifier le tooltip

ggplot2 + plotly

Modifier le tooltip

Code associé

p <- ggplot(data=stocks_long )+
  geom_line(aes(x=date, y=indice,
                 group = 1,
            text = paste("Date: ", date,
                         "<br>Valeur: $", indice))
            )+
  # ggtitle("Evolution du cours d'Aethlon Medical Inc \n à la bourse entre 2013 et 2018") +
  xlab("Date") +
  ylab("Valeur ($)") 

ggplotly(p, tooltip = c("date"), width= 500, height= 280) %>% config(displayModeBar=FALSE)

Modifier le tooltip

highcharter

Modifier le tooltip

Code associé

library(highcharter)

hchart(
  stocks_long, #data
  "line", #type of chart
  hcaes(x = date, y = indice, color=stocks)) %>% 
  hc_tooltip(
    headerFormat = "",
    pointFormat = "<b>{point.date}</b> est la date <br> <b>{point.indice}</b>  est la valeur")%>% 
  hc_size(height = 280, width=500)

Instructions

  • 1) Reprendre le rapport crée
  • 2) Changer les graphiques en graphiques interactifs

Source

Highcharter
ggplot2
Plotly

Contact

Kevin Rosamont Prombo