the tidyverse and ggplot2 - statistics€¦ · cc by rstudio the tidyverse and ggplot2 sungkyu jung...
TRANSCRIPT
CC by RStudio
the Tidyverse and
ggplot2
Sungkyu Jung
excepted from slides at Rstudio.com (00-Tidyverse-webinar)
R Packages
function1()function2()function3()function4()
help help help
p p p p10071007100710071009100910091009p p p
p p p
function5()function6()function7()function8()
help help help
p p p p10071007100710071009100910091009p p p
p p p
function9()functionA()functionB()functionC()
help help help
p p p p10071007100710071009100910091009p p p
p p p
functionD()functionE()functionF()functionG()
help help help
p p p p10071007100710071009100910091009p p p
p p p
function1()function2()function3()function4()
help help help
p p p p10071007100710071009100910091009p p p
p p p
function5()function6()function7()function8()
help help help
p p p p10071007100710071009100910091009p p p
p p p
function9()functionA()functionB()functionC()
help help help
p p p p10071007100710071009100910091009p p p
p p p
functionD()functionE()functionF()functionG()
help help help
p p p p10071007100710071009100910091009p p p
p p p
Base R
function1()function2()function3()function4()
help help help
p p p p10071007100710071009100910091009p p p
p p p
function5()function6()function7()function8()
help help help
p p p p10071007100710071009100910091009p p p
p p p
function9()functionA()functionB()functionC()
help help help
p p p p10071007100710071009100910091009p p p
p p p
functionD()functionE()functionF()functionG()
help help help
p p p p10071007100710071009100910091009p p p
p p p
Base R R Packages
CC by RStudio
Using packages
install.packages("foo")
Downloads files to computer
1
1 x per computer
CC by RStudio
Using packages
install.packages("foo")
Downloads files to computer
1library("foo")
Loads package
2
1 x per computer 1 x per R Session
CC by RStudioDisplay by Adolfo A ́lvarez
The TidyverseA collection of modern R packages that share common philosophies, embed best practices, and are designed to work together.
CC by RStudio
CC by RStudio
tidyverseAn R package that serves as a short cut for installing and loading the components of the tidyverse.
library("tidyverse")
CC by RStudio
install.packages("tidyverse")
does the equivalent ofinstall.packages("ggplot2")install.packages("dplyr")install.packages("tidyr")install.packages("readr")install.packages("purrr")install.packages("tibble")install.packages("hms")install.packages("stringr")install.packages("lubridate")install.packages("forcats")install.packages("DBI")install.packages("haven")install.packages("httr")install.packages("jsonlite")install.packages("readxl")install.packages("rvest")install.packages("xml2")install.packages("modelr")install.packages("broom")
CC by RStudio
install.packages("tidyverse")
does the equivalent ofinstall.packages("ggplot2")install.packages("dplyr")install.packages("tidyr")install.packages("readr")install.packages("purrr")install.packages("tibble")install.packages("hms")install.packages("stringr")install.packages("lubridate")install.packages("forcats")install.packages("DBI")install.packages("haven")install.packages("httr")install.packages("jsonlite")install.packages("readxl")install.packages("rvest")install.packages("xml2")install.packages("modelr")install.packages("broom")
library("tidyverse")
does the equivalent oflibrary("ggplot2")library("dplyr")library("tidyr")library("readr")library("purrr")library("tibble")
CC by RStudio
install.packages("tidyverse")
does the equivalent ofinstall.packages("ggplot2")install.packages("dplyr")install.packages("tidyr")install.packages("readr")install.packages("purrr")install.packages("tibble")install.packages("hms")install.packages("stringr")install.packages("lubridate")install.packages("forcats")install.packages("DBI")install.packages("haven")install.packages("httr")install.packages("jsonlite")install.packages("readxl")install.packages("rvest")install.packages("xml2")install.packages("modelr")install.packages("broom")
library("tidyverse")
does the equivalent oflibrary("ggplot2")library("dplyr")library("tidyr")library("readr")library("purrr")library("tibble")
Six functions • arrange() • filter() • select() • mutate() • summarise() • group_by()
Visualization tools
Grammar of Graphics
CC by RStudio
mpg cyl disp hp
21.0 6 160.0 221.0 6 160.0 222.8 4 108.0 121.4 6 258.0 218.7 8 360.0 318.1 6 225.0 214.3 8 360.0 524.4 4 146.7 122.8 4 140.8 119.2 6 167.6 217.8 6 167.6 216.4 8 275.8 317.3 8 275.8 315.2 8 275.8 310.4 8 472.0 410.4 8 460.0 414.7 8 440.0 432.4 4 78.7 130.4 4 75.7 133.9 4 71.1 1
data geom
CC by RStudio
mpg cyl disp hp
21.0 6 160.0 221.0 6 160.0 222.8 4 108.0 121.4 6 258.0 218.7 8 360.0 318.1 6 225.0 214.3 8 360.0 524.4 4 146.7 122.8 4 140.8 119.2 6 167.6 217.8 6 167.6 216.4 8 275.8 317.3 8 275.8 315.2 8 275.8 310.4 8 472.0 410.4 8 460.0 414.7 8 440.0 432.4 4 78.7 130.4 4 75.7 133.9 4 71.1 1
data geom
mappingsfill
CC by RStudio
mpg cyl disp hp
21.0 6 160.0 221.0 6 160.0 222.8 4 108.0 121.4 6 258.0 218.7 8 360.0 318.1 6 225.0 214.3 8 360.0 524.4 4 146.7 122.8 4 140.8 119.2 6 167.6 217.8 6 167.6 216.4 8 275.8 317.3 8 275.8 315.2 8 275.8 310.4 8 472.0 410.4 8 460.0 414.7 8 440.0 432.4 4 78.7 130.4 4 75.7 133.9 4 71.1 1
fillshape
data geom
mappings
CC by RStudio
mpg cyl disp hp
21.0 6 160.0 221.0 6 160.0 222.8 4 108.0 121.4 6 258.0 218.7 8 360.0 318.1 6 225.0 214.3 8 360.0 524.4 4 146.7 122.8 4 140.8 119.2 6 167.6 217.8 6 167.6 216.4 8 275.8 317.3 8 275.8 315.2 8 275.8 310.4 8 472.0 410.4 8 460.0 414.7 8 440.0 432.4 4 78.7 130.4 4 75.7 133.9 4 71.1 1
shape fillx
data geom
mappings
CC by RStudio
mpg cyl disp hp
21.0 6 160.0 221.0 6 160.0 222.8 4 108.0 121.4 6 258.0 218.7 8 360.0 318.1 6 225.0 214.3 8 360.0 524.4 4 146.7 122.8 4 140.8 119.2 6 167.6 217.8 6 167.6 216.4 8 275.8 317.3 8 275.8 315.2 8 275.8 310.4 8 472.0 410.4 8 460.0 414.7 8 440.0 432.4 4 78.7 130.4 4 75.7 133.9 4 71.1 1
y xshape fill
data geom
mappings
CC by RStudio
mpg cyl disp hp
21.0 6 160.0 221.0 6 160.0 222.8 4 108.0 121.4 6 258.0 218.7 8 360.0 318.1 6 225.0 214.3 8 360.0 524.4 4 146.7 122.8 4 140.8 119.2 6 167.6 217.8 6 167.6 216.4 8 275.8 317.3 8 275.8 315.2 8 275.8 310.4 8 472.0 410.4 8 460.0 414.7 8 440.0 432.4 4 78.7 130.4 4 75.7 133.9 4 71.1 1
xshape filly
pointslines
data geom
mappings
CC by RStudio
mpg cyl disp hp
21.0 6 160.0 221.0 6 160.0 222.8 4 108.0 121.4 6 258.0 218.7 8 360.0 318.1 6 225.0 214.3 8 360.0 524.4 4 146.7 122.8 4 140.8 119.2 6 167.6 217.8 6 167.6 216.4 8 275.8 317.3 8 275.8 315.2 8 275.8 310.4 8 472.0 410.4 8 460.0 414.7 8 440.0 432.4 4 78.7 130.4 4 75.7 133.9 4 71.1 1
xy
linesbars
pointsdata geom
mappings
CC by RStudio
mpg cyl disp hp
21.0 6 160.0 221.0 6 160.0 222.8 4 108.0 121.4 6 258.0 218.7 8 360.0 318.1 6 225.0 214.3 8 360.0 524.4 4 146.7 122.8 4 140.8 119.2 6 167.6 217.8 6 167.6 216.4 8 275.8 317.3 8 275.8 315.2 8 275.8 310.4 8 472.0 410.4 8 460.0 414.7 8 440.0 432.4 4 78.7 130.4 4 75.7 133.9 4 71.1 1
mappingsy
geomdata
linesbars
points
xfill
To make a graph
CC by RStudio
ggplot(data = <DATA>) + <GEOM_FUNCTION>(mapping = aes(<MAPPINGS>))[template]
To make a graph
dataCC by RStudio
ggplot(data = <DATA>) + <GEOM_FUNCTION>(mapping = aes(<MAPPINGS>))
1. Pick a data setmpg cyl disp hp
21.0 6 160.0 221.0 6 160.0 222.8 4 108.0 121.4 6 258.0 218.7 8 360.0 318.1 6 225.0 214.3 8 360.0 524.4 4 146.7 122.8 4 140.8 119.2 6 167.6 217.8 6 167.6 216.4 8 275.8 317.3 8 275.8 315.2 8 275.8 310.4 8 472.0 410.4 8 460.0 414.7 8 440.0 432.4 4 78.7 130.4 4 75.7 133.9 4 71.1 1
To make a graph
CC by RStudio
ggplot(data = <DATA>) + <GEOM_FUNCTION>(mapping = aes(<MAPPINGS>))
1. Pick a data set
2. Choose a geom to display cases
mpg cyl disp hp
21.0 6 160.0 221.0 6 160.0 222.8 4 108.0 121.4 6 258.0 218.7 8 360.0 318.1 6 225.0 214.3 8 360.0 524.4 4 146.7 122.8 4 140.8 119.2 6 167.6 217.8 6 167.6 216.4 8 275.8 317.3 8 275.8 315.2 8 275.8 310.4 8 472.0 410.4 8 460.0 414.7 8 440.0 432.4 4 78.7 130.4 4 75.7 133.9 4 71.1 1
data geom
To make a graphmpg cyl disp hp
21.0 6 160.0 221.0 6 160.0 222.8 4 108.0 121.4 6 258.0 218.7 8 360.0 318.1 6 225.0 214.3 8 360.0 524.4 4 146.7 122.8 4 140.8 119.2 6 167.6 217.8 6 167.6 216.4 8 275.8 317.3 8 275.8 315.2 8 275.8 310.4 8 472.0 410.4 8 460.0 414.7 8 440.0 432.4 4 78.7 130.4 4 75.7 133.9 4 71.1 1
data geom
mappingsfill
CC by RStudio
ggplot(data = <DATA>) + <GEOM_FUNCTION>(mapping = aes(<MAPPINGS>))
1. Pick a data set
2. Choose a geom to display cases
3. Map aesthetic properties to variables