In class exericse 03

Class notes and Pareto Chart exercise

Frostbear https://sg.linkedin.com/in/farahfoo (SMU Masters in IT business (Fintech and Analytics))https://scis.smu.edu.sg/master-it-business
2022-02-12

Using Tableau to create visualisations with time filters

Data science R tools

Adding Chart as a tooltip

Data science R tools

Exam marks as Scatter and box plot

Data science R tools

Bubbleplots plots on population with auto moving year

Data science R tools # USING R FOR GRAPHS

To load packages required today:

Show code
packages = c('tidyverse', 'readxl', 'ggthemes', 'ggiraph', 'plotly', 
             'gganimate', 'patchwork', 'DT', 'gifski', 'gapminder')

for(p in packages){
  if(!require(p, character.only = T)){
    install.packages(p)
  }
  library(p, character.only = T)
}

exam_data <- read_csv("data/Exam_data.csv")
head(exam_data,5)
# A tibble: 5 x 7
  ID         CLASS GENDER RACE    ENGLISH MATHS SCIENCE
  <chr>      <chr> <chr>  <chr>     <dbl> <dbl>   <dbl>
1 Student321 3I    Male   Malay        21     9      15
2 Student305 3I    Female Malay        24    22      16
3 Student289 3H    Male   Chinese      26    16      16
4 Student227 3F    Male   Chinese      27    77      31
5 Student318 3I    Male   Malay        27    11      25
Show code
p <- ggplot(data=exam_data,
aes(x = MATHS)) + 
geom_dotplot_interactive(
aes(tooltip = ID),
stackgroups = TRUE,
binwidth =1,
method = "histodot") +
scale_y_continuous(NULL, breaks = NULL)


girafe(ggobj = p,
width_svg = 6,
height_svg = 6*0.618)

changing the mouse-over to show associated elements

Show code
colnames (exam_data)
[1] "ID"      "CLASS"   "GENDER"  "RACE"    "ENGLISH" "MATHS"  
[7] "SCIENCE"
Show code
p <- ggplot(data=exam_data,
aes(x = MATHS)) + 
geom_dotplot_interactive(aes(data_id = CLASS),
stackgroups = TRUE, binwidth =1, method = "histodot") +
scale_y_continuous(NULL, breaks = NULL)
  #hover_css = "fill: blue"


girafe(ggobj = p,
width_svg = 6,
height_svg = 6*0.618)

varying more of the mouse-over colours

Show code
head(p,2)
$data
# A tibble: 322 x 7
   ID         CLASS GENDER RACE    ENGLISH MATHS SCIENCE
   <chr>      <chr> <chr>  <chr>     <dbl> <dbl>   <dbl>
 1 Student321 3I    Male   Malay        21     9      15
 2 Student305 3I    Female Malay        24    22      16
 3 Student289 3H    Male   Chinese      26    16      16
 4 Student227 3F    Male   Chinese      27    77      31
 5 Student318 3I    Male   Malay        27    11      25
 6 Student306 3I    Female Malay        31    16      16
 7 Student313 3I    Male   Chinese      31    21      25
 8 Student316 3I    Male   Malay        31    18      27
 9 Student312 3I    Male   Malay        33    19      15
10 Student297 3H    Male   Indian       34    49      37
# ... with 312 more rows

$layers
$layers[[1]]
mapping: data_id = ~CLASS 
geom_interactive_dotplot: binaxis = x, stackdir = up, stackratio = 1, dotsize = 1, stackgroups = TRUE, na.rm = FALSE, .ipar = c("data_id", "tooltip", "onclick", "hover_css", "selected_css", "tooltip_fill")
stat_bindot: binaxis = x, binwidth = 1, binpositions = bygroup, method = histodot, origin = NULL, right = TRUE, width = 0.9, drop = FALSE, na.rm = FALSE
position_identity 
Show code
colnames (exam_data)
[1] "ID"      "CLASS"   "GENDER"  "RACE"    "ENGLISH" "MATHS"  
[7] "SCIENCE"
Show code
p <- ggplot(data=exam_data,
aes(x = MATHS)) + 
geom_dotplot_interactive(aes(data_id = CLASS),
stackgroups = TRUE, binwidth =1, method = "histodot") +
scale_y_continuous(NULL, breaks = NULL)
  #hover_css = "fill: blue"


girafe(ggobj = p,
width_svg = 6,
height_svg = 6*0.618,
options = list(opts_hover(css = "fill: blue"),
               opts_hover_inv(css = "opacity:0.2;")))

use patchwork to arrange multiple graphs together

Show code
p1 <- ggplot(data=exam_data,
aes(x = MATHS)) + 
geom_dotplot_interactive(aes(data_id = ID),
stackgroups = TRUE, binwidth =1, method = "histodot") +
  coord_cartesian (xlim=c(0,100)) +
scale_y_continuous(NULL, breaks = NULL)

p2 <- ggplot(data=exam_data,
aes(x = ENGLISH)) + 
geom_dotplot_interactive(aes(data_id = ID),
stackgroups = TRUE, binwidth =1, method = "histodot") +
  coord_cartesian (xlim=c(0,100)) +
scale_y_continuous(NULL, breaks = NULL)

# girafe(ggobj = print (p1 / p2),
# width_svg = 6,
# height_svg = 6,
# options = list(opts_hover(css = "fill: blue"),
#                opts_hover_inv(css = "opacity:0.2;")))