Share
Embed
Tokyo.R#73 BeginneRSession3
Tokyo.R#73の初心者セッション3「Plot and Visualization 」のスライドです。
kilometer
October 20, 2018
Transcript
97 slides
-
Who!? 名前: 三村 @kilometer 職業: ポスドク (こうがくはくし) 専⾨: ⾏動神経科学(霊⻑類) 脳イメージング
医療システム⼯学 R歴: ~ 10年ぐらい 流⾏: イタ飯 -
BeginneR Advanced Hoxo_m If I have seen further it is
by standing on the shoulders of Giants. -- Sir Isaac Newton, 1676 -
ブール演算⼦ Boolean Algebra A == B A != B A
| B A & B A %in% B # equal to # not equal to # or # and # is A in B? -
George Boole 1815 - 1864 A Class-Room Introduction to Logic
https://niyamaklogic.wordpress.com/c ategory/laws-of-thoughts/ Mathematician Philosopher & -
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 -
{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 %>% -
{magrittr} # こうやって書く事もできます。 dat <- dat0 %>% f1(var1-1, var1-2) %>%
f2(var2) %>% f3(var3) %>% f4(var4-1, ., var4-2) %>% f5(var5) %>% f6(var6) */ 065 1JQFBMHFCSB %>% -
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... -
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.... -
Text Image First, A. Next, B. Then C. Finally D.
time Intention encode "Frozen" structure A B C D time value α β -
VC Mishkin 1983 TINS, Saur 2008 PNAS Visual pathway of
the brain primary visual cortex -
Saur 2008 PNAS Auditory pathway of the brain VC AC
(repetition) (comprehension) -
AC VC Mishkin 1983 TINS, Saur 2008 PNAS Visual &
auditory pathway of the brain -
AC VC Language Mishkin 1983 TINS, Saur 2008 PNAS Visual
& auditory pathway of the brain -
Text Image First, A. Next, B. Then C. Finally D.
time Intention encode "Frozen" structure A B C D time value α β -
VC SC FEF White 2017 PNAS, Veale 2017 PhilosTransB Visual
saliency & priority hypothesis SC: superior colliculus, FEF: frontal eye field -
VC SC FEF White 2017 PNAS, Veale 2017 PhilosTransB Visual
saliency & priority hypothesis SC: superior colliculus, FEF: frontal eye field -
VC SC FEF White 2017 PNAS, Veale 2017 PhilosTransB Visual
saliency & priority hypothesis SC: superior colliculus, FEF: frontal eye field -
・What & Where pathway ・ Image ⇔ Language(multimodality) ・ Spatial
saliency How we "read" visual images? -
Text Image First, A. Next, B. Then C. Finally D.
time Intention encode "Frozen" structure A B C D time value α β -
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 -
Basic plotting functions in {graphics} ・High-level: create a new plot
・Low-level: add more information ・Interactive: interactively add info. -
Basic plotting functions in {graphics} ・High-level: create a new plot
・Low-level: add more information ・Interactive: interactively add info. -
・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 -
・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 -
・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 -
・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 -
・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 -
・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 -
・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 -
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 -
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) -
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) -
# 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 -
# 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} -
# 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 -
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 -
Basic plotting functions in {graphics} plot(iris) Round robin 2D scatter
plots plot data.frame -
Basic plotting functions in {graphics} plot(iris) Round robin 2D scatter
plots ONLY plot data.frame -
"declarative" graphic package {ggplot2} Basic plotting functions in {graphics} ⇔
ふつ〜に思ったことを書けば描けるというニュアンス (直訳すると"平叙⽂の"ぐらいの意味) -
"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 ふつ〜に思ったことを書けば描ける -
# 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} -
# 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 -
# 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 -
"declarative" graphic package {ggplot2} ggplot(iris, aes(x = Sepal.Length, y =
Petal.Width, color = Species))+ geom_point() -
"declarative" graphic package {ggplot2} ggplot(iris, aes(x = Sepal.Length, y =
Petal.Width, color = Species))+ geom_point() mode: factor -
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 -
"declarative" graphic package {ggplot2} ggplot(iris, aes(x = Sepal.Length, y =
Petal.Width, color = Petal.Length))+ geom_point() continuous volume -
"declarative" graphic package {ggplot2} ggplot(iris, aes(x = Sepal.Length, y =
Petal.Width, color = Species))+ geom_line() -
"declarative" graphic package {ggplot2} ggplot(iris, aes(x = Sepal.Length, y =
Petal.Width, color = Species))+ geom_smooth() -
"declarative" graphic package {ggplot2} ggplot(iris, aes(x = Sepal.Length, y =
Petal.Width, color = Species))+ geom_smooth()+ geom_points() -
"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) -
"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")) -
"declarative" graphic package {ggplot2} ggplot(iris, aes(x = Sepal.Length, y =
Petal.Width, color = Species))+ geom_smooth()+ geom_points()+ theme_classic()+ -
"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") -
# 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 -
# 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) -
# 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 -
"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 ふつ〜に思ったことを書けば描ける -
・What & Where pathway ・ Image ⇔ Language(multimodality) ・ Spatial
saliency How we "read" visual images? -
Social communication output input/feedback decode encode internal status A B
estimated B status A status Emotion, Intention, ... = Language, Gesture, Prosody, Face expression... -
BeginneR Advanced Hoxo_m If I have seen further it is
by standing on the sholders of Giants. -- Sir Isaac Newton, 1676