Tokyo.R#73 BeginneRSession3

8284465a94bbdf1ea82cf1a67d55f447?s=47 kilometer
October 20, 2018
310

Tokyo.R#73 BeginneRSession3

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

8284465a94bbdf1ea82cf1a67d55f447?s=128

kilometer

October 20, 2018
Tweet

Transcript

  1. #73 @kilometer00 2018.10.20 at eureka BeginneR Session 3 -- Visualization

    & Plot --
  2. Who!?

  3. Who!? 名前: 三村 @kilometer 職業: ポスドク (こうがくはくし) 専⾨: ⾏動神経科学(霊⻑類) 脳イメージング

    医療システム⼯学 R歴: ~ 10年ぐらい 流⾏: イタ飯
  4. BeginneR Session

  5. BeginneR

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

    by standing on the shoulders of Giants. -- Sir Isaac Newton, 1676
  7. Before After BeginneR Session BeginneR BeginneR

  8. ブール演算⼦ Boolean Algebra A == B A != B A

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

    https://niyamaklogic.wordpress.com/c ategory/laws-of-thoughts/ Mathematician Philosopher &
  10. 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
  11. {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 %>%
  12. {magrittr} # こうやって書く事もできます。 dat <- dat0 %>% f1(var1-1, var1-2) %>%

    f2(var2) %>% f3(var3) %>% f4(var4-1, ., var4-2) %>% f5(var5) %>% f6(var6) */ 065 1JQFBMHFCSB %>%
  13. None
  14. #73 @kilometer00 2018.10.20 at eureka BeginneR Session 3 -- Visualization

    & Plot --
  15. Ageanda 1. Vision 2. Visualization in

  16. Visual image - in a way of Intention coding

  17. 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...
  18. Text Image Information Intention Data decode encode Public writing no

    feedback
  19. Text Image Information Intention Data decode encode Private (exploratory) writing

    Feedback
  20. plot(iris)

  21. 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....
  22. Text Image ≠

  23. Text Image Information Intention Data decode encode Public writing no

    feedback ≠
  24. Text Image First, A. Next, B. Then C. Finally D.

    time Intention encode "Frozen" structure A B C D time value α β
  25. How we "read" visual images?

  26. Human brain

  27. VC Mishkin 1983 TINS, Saur 2008 PNAS Visual pathway of

    the brain primary visual cortex
  28. Mishkin 1983 TINS Visual pathway of the brain VC

  29. Mishkin 1983 TINS Visual pathway of the brain VC

  30. What Yamis 2014 PNAS Visual pathway of the brain Convolutional

    Feature map VC
  31. Saur 2008 PNAS Auditory pathway of the brain VC AC

  32. Saur 2008 PNAS Auditory pathway of the brain VC AC

    (repetition) (comprehension)
  33. AC VC Mishkin 1983 TINS, Saur 2008 PNAS Visual &

    auditory pathway of the brain
  34. AC VC Language Mishkin 1983 TINS, Saur 2008 PNAS Visual

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

    time Intention encode "Frozen" structure A B C D time value α β
  36. How we "read" visual images?

  37. Hou 2007 IEEE

  38. Hou 2007 IEEE

  39. VC SC FEF White 2017 PNAS, Veale 2017 PhilosTransB Visual

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

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

    saliency & priority hypothesis SC: superior colliculus, FEF: frontal eye field
  42. Original Saliency map Visual saliency map Hou 2007 IEEE

  43. ・What & Where pathway ・ Image ⇔ Language(multimodality) ・ Spatial

    saliency How we "read" visual images?
  44. Text Image First, A. Next, B. Then C. Finally D.

    time Intention encode "Frozen" structure A B C D time value α β
  45. Text Image Information Intention Data decode encode Public writing no

    feedback ≠
  46. References

  47. Ageanda 1. Vision 2. Visualization in

  48. 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
  49. Basic plotting functions in {graphics} ・High-level: create a new plot

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

    ・Low-level: add more information ・Interactive: interactively add info.
  51. ・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
  52. ・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
  53. ・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
  54. ・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
  55. ・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
  56. ・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
  57. ・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
  58. 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
  59. Basic plotting functions in {graphics} How to plot multiple data

    series?
  60. 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)
  61. 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)
  62. Basic plotting functions in {graphics} How to custom graphic parameters?

  63. # 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
  64. # 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}
  65. # 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
  66. Basic plotting functions in {graphics} plot data.frame plot(iris)

  67. 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
  68. Basic plotting functions in {graphics} plot(iris) Round robin 2D scatter

    plots plot data.frame
  69. Basic plotting functions in {graphics} plot(iris) Round robin 2D scatter

    plots ONLY plot data.frame
  70. "declarative" graphic package {ggplot2} Basic plotting functions in {graphics} ⇔

    ふつ〜に思ったことを書けば描けるというニュアンス (直訳すると"平叙⽂の"ぐらいの意味)
  71. "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 ふつ〜に思ったことを書けば描ける
  72. # 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}
  73. # 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
  74. Text Image Information Intention Data decode encode Public writing no

    feedback ≠ ① ②
  75. # 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
  76. "declarative" graphic package {ggplot2} ggplot(iris, aes(x = Sepal.Length, y =

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

    Petal.Width, color = Species))+ geom_point() mode: factor
  78. 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
  79. "declarative" graphic package {ggplot2} ggplot(iris, aes(x = Sepal.Length, y =

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

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

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

    Petal.Width, color = Species))+ geom_smooth()+ geom_points()
  83. "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)
  84. "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"))
  85. "declarative" graphic package {ggplot2} ggplot(iris, aes(x = Sepal.Length, y =

    Petal.Width, color = Species))+ geom_smooth()+ geom_points()+ theme_classic()+
  86. "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")
  87. # 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
  88. # 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)
  89. # 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
  90. "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 ふつ〜に思ったことを書けば描ける
  91. Text Image Information Intention Data decode encode Public writing no

    feedback ≠
  92. ・What & Where pathway ・ Image ⇔ Language(multimodality) ・ Spatial

    saliency How we "read" visual images?
  93. Social communication output input/feedback decode encode internal status A B

    estimated B status A status Emotion, Intention, ... = Language, Gesture, Prosody, Face expression...
  94. Before After BeginneR Session BeginneR BeginneR

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

    by standing on the sholders of Giants. -- Sir Isaac Newton, 1676
  96. Enjoy!! KMT©

  97. Bar DraDra KMT©