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

Tokyo.R#73 BeginneRSession3

kilometer
October 20, 2018
370

Tokyo.R#73 BeginneRSession3

Tokyo.R#73の初心者セッション3「Plot and Visualization 」のスライドです。

kilometer

October 20, 2018
Tweet

Transcript

  1. BeginneR Advanced Hoxo_m If I have seen further it is

    by standing on the shoulders of Giants. -- Sir Isaac Newton, 1676
  2. ブール演算⼦ Boolean Algebra A == B A != B A

    | B A & B A %in% B # equal to # not equal to # or # and # is A in B?
  3. George Boole 1815 - 1864 A Class-Room Introduction to Logic

    https://niyamaklogic.wordpress.com/c ategory/laws-of-thoughts/ Mathematician Philosopher &
  4. 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
  5. {magrittr} # こう書きますか? 536&*/ dat1 <- f1(dat0, var1-1, var1-2) dat2

    <- f2(dat1, var2) dat3 <- f3(dat2, var3) dat4 <- f4(var4-1, dat3, var4-2) dat5 <- f5(dat4, var5) dat6 <- f6(dat5, var6) 536&065 5IJOLJOH 3FBEJOH 1JQFBMHFCSB %>%
  6. {magrittr} # こうやって書く事もできます。 dat <- dat0 %>% f1(var1-1, var1-2) %>%

    f2(var2) %>% f3(var3) %>% f4(var4-1, ., var4-2) %>% f5(var5) %>% f6(var6) */ 065 1JQFBMHFCSB %>%
  7. Face-to-face communication output input/feedback decode encode internal status A B

    estimated B status A status Emotion, Intention, ... = Language, Gesture, Prosody, Face expression...
  8. I visualized iris data on R!! Please explain the figure

    in text only. you you WHAT!!? There are totally 18 scattered plots arrayed in 5 by 5 matrix. Each plot represent 2 of 5 parameters on x and y axis. Only one parameter is discrete value named Species, and the others are Width and Length of Sepal and Petal. And....
  9. Text Image First, A. Next, B. Then C. Finally D.

    time Intention encode "Frozen" structure A B C D time value α β
  10. AC VC Mishkin 1983 TINS, Saur 2008 PNAS Visual &

    auditory pathway of the brain
  11. Text Image First, A. Next, B. Then C. Finally D.

    time Intention encode "Frozen" structure A B C D time value α β
  12. VC SC FEF White 2017 PNAS, Veale 2017 PhilosTransB Visual

    saliency & priority hypothesis SC: superior colliculus, FEF: frontal eye field
  13. VC SC FEF White 2017 PNAS, Veale 2017 PhilosTransB Visual

    saliency & priority hypothesis SC: superior colliculus, FEF: frontal eye field
  14. VC SC FEF White 2017 PNAS, Veale 2017 PhilosTransB Visual

    saliency & priority hypothesis SC: superior colliculus, FEF: frontal eye field
  15. Text Image First, A. Next, B. Then C. Finally D.

    time Intention encode "Frozen" structure A B C D time value α β
  16. Sample data iris %>% str pipe algebra row column 'data.frame':

    150 obs. of 5 variables: $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 ... $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 ... $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 ... $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 ... $ Species : Factor w/ 3 levels "setosa", ... class names mode value
  17. Basic plotting functions in {graphics} ・High-level: create a new plot

    ・Low-level: add more information ・Interactive: interactively add info.
  18. Basic plotting functions in {graphics} ・High-level: create a new plot

    ・Low-level: add more information ・Interactive: interactively add info.
  19. ・High-level: create a new plot Basic plotting functions in {graphics}

    x <- c(1:10) y <- 0.5 * x + 3 plot(x, y) Scatterplot of two vectors
  20. ・High-level: create a new plot Basic plotting functions in {graphics}

    x <- c(1:10) y <- 0.5 * x + 3 plot(x, y, type = "b") Scatterplot of two vectors
  21. ・High-level: create a new plot Basic plotting functions in {graphics}

    x <- c(1:10) y <- 0.5 * x + 3 plot(x, y, type = "o") Scatterplot of two vectors
  22. ・High-level: create a new plot Basic plotting functions in {graphics}

    x <- c(1:10) y <- 0.5 * x + 3 plot(x, y, type = "o", col = "red") Scatterplot of two vectors
  23. ・High-level: create a new plot Basic plotting functions in {graphics}

    x <- c(1:10) y <- 0.5 * x + 3 plot(x, y, type = "o", col = "red", pch = 2) Scatterplot of two vectors
  24. ・High-level: create a new plot Basic plotting functions in {graphics}

    Scatterplot of two vectors R-Tips, http://cse.naro.affrc.go.jp/takezawa/r-tips/r/53.html
  25. ・High-level: create a new plot Basic plotting functions in {graphics}

    x <- c(1:10) y <- 0.5 * x + 3 plot(x, y, type = "o", col = "red", pch = 2, lwd = 5) Scatterplot of two vectors
  26. Basic plotting functions in {graphics} How to plot multiple data

    series? + x <- 1:10 y <- 0.5 * x + 3 x <- 1:10 y2 <- 0.25 * x + 0.1 * x^2 + 1
  27. Basic plotting functions in {graphics} How to plot multiple data

    series? x <- c(1:10) y1 <- 0.5 * x + 3 y2 <- 0.25 * x + 0.1 * x^2 + 1 # create & set a new plot: high level plot(0, 0, type = "n", xlim = c(min(x), max(x)), ylim = c(min(y1, y2), max(y1, y2)), xlab = "x", ylab = "y") # add elements: low level funcs. lines(x, y1, col = "red") lines(x, y2, col = "blue") points(x, y1, col = "red") points(x, y2, col = "blue", pch = 2)
  28. Basic plotting functions in {graphics} How to plot multiple data

    series? x <- c(1:10) y1 <- 0.5 * x + 3 y2 <- 0.25 * x + 0.1 * x^2 + 1 # create & set a new plot: high level plot(0, 0, type = "n", xlim = c(min(x), max(x)), ylim = c(min(y1, y2), max(y1, y2)), xlab = "x", ylab = "y") # add elements: low level funcs lines(x, y1, col = "red") lines(x, y2, col = "blue") points(x, y1, col = "red") points(x, y2, col = "blue", pch = 2)
  29. # set graphic params: par par(mar = c(3, 3, 0.5,

    0.5), tcl = -0.2, mgp = c(1.5, 0.3, 0), bty = "l") Basic plotting functions in {graphics} How to custom graphic parameters? cf. R-Tips
  30. # set graphic params: par par(mar = c(3, 3, 0.5,

    0.5), tcl = -0.2, mgp = c(1.5, 0.3, 0), bty = "l") # create & set a new plot: high level plot(0, 0, type = "n", xlab = "x", ylab = "y", xlim = c(min(x), max(x)), ylim = c(min(y1, y2), max(y1, y2))) # add elements: low level funcs. lines(x, y1, col = "red") lines(x, y2, col = "blue") points(x, y1, col = "red") points(x, y2, col = "blue", pch = 2) Basic plotting functions in {graphics}
  31. # set graphic params: par par(mar = c(3, 3, 0.5,

    0.5), tcl = -0.2, mgp = c(1.5, 0.3, 0), bty = "l") # create & set a new plot: high level plot(0, 0, type = "n", xlab = "x", ylab = "y", xlim = c(min(x), max(x)), ylim = c(min(y1, y2), max(y1, y2))) # add elements: low level funcs. lines(x, y1, col = "red") lines(x, y2, col = "blue") points(x, y1, col = "red") points(x, y2, col = "blue", pch = 2) Basic plotting functions in {graphics} TOO BAD default values TOO MANY series names TOO MANY series parameters
  32. Sample data iris %>% str row column 'data.frame': 150 obs.

    of 5 variables: $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 ... $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 ... $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 ... $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 ... $ Species : Factor w/ 3 levels "setosa", ... class names mode value
  33. "declarative" graphic package {ggplot2} Basic plotting functions in {graphics} ⇔

    ふつ〜に思ったことを書けば描けるというニュアンス (直訳すると"平叙⽂の"ぐらいの意味)
  34. "declarative" graphic package {ggplot2} Basic plotting functions in {graphics} ⇔

    ggplot2 is a system for declaratively creating graphics, based on The Grammar of Graphics. You provide the data, tell ggplot2 how to map variables to aesthetics, what graphical primitives to use, and it takes care of the details. https://ggplot2.tidyverse.org/index.html ふつ〜に思ったことを書けば描ける
  35. # set graphic params par(...) # set a new plot

    plot(0, 0, type = "n", ...) # add elements lines(x, y1,..) lines(x, y2, ...) points(x, y1, ...) points(x, y2, ...) ggplot(data, aes(x, y, ...))+ geom_path()+ geom_points()+ scale_... theme_... in {graphics} in {ggplot2}
  36. # set graphic params par(...) # set a new plot

    plot(0, 0, type = "n", ...) # add elements lines(x, y1,..) lines(x, y2, ...) points(x, y1, ...) points(x, y2, ...) ggplot(data, aes(x, y, ...))+ geom_path()+ geom_points()+ scale_...()+ theme_...() in {graphics} in {ggplot2} ONLY data.frame
  37. # set graphic params par(...) # set a new plot

    plot(0, 0, type = "n", ...) # add elements lines(x, y1,..) lines(x, y2, ...) points(x, y1, ...) points(x, y2, ...) ggplot(data, aes(x, y, ...))+ geom_path()+ geom_points()+ scale_...()+ theme_...() in {graphics} in {ggplot2} ONLY data.frame ① data ② encode optionally, ③ style
  38. "declarative" graphic package {ggplot2} ggplot(iris, aes(x = Sepal.Length, y =

    Petal.Width, color = Species))+ geom_point() mode: factor
  39. Sample data iris %>% str row column 'data.frame': 150 obs.

    of 5 variables: $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 ... $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 ... $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 ... $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 ... $ Species : Factor w/ 3 levels "setosa", ... class names mode value
  40. "declarative" graphic package {ggplot2} ggplot(iris, aes(x = Sepal.Length, y =

    Petal.Width, color = Petal.Length))+ geom_point() continuous volume
  41. "declarative" graphic package {ggplot2} ggplot(iris, aes(x = Sepal.Length, y =

    Petal.Width, color = Species))+ geom_smooth()+ geom_points()
  42. "declarative" graphic package {ggplot2} ggplot(iris, aes(x = Sepal.Length, y =

    Petal.Width, color = Species))+ geom_smooth()+ geom_points(aes(shape=Species), size = 4, alpha = 0.5)
  43. "declarative" graphic package {ggplot2} ggplot(iris, aes(x = Sepal.Length, y =

    Petal.Width, color = Species))+ geom_smooth()+ geom_points()+ scale_color_manual(values = c("Blue", "Red", "Black"))
  44. "declarative" graphic package {ggplot2} ggplot(iris, aes(x = Sepal.Length, y =

    Petal.Width, color = Species))+ geom_smooth()+ geom_points()+ theme_classic()+
  45. "declarative" graphic package {ggplot2} ggplot(iris, aes(x = Sepal.Length, y =

    Petal.Width, color = Species))+ geom_smooth()+ geom_points()+ theme_classic()+ theme(legend.position = "bottom")
  46. # set graphic params par(...) # set a new plot

    plot(0, 0, type = "n", ...) # add elements lines(x, y1,..) lines(x, y2, ...) points(x, y1, ...) points(x, y2, ...) ggplot(data, aes(x, y, ...))+ geom_path()+ geom_points()+ scale_...()+ theme_...() in {graphics} in {ggplot2} ONLY data.frame ① data ② encode optionally, ③ style
  47. # set graphic params par(...) # set a new plot

    plot(0, 0, type = "n", ...) # add elements lines(x, y1,..) lines(x, y2, ...) points(x, y1, ...) points(x, y2, ...) ggplot(data, aes(x, y, ...))+ geom_path()+ geom_points()+ scale_...()+ theme_...() in {graphics} in {ggplot2} # open graphic device png("img.png", ...) # colse graphic device dev.off() ggsave("img.png", width = 5, height = 4)
  48. # set graphic params par(...) # set a new plot

    plot(0, 0, type = "n", ...) # add elements lines(x, y1,..) lines(x, y2, ...) points(x, y1, ...) points(x, y2, ...) ggplot(data, aes(x, y, ...))+ geom_path()+ geom_points()+ scale_...()+ theme_...() in {graphics} in {ggplot2} # open graphic device png("img.png", ...) # colse graphic device dev.off() ggsave("img.png", width = 5, height = 4) 1 2 3 4 5 1 2 3 4 5
  49. "declarative" graphic package {ggplot2} ggplot2 is a system for declaratively

    creating graphics, based on The Grammar of Graphics. You provide the data, tell ggplot2 how to map variables to aesthetics, what graphical primitives to use, and it takes care of the details. https://ggplot2.tidyverse.org/index.html ふつ〜に思ったことを書けば描ける
  50. Social communication output input/feedback decode encode internal status A B

    estimated B status A status Emotion, Intention, ... = Language, Gesture, Prosody, Face expression...
  51. BeginneR Advanced Hoxo_m If I have seen further it is

    by standing on the sholders of Giants. -- Sir Isaac Newton, 1676