第97回Tokyo.Rの初心者セッションでトークした際のスライドです。
#97@kilometer002022.03.19BeginneR Session-- Data Visualization --
View Slide
Who!?誰だ?
Who!?名前: 三村 @kilometer職業: ポスドク (こうがくはくし)専⾨: ⾏動神経科学(霊⻑類)脳イメージング医療システム⼯学R歴: ~ 10年ぐらい流⾏: むし社
宣伝!!(書籍の翻訳に参加しました。)
BeginneR Session
BeginneR
BeginneRAdvancedHoxo_mIf I have seen further it is by standing on theshoulders of Giants.-- Sir Isaac Newton, 1676
Before AfterBeginneR SessionBeginneR BeginneR
"a" != "b"# is A in B?ブール演算⼦ Boolean Algebra[1] TRUE1 %in% 10:100# is A in B?[1] FALSE
George Boole1815 - 1864A Class-Room Introduc2on to Logich7ps://niyamaklogic.wordpress.com/category/laws-of-thoughts/Mathema;cianPhilosopher&
ブール演算⼦ Boolean AlgebraA == B A != BGeorge Boole1815 - 1864A | B A & BA %in% B# equal to # not equal to# or # and# is A in B?wikipedia
Programing
ProgramingWriteRunReadThinkWriteRunReadThinkCommunicateShare
Text ImageInformationIntentionDatadecodeencodeData analysisfeedback≠
TextImageFirst, A. Next, B.Then C. Finally D.timeIntentionencode"Frozen" structureA B C D 8mevalueαβ
σʔλ情報のうち意思伝達・解釈・処理に適した再利⽤可能なもの国際電気標準会議(International Electrotechnical Commission, IEC)による定義
σʔλ情報のうち意思伝達・解釈・処理に適した再利⽤可能なものใ 実存を符号化した表象
σʔλใͷ͏ͪҙࢥୡɾղऍɾॲཧʹదͨ͠࠶ར༻Մೳͳͷใ ࣮ଘΛූ߸Խͨ͠ද࣮ଘ؍ͷ༗ແʹΑΒͣଘࡏ͍ͯ͠Δͷͦͷͷࣸ૾ʢූ߸Խʣ
ࣸ૾Ϧϯΰʢ࣮ଘʣϦϯΰʢใʣmapping
ࣸ૾ (mapping)𝑓: 𝑋 → 𝑌𝑋 𝑌͋Δใͷू߹ͷཁૉΛɺผͷใͷू߹ͷͨͩͭͷཁૉʹରԠ͚ͮΔϓϩηε
ใྔ࣮ଘใσʔλ Ϧϯΰ ූ߸Խ
ใྔ࣮ଘใσʔλ Ϧϯΰ ූ߸Խใྔͷଛࣦ
Ϧϯΰࣸ૾ϑϧʔπ৭ը૾࣮ଘ ใνϟωϧmappingchannel
𝑋𝑌𝑦!𝑥!𝑦"𝑥"𝑋 𝑌𝑥!𝑥"𝑦!𝑦"σʔλՄࢹԽࣸ૾mapping
𝑋𝑌𝑦!𝑥!𝑦"𝑥"𝑋 𝑌𝑥!𝑥"𝑦!𝑦"σʔλՄࢹԽࣸ૾mappingx axis, y axis, color, fill,shape, linetype, alpha…aesthetic channels৹ඒతνϟωϧ
𝑋𝑌𝑦!𝑥!𝑦"𝑥"𝑋 𝑌𝑥!𝑥"𝑦!𝑦"σʔλՄࢹԽࣸ૾mappingx axis, y axis, color, fill,shape, linetype, alpha…aesthetic channels৹ඒతνϟωϧggplot(data = my_data) +aes(x = X, y = Y)) +goem_point()HHQMPUʹΑΔ࡞ਤ
࣮ଘࣸ૾ʢ؍ʣσʔλࣸ૾ʢσʔλՄࢹԽʣάϥϑ𝑋𝑌𝑦!𝑥!𝑦"𝑥"𝑋 𝑌𝑥!𝑥"𝑦!𝑦"EBUBmappingaesthetic channels৹ඒతνϟωϧσʔλՄࢹԽ
ॳΊͯͷHHQMPUlibrary(tidyverse)dat data.frame(tag = rep(c("a", "b"), each = 2),X = c(1, 3, 5, 7),Y = c(3, 9, 4, 2))ggplot() +geom_point(data = dat,mapping = aes(x = X, y = Y))
ॳΊͯͷHHQMPU
ॳΊͯͷHHQMPUlibrary(tidyverse)dat data.frame(tag = rep(c("a", "b"), each = 2),X = c(1, 3, 5, 7),Y = c(3, 9, 4, 2))ggplot() +geom_point(data = dat,mapping = aes(x = X, y = Y))EBUBGSBNFͷࢦఆBFT ؔͷதͰ৹ඒతཁૉͱͯ͠มͱνϟωϧͷରԠΛࢦఆඳը։࢝Λએݴ ه߸Ͱͭͳ͙BFT ؔͷҾ໊EBUͷม໊άϥϑͷछྨʹ߹Θͤͨ[email protected] ؔΛ༻
library(tidyverse)dat data.frame(tag = rep(c("a", "b"), each = 2),X = c(1, 3, 5, 7),Y = c(3, 9, 4, 2))ggplot() +geom_point(data = dat,mapping = aes(x = X, y = Y)) +geom_path(data = dat,mapping = aes(x = X, y = Y))ॳΊ͔ͯΒ൪ͷHHQMPU
ॳΊ͔ͯΒ൪ͷHHQMPU
HHQMPUίʔυͷॻ͖ํͷ৭ʑggplot() +geom_point(data = dat,mapping = aes(x = X, y = Y)) +geom_path(data = dat,mapping = aes(x = X, y = Y))ggplot(data = dat,mapping = aes(x = X, y = Y)) +geom_point() +geom_path()ggplot(data = dat) +aes(x = X, y = Y) +geom_point() +geom_path()ڞ௨ͷࢦఆΛHHQMPU ؔͷதͰߦ͍ɺҎԼলུ͢Δ͜ͱ͕ՄೳNBQQJOHͷใ͕ॻ͔ΕͨBFT ؔΛHHQMPU ؔͷ֎ʹஔ͘͜ͱͰ͖Δ
HHQMPUίʔυͷॻ͖ํͷ৭ʑggplot() +geom_point(data = dat,mapping = aes(x = X, y = Y, color = tag)) +geom_path(data = dat,mapping = aes(x = X, y = Y))ggplot(data = dat) +aes(x = X, y = Y) + # 括り出すのは共通するものだけgeom_point(mapping = aes(color = tag)) +geom_path()ϙΠϯτͷ৭ͷNBQQJOHΛࢦఆ
HHQMPUίʔυͷॻ͖ํͷ৭ʑggplot(data = dat) +aes(x = X, y = Y) +geom_point(aes(color = tag)) +geom_path()ggplot(data = dat) +aes(x = X, y = Y) +geom_path() +geom_point(aes(color = tag))͋ͱ͔ΒͰॏͶͨཁૉ͕લ໘ʹඳը͞ΕΔ
library(tidyverse)dat data.frame(tag = rep(c("a", "b"), each = 2),X = c(1, 3, 5, 7),Y = c(3, 9, 4, 2))g ggplot(data = dat) +aes(x = X, y = Y) +geom_path() +geom_point(mapping = aes(color = tag))HHQMPUը૾ͷอଘggsave(filename = "fig/demo01.png",plot = g,width = 4, height = 3, dpi = 150)
library(tidyverse)dat data.frame(tag = rep(c("a", "b"), each = 2),X = c(1, 3, 5, 7),Y = c(3, 9, 4, 2))g ggplot(data = dat) +aes(x = X, y = Y) +geom_path() +geom_point(mapping = aes(color = tag))HHQMPUը૾ͷอଘggsave(filename = "fig/demo01.png",plot = g,width = 4, height = 3, dpi = 150)αΠζσϑΥϧτͰΠϯν୯ҐͰࢦఆ
library(tidyverse)dat data.frame(tag = rep(c("a", "b"), each = 2),X = c(1, 3, 5, 7),Y = c(3, 9, 4, 2))g ggplot(data = dat) +aes(x = X, y = Y) +geom_path() +geom_point(mapping = aes(color = tag))HHQMPUը૾ͷอଘggsave(filename = "fig/demo01.png",plot = g,width = 10, height = 7.5, dpi = 150,units = "cm") # "cm", "mm", "in"を指定可能
[email protected] ؔ܈ DGIUUQTXXXSTUVEJPDPNSFTPVSDFTDIFBUTIFFUT
ෳͷܥྻΛඳը͢Δ> head(anscombe)x1 x2 x3 x4 y1 y2 y3 y41 10 10 10 8 8.04 9.14 7.46 6.582 8 8 8 8 6.95 8.14 6.77 5.763 13 13 13 8 7.58 8.74 12.74 7.714 9 9 9 8 8.81 8.77 7.11 8.845 11 11 11 8 8.33 9.26 7.81 8.476 14 14 14 8 9.96 8.10 8.84 7.04ggplot(data = anscombe) +geom_point(aes(x = x1, y = y1)) +geom_point(aes(x = x2, y = y2), color = "Red") +geom_point(aes(x = x3, y = y3), color = "Blue") +geom_point(aes(x = x4, y = y4), color = "Green")͜Ε·ͰͷࣝͰؤுΔͱ͜͏ͳΔ
HHQMPUʹΑΔσʔλՄࢹԽ࣮ଘࣸ૾ʢ؍ʣσʔλࣸ૾ʢσʔλՄࢹԽʣάϥϑ𝑋𝑌𝑦!𝑥!𝑦"𝑥"SBXEBUB写像aesthetic channels৹ඒతνϟωϧՄࢹԽʹదͨ͠EBUBܗࣜ変形ਤͷͭͷ৹ඒతνϟωϧ͕σʔλͷͭͷมʹରԠ͍ͯ͠Δ
> head(anscombe)x1 x2 x3 x4 y1 y2 y3 y41 10 10 10 8 8.04 9.14 7.46 6.582 8 8 8 8 6.95 8.14 6.77 5.763 13 13 13 8 7.58 8.74 12.74 7.714 9 9 9 8 8.81 8.77 7.11 8.845 11 11 11 8 8.33 9.26 7.81 8.476 14 14 14 8 9.96 8.10 8.84 7.04> head(anscombe_long)key x y1 1 10 8.042 2 10 9.143 3 10 7.464 4 8 6.585 1 8 6.956 2 8 8.14ggplot(data = anscombe_long) +aes(x = x, y = y, color = key) +geom_point()৹ඒతνϟωϧ Y࣠ Z࣠ ৭ʹରԠ͢ΔมʹͳΔΑ͏มܗݟ௨͠ྑ͘γϯϓϧʹՄࢹԽͰ͖Δ
> head(anscombe)x1 x2 x3 x4 y1 y2 y3 y41 10 10 10 8 8.04 9.14 7.46 6.582 8 8 8 8 6.95 8.14 6.77 5.763 13 13 13 8 7.58 8.74 12.74 7.714 9 9 9 8 8.81 8.77 7.11 8.845 11 11 11 8 8.33 9.26 7.81 8.476 14 14 14 8 9.96 8.10 8.84 7.04> head(anscombe_long)key x y1 1 10 8.042 2 10 9.143 3 10 7.464 4 8 6.585 1 8 6.956 2 8 8.14৹ඒతνϟωϧ Y࣠ Z࣠ ৭ʹରԠ͢ΔมʹͳΔΑ͏มܗanscombe_long pivot_longer(data = anscombe,cols = everything(),names_to = c(".value","key"),names_pattern = "(.)(.)")ԣσʔλॎσʔλ
ggplot(data = anscombe_long) +aes(x = x, y = y, color = key) +geom_point()ggplot(data = anscombe_long) +aes(x = x, y = y, color = key) +geom_point() +facet_wrap(facets = . ~ key, nrow = 1)ਫ४ͰਤΛׂ͢Δ
Wide LongNestedinput outputpivot_longerpivot_widergroup_nestunnestggplotvisualizationmapoutputggsave
Enjoy!!KMT©