Data Science

Visualization: Line Chart

Séance Précendente


  • df <- read_csv('~path')
  • df2 <- df %>% dplyr::function()
  • write_csv(df2, '~path2')



Import -> Modify -> Save

Introduction à la visualisation

Type de graphiques

Pourquoi communiquer via un graphique?

  • Synthétisation de l’information
  • Plus facile à analyser
  • Plus rapide à analyser

Les graphiques sont analysés 60 000 fois plus vite que des mots
Des contenus avec des visuels attirent plus l’attention
Il suffit de 1/10s pour comprendre un visuel, comparer à 60s pour lire 200 mots
65% d’un contenu visuel est retenu contre 10% d’un texte

Line chart

Line chart

Line chart

Line chart

Line chart

Code

library(ggplot)
ggplot(data= YOUR_DATASET)+
  
  geom_line(aes(x= X_AXIS_VAR, y= Y_AXIS_VAR, color= COLOR_VAR)) +

  ggtitle("YOUR TITLE")+ # The Graph title
  xlab("YOUR X LABEL") + # If you want something different 
  ylab("YOUR Y LABEL") + # from X_AXIS_VAR & Y_AXIS_VAR
  
  theme_minimal() +      # Optional
  theme(plot.title = element_text(size = 15,  hjust = 0.5), # Optional
        panel.grid.minor = element_blank(), # Optional
        panel.grid.major.x = element_blank(), # Optional
        )

Instructions

  • Lire le fichier stocks.xlsx sheet 2
  • Format adéquate pour faire un graphe
  • Faire le graphe
  • Pimp your graph

Solution proposée

library(tidyverse)
library(readxl)
library(janitor)

stocks <- read_xlsx('/data/stocks.xlsx', sheet = 2, skip = 3)


stocks_long <- stocks %>% 
  rename(stocks=date) %>% 
  pivot_longer(-stocks, names_to = 'date', values_to = 'val') %>% 
  mutate(date=as.Date(as.numeric(date), origin = "1899-12-30"),
         val=as.numeric(val)) %>% 
  filter(date >as.Date('2013-01-01')) %>% 
  group_by(stocks) %>% 
  mutate(indice= 100*val/first(val))

p1 <- ggplot(data=stocks_long %>% filter(stocks=='AEMD US  EQUITY'))+
  geom_line(aes(x=date, y=indice, color=stocks))

p2 <- ggplot(data=stocks_long %>% filter(stocks=='AEMD US  EQUITY'))+
  geom_line(aes(x=date, y=indice, color=stocks))+
  ggtitle("Evolution du cours d'Aethlon Medical Inc \n à la bourse entre 2013 et 2018") +
  xlab("Date") +
  ylab("Valeur ($)") +
  theme_minimal() +
  theme(plot.title = element_text(size = 15, colour = "red", face = "bold", hjust = 0.5),
        panel.grid.minor = element_blank(),
        panel.grid.major.x = element_blank(),
        axis.title.x = element_text(colour = "red", face = "bold"),
        axis.title.y = element_text(colour = "red", face = "bold"),
        legend.position="none")

Solution proposée

library(tidyverse)
library(readxl)
library(janitor)

stocks <- read_xlsx('/data/stocks.xlsx', sheet = 2, skip = 3)


stocks_long <- stocks %>% 
  rename(stocks=date) %>% 
  pivot_longer(-stocks, names_to = 'date', values_to = 'val') %>% 
  mutate(date=as.Date(as.numeric(date), origin = "1899-12-30"),
         val=as.numeric(val)) %>% 
  filter(date >as.Date('2013-01-01')) %>% 
  group_by(stocks) %>% 
  mutate(indice= 100*val/first(val))

p1 <- ggplot(data=stocks_long %>% filter(stocks=='AEMD US  EQUITY'))+
  geom_line(aes(x=date, y=indice, color=stocks))

p2 <- p1+
  ggtitle("Evolution du cours d'Aethlon Medical Inc \n 
          à la bourse entre 2013 et 2018") +
  xlab("Date") +
  ylab("Valeur ($)") +
  theme_minimal() +
  theme(plot.title = element_text(size = 14, colour = "#717171", face = "bold", hjust = 0.5),
        panel.grid.minor = element_blank(),
        panel.grid.major.x = element_blank(),
        axis.title.x = element_text(colour = "#717171"),
        axis.title.y = element_text(colour = "#717171"),
        legend.position="none")

Rendu Visuel

Pour aller plus loin

facet_wrap()

ggplot(data=stocks_long2)+
  geom_line(aes(x=date, y=indice, color=new_stocks)) +
  labs(color ="Stocks") +
  theme_minimal() +
  facet_wrap(~gics_sector_name, nrow = 3)

Instructions

  • Joindre stocks.xlsx sheet 2 et 3
  • Faire un graphe avec facet_wrap()
  • Pimp your graph

Graphique Interactif

Rendu Visuel

Code

library(highcharter)

stocks_long2 <- stocks_long2 %>% 
  mutate(new_stocks2= paste0(str_sub(new_stocks,start=1, end=-7),'(',gics_sector_name, ')'))

hchart(stocks_long2, type="line", hcaes(x = date, y = indice, group = new_stocks2)) %>% 
  hc_title(text = "Stock Comparison<br>from 2013 to 2019", useHTML=TRUE)

Récapitulatif

  • Graphique :
  •   Titre, noms des axes, légende
  • Line chart :
  •   2 variables continues
  •   Beaucoup de données sur l’axe horizontal
  • Code :
  •   ggplot()+geom_line(DATA, aes(X,Y)) (Normal)
  •   hchart(DATA,'line',hcaes(X,Y)) (Interactif)

Merci pour votre attention

Questions?

Contact

Kevin Rosamont Prombo