Upgrade to Pro — share decks privately, control downloads, hide ads and more …

第85回Tokyo.R初心者セッション:データ可視化

 第85回Tokyo.R初心者セッション:データ可視化

第85回Tokyo.R初心者セッションのトークスライドです。

kilometer

May 23, 2020
Tweet

More Decks by kilometer

Other Decks in Technology

Transcript

  1. ⫝⥼ (mapping) ! " #: % → ' % '

    # ! = " ꞊丗 ⊂ ⫝⥼
  2. ! " #$ %$ #& %& ! " %$ %&

    #$ #& ⺎釱⴫ ⊂ ⫝⥼ mapping
  3. ! " #$ %$ #& %& ! " %$ %&

    #$ #& ⺎釱⴫ ⊂ ⫝⥼ mapping x axis, y axis, color, fill, shape, linetype, alpha… aesthetic channels
  4. Data visualization with ggplot2 ! " #$ %$ #& %&

    ! " %$ %& #$ #& mapping x axis, y axis, color, fill, shape, linetype, alpha… aesthetic channels data
  5. Data visualization with ggplot2 # install.packages("tidyverse") library(tidyverse) dat <- data.frame(a

    = 1:3, b = 8:10) Attach package Simple example > dat a b 1 1 8 2 2 9 3 3 10
  6. Data visualization with ggplot2 dat <- data.frame(a = 1:3, b

    = 8:10) ggplot(data = dat)+ geom_point(mapping = aes(x = a, y = b))
  7. Data visualization with ggplot2 dat <- data.frame(a = 1:3, b

    = 8:10) ggplot(data = dat)+ geom_point(mapping = aes(x = a, y = b)) ! " #$ %$ #& %& ! " %$ %& #$ #& mapping x axis, y axis, color, fill, shape, linetype, alpha… aesthetic channels data
  8. Data visualization with ggplot2 dat <- data.frame(a = 1:3, b

    = 8:10) ggplot(data = dat)+ geom_point(mapping = aes(x = a, y = b)) ! " #$ %$ #& %& ! " %$ %& #$ #& mapping x axis, y axis, color, fill, shape, linetype, alpha… aesthetic channels data
  9. Data visualization with ggplot2 dat <- data.frame(a = 1:3, b

    = 8:10) ggplot(data = dat)+ geom_point(mapping = aes(x = a, y = b))+ geom_path(mapping = aes(x = a, y = b))
  10. Data visualization with ggplot2 dat <- data.frame(a = 1:3, b

    = 8:10) ggplot(data = dat, mapping = aes(x = a, y = b))+ geom_point()+ geom_path()
  11. Data visualization with ggplot2 dat <- data.frame(a = 1:3, b

    = 8:10) ggplot(data = dat, mapping = aes(x = a, y = b))+ geom_point()+ geom_path()
  12. Data visualization with ggplot2 dat <- data.frame(a = 1:3, b

    = 8:10) ggplot(data = dat)+ aes(x = a, y = b)+ geom_point()+ geom_path()
  13. Data visualization with ggplot2 dat <- data.frame(a = 1:3, b

    = 8:10) g <- ggplot(data = dat)+ aes(x = a, y = b) g+ geom_point()+ geom_path()
  14. Data visualization with ggplot2 dat <- data.frame(a = 1:3, b

    = 8:10) g <- ggplot(data = dat)+ aes(x = a, y = b)+ geom_point() g+ geom_path()
  15. Data visualization with ggplot2 dat <- data.frame(x = 1:3, y1

    = 8:10, y2 = 6:8) Simple example #2 > dat x y1 y2 1 1 8 6 2 2 9 7 3 3 10 8
  16. Data visualization with ggplot2 dat <- data.frame(x = 1:3, y1

    = 8:10, y2 = 6:8) Simple example #2 > dat x y1 y2 1 1 8 6 2 2 9 7 3 3 10 8 mapping
  17. Data visualization with ggplot2 dat <- data.frame(x = 1:3, y1

    = 8:10, y2 = 6:8) Simple example #2 > dat x y1 y2 1 1 8 6 2 2 9 7 3 3 10 8 mapping y x aes(x = x, y = ???)
  18. Data visualization with ggplot2 dat <- data.frame(x = 1:3, y1

    = 8:10, y2 = 6:8) Simple example #2 > dat x y1 y2 1 1 8 6 2 2 9 7 3 3 10 8 aes(x = x, y = y) x key y 1 1 y1 8 2 1 y2 6 3 2 y1 9 4 2 y2 7 5 3 y1 10 6 3 y2 8
  19. Data visualization with ggplot2 dat <- data.frame(x = 1:3, y1

    = 8:10, y2 = 6:8) Simple example #2 > dat x y1 y2 1 1 8 6 2 2 9 7 3 3 10 8 > dat_long x key y 1 1 y1 8 2 1 y2 6 3 2 y1 9 4 2 y2 7 5 3 y1 10 6 3 y2 8 Wide Long Long format Wide format
  20. Data visualization with ggplot2 dat <- data.frame(x = 1:3, y1

    = 8:10, y2 = 6:8) Simple example #2 dat_long <- pivot_longer(data = dat, cols = starts_with("y"), names_to = "key", values_to = "y") > dat_long x key y 1 1 y1 8 2 1 y2 6 3 2 y1 9 4 2 y2 7 5 3 y1 10 6 3 y2 8
  21. Data visualization with ggplot2 dat <- data.frame(x = 1:3, y1

    = 8:10, y2 = 6:8) Simple example #2 dat_long <- pivot_longer(data = dat, cols = starts_with("y"), names_to = "key", values_to = "y") > dat_long x key y 1 1 y1 8 2 1 y2 6 3 2 y1 9 4 2 y2 7 5 3 y1 10 6 3 y2 8 ggplot(data = dat_long)+ aes(x = x, y = y, color = key)+ geom_point()+ geom_path()
  22. Data visualization with ggplot2 dat <- data.frame(x = 1:3, y1

    = 8:10, y2 = 6:8) Simple example #2 dat_long <- pivot_longer(data = dat, cols = starts_with("y"), names_to = "key", values_to = "y") > dat_long x key y 1 1 y1 8 2 1 y2 6 3 2 y1 9 4 2 y2 7 5 3 y1 10 6 3 y2 8 ggplot(data = dat_long)+ aes(x = x, y = y, color = key)+ geom_point()+ geom_path()
  23. Data visualization with ggplot2 > anscombe x1 x2 x3 x4

    y1 y2 y3 y4 1 10 10 10 8 8.04 9.14 7.46 6.58 2 8 8 8 8 6.95 8.14 6.77 5.76 3 13 13 13 8 7.58 8.74 12.74 7.71 4 9 9 9 8 8.81 8.77 7.11 8.84 5 11 11 11 8 8.33 9.26 7.81 8.47 6 14 14 14 8 9.96 8.10 8.84 7.04 7 6 6 6 8 7.24 6.13 6.08 5.25 8 4 4 4 19 4.26 3.10 5.39 12.50 9 12 12 12 8 10.84 9.13 8.15 5.56 10 7 7 7 8 4.82 7.26 6.42 7.91 11 5 5 5 8 5.68 4.74 5.73 6.89
  24. Data visualization with ggplot2 > anscombe x1 x2 x3 x4

    y1 y2 y3 y4 1 10 10 10 8 8.04 9.14 7.46 6.58 2 8 8 8 8 6.95 8.14 6.77 5.76 3 13 13 13 8 7.58 8.74 12.74 7.71 4 9 9 9 8 8.81 8.77 7.11 8.84 5 11 11 11 8 8.33 9.26 7.81 8.47 6 14 14 14 8 9.96 8.10 8.84 7.04 7 6 6 6 8 7.24 6.13 6.08 5.25 8 4 4 4 19 4.26 3.10 5.39 12.50 9 12 12 12 8 10.84 9.13 8.15 5.56 10 7 7 7 8 4.82 7.26 6.42 7.91 11 5 5 5 8 5.68 4.74 5.73 6.89
  25. Data visualization with ggplot2 > anscombe x1 x2 x3 x4

    y1 y2 y3 y4 1 10 10 10 8 8.04 9.14 7.46 6.58 2 8 8 8 8 6.95 8.14 6.77 5.76 3 13 13 13 8 7.58 8.74 12.74 7.71 4 9 9 9 8 8.81 8.77 7.11 8.84 5 11 11 11 8 8.33 9.26 7.81 8.47 6 14 14 14 8 9.96 8.10 8.84 7.04 7 6 6 6 8 7.24 6.13 6.08 5.25 8 4 4 4 19 4.26 3.10 5.39 12.50 9 12 12 12 8 10.84 9.13 8.15 5.56 10 7 7 7 8 4.82 7.26 6.42 7.91 11 5 5 5 8 5.68 4.74 5.73 6.89 a > anscombe_long # A tibble: 44 x 3 key x y <chr> <dbl> <dbl> 1 1 10 8.04 2 2 10 9.14 3 3 10 7.46 4 4 8 6.58 5 1 8 6.95 6 2 8 8.14 7 3 8 6.77 8 4 8 5.76
  26. Data visualization with ggplot2 > anscombe x1 x2 x3 x4

    y1 y2 y3 y4 1 10 10 10 8 8.04 9.14 7.46 6.58 2 8 8 8 8 6.95 8.14 6.77 5.76 3 13 13 13 8 7.58 8.74 12.74 7.71 anscombe_long <- pivot_longer(data = anscombe, cols = everything(), names_pattern = "(.)(.)", names_to = c(".value", "key"))
  27. > anscombe x1 x2 x3 x4 y1 y2 y3 y4

    1 10 10 10 8 8.04 9.14 7.46 6.58 2 8 8 8 8 6.95 8.14 6.77 5.76 3 13 13 13 8 7.58 8.74 12.74 7.71 Data visualization with ggplot2 anscombe_long <- pivot_longer(data = anscombe, cols = everything(), names_pattern = "(.)(.)", names_to = c(".value", "key"))
  28. Data visualization with ggplot2 anscombe_long <- pivot_longer(data = anscombe, cols

    = everything(), names_pattern = "(.)(.)", names_to = c(".value", "key")) > anscombe_long # A tibble: 44 x 3 key x y <chr> <dbl> <dbl> 1 1 10 8.04 2 2 10 9.14 3 3 10 7.46 4 4 8 6.58 5 1 8 6.95 6 2 8 8.14 7 3 8 6.77 8 4 8 5.76
  29. Data visualization with ggplot2 anscombe_long <- pivot_longer(data = anscombe, cols

    = everything(), names_pattern = "(.)(.)", names_to = c(".value", "key")) > anscombe_long # A tibble: 44 x 3 key x y <chr> <dbl> <dbl> 1 1 10 8.04 2 2 10 9.14 3 3 10 7.46 4 4 8 6.58 5 1 8 6.95 6 2 8 8.14 7 3 8 6.77 8 4 8 5.76 g_anscomb <- ggplot(data = anscombe_long)+ aes(x = x, y = y, color = key)+ geom_point()
  30. Data visualization with ggplot2 anscombe_long <- pivot_longer(data = anscombe, cols

    = everything(), names_pattern = "(.)(.)", names_to = c(".value", "key")) g_anscomb <- ggplot(data = anscombe_long)+ aes(x = x, y = y, color = key)+ geom_point() > anscombe_long # A tibble: 44 x 3 key x y <chr> <dbl> <dbl> 1 1 10 8.04 2 2 10 9.14 3 3 10 7.46 4 4 8 6.58 5 1 8 6.95 6 2 8 8.14 7 3 8 6.77 8 4 8 5.76
  31. ! " #$ %$ #& %& ! " %$ %&

    #$ #& ⺎釱⴫ ⊂ ⫝⥼ mapping x axis, y axis, color, fill, shape, linetype, alpha… aesthetic channels
  32. Data visualization with ggplot2 dat <- data.frame(a = 1:3, b

    = 8:10) ggplot(data = dat)+ geom_point(mapping = aes(x = a, y = b)) ! " #$ %$ #& %& ! " %$ %& #$ #& mapping x axis, y axis, color, fill, shape, linetype, alpha… aesthetic channels data
  33. Data visualization with ggplot2 dat <- data.frame(a = 1:3, b

    = 8:10) ggplot(data = dat)+ aes(x = a, y = b)+ geom_point()+ geom_path()
  34. Data visualization with ggplot2 dat <- data.frame(x = 1:3, y1

    = 8:10, y2 = 6:8) Simple example #2 > dat x y1 y2 1 1 8 6 2 2 9 7 3 3 10 8 > dat_long x key y 1 1 y1 8 2 1 y2 6 3 2 y1 9 4 2 y2 7 5 3 y1 10 6 3 y2 8 Wide Long Long format Wide format
  35. Data visualization with ggplot2 dat <- data.frame(x = 1:3, y1

    = 8:10, y2 = 6:8) Simple example #2 dat_long <- pivot_longer(data = dat, cols = starts_with("y"), names_to = "key", values_to = "y") > dat_long x key y 1 1 y1 8 2 1 y2 6 3 2 y1 9 4 2 y2 7 5 3 y1 10 6 3 y2 8 ggplot(data = dat_long)+ aes(x = x, y = y, color = key)+ geom_point()+ geom_path()