Slide 1

Slide 1 text

#89 @kilometer00 2021.01.23 BeginneR Session -- Data Visualization --

Slide 2

Slide 2 text

Who!? 誰だ?

Slide 3

Slide 3 text

Who!? 名前: 三村 @kilometer 職業: ポスドク (こうがくはくし) 専⾨: ⾏動神経科学(霊⻑類) 脳イメージング 医療システム⼯学 R歴: ~ 10年ぐらい 流⾏: 直⽴⼆⾜歩⾏

Slide 4

Slide 4 text

㲚⚥傴盈ס緾錌מ⹆ⱶ׊ױ׊גն 鞄㚞ꝧ㡎 "NB[PO־׼◀硜ך׀ױ׌

Slide 5

Slide 5 text

BeginneR Session

Slide 6

Slide 6 text

BeginneR

Slide 7

Slide 7 text

BeginneR Advanced Hoxo_m If I have seen further it is by standing on the shoulders of Giants. -- Sir Isaac Newton, 1676

Slide 8

Slide 8 text

Before A'er BeginneR Session BeginneR BeginneR

Slide 9

Slide 9 text

Why & How

Slide 10

Slide 10 text

"a" != "b" # is A in B? ブール演算⼦ Boolean Algebra [1] TRUE 1 %in% 10:100 # is A in B? [1] FALSE

Slide 11

Slide 11 text

George Boole 1815 - 1864 A Class-Room Introduction to Logic https://niyamaklogic.wordpress.com/c ategory/laws-of-thoughts/ Mathematician Philosopher &

Slide 12

Slide 12 text

ブール演算⼦ Boolean Algebra A == B A != B George Boole 1815 - 1864 A | B A & B A %in% B # equal to # not equal to # or # and # is A in B? wikipedia

Slide 13

Slide 13 text

1JQFBMHFCSB X %>% f X %>% f(y) X %>% f %>% g X %>% f(y, .) f(X) f(X, y) g(f(X)) f(y, X) %>% {magrittr} 「dplyr再⼊⾨(基本編)」yutanihilation https://speakerdeck.com/yutannihilation/dplyrzai-ru-men-ji-ben-bian

Slide 14

Slide 14 text

① ② ③ ④ lift take pour put Bring me milk from the kitchen!

Slide 15

Slide 15 text

① lift Bring me milk from the kitchen! lift(Robot, glass, table) -> Robot' take ② take(Robot', fridge, milk) -> Robot''

Slide 16

Slide 16 text

Bring me milk from the kitchen! Robot' <- lift(Robot, glass, table) Robot'' <- take(Robot', fridge, milk) Robot''' <- pour(Robot'', milk, glass) result <- put(Robot''', glass, table) result <- Robot %>% lift(glass, table) %>% take(fridge, milk) %>% pour(milk, glass) %>% put(glass, table) by using pipe, # ① # ② # ③ # ④ # ① # ② # ③ # ④

Slide 17

Slide 17 text

Programing

Slide 18

Slide 18 text

Programing

Slide 19

Slide 19 text

Programing Write Run Read Think Write Run Read Think Communicate Share

Slide 20

Slide 20 text

Text Image Information Intention Data decode encode Data analysis feedback ≠

Slide 21

Slide 21 text

Text Image First, A. Next, B. Then C. Finally D. time Intention encode "Frozen" structure A B C D time value α β

Slide 22

Slide 22 text

&ODPEF "QQMF 3FBM "QQMF *OGPSNBUJPO %FDPEF

Slide 23

Slide 23 text

%JWFSHFODF 3FBM *OGP %BUB "QQMF &ODPEJOH

Slide 24

Slide 24 text

"QQMF &ODPEF 'SVJU 3FE JNBHF 3FBM *OGPSNBUJPO

Slide 25

Slide 25 text

"QQMF &ODPEF 'SVJU 3FE JNBHF 3FBM *OGPSNBUJPO DIBOOFM

Slide 26

Slide 26 text

⫝⥼ (mapping) !: # → % # % ֵ׾䗯㕔ס꥗⺬ס釐碛؅յ⮯ס䗯㕔ס꥗⺬ס גדחס釐碛מ㵚䑴טׄ׾وٞجت

Slide 27

Slide 27 text

⫝⥼ (mapping) !"##$%& '"(" )!"&*

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

"QQMF &ODPEF 'SVJU 3FE JNBHF 3FBM *OGPSNBUJPO DIBOOFM

Slide 31

Slide 31 text

! " #$ %$ #& %& ! " %$ %& #$ #& mapping x axis, y axis, color, fill, shape, linetype, alpha… aesthetic channels data ggplot2 package

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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 ggplot2

Slide 35

Slide 35 text

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))

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

Anscombe's quartet > 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

Slide 41

Slide 41 text

ggplot(data = anscombe) + aes(x = x1, y = y1)) + geom_point() Anscombe's quartet

Slide 42

Slide 42 text

> 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 x y mapping Anscombe's quartet

Slide 43

Slide 43 text

> 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 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 Wide form Long form

Slide 44

Slide 44 text

> 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")) Wide -> Long form

Slide 45

Slide 45 text

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 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 Anscombe's quartet

Slide 46

Slide 46 text

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 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 Anscombe's quartet g_anscomb <- ggplot(data = anscombe_long) + aes(x = x, y = y, color = key) + geom_point()

Slide 47

Slide 47 text

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 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 Anscombe's quartet

Slide 48

Slide 48 text

g_anscomb + facet_wrap( ~ key) Anscombe's quartet

Slide 49

Slide 49 text

g_anscomb + facet_wrap( ~ key) + theme(legend.position = "none") Anscombe's quartet

Slide 50

Slide 50 text

Wide Long Nested input output pivot_longer pivot_wider group_nest unnest ggplot visualization map output ggsave

Slide 51

Slide 51 text

Enjoy!! KMT©