COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse...

29
DataCamp Communicating with Data in the Tidyverse Visualizing aspects of data with facets COMMUNICATING WITH DATA IN THE TIDYVERSE Timo Grossenbacher Data Journalist

Transcript of COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse...

Page 1: COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse The fct_reorder function ilo_data # A tibble: 34 x 4 country year hourly_compensation

DataCamp CommunicatingwithDataintheTidyverse

Visualizingaspectsofdatawithfacets

COMMUNICATINGWITHDATAINTHETIDYVERSE

TimoGrossenbacherDataJournalist

Page 2: COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse The fct_reorder function ilo_data # A tibble: 34 x 4 country year hourly_compensation

DataCamp CommunicatingwithDataintheTidyverse

Thefacet_grid()functionilo_data<-ilo_data%>%filter(year=="1996"|year=="2006")

ggplot(ilo_data)+geom_histogram(aes(x=working_hours))+labs(x="Workinghoursperweek",y="Numberofcountries")+facet_grid(.~year)

ilo_data<-ilo_data%>%filter(year=="1996"|year=="2006")

ggplot(ilo_data)+geom_histogram(aes(x=working_hours))+labs(x="Workinghoursperweek",y="Numberofcountries")+facet_grid(year~.)

Page 3: COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse The fct_reorder function ilo_data # A tibble: 34 x 4 country year hourly_compensation

DataCamp CommunicatingwithDataintheTidyverse

Thefacet_grid()functionilo_data<-ilo_data%>%filter(year=="1996"|year=="2006")

ggplot(ilo_data)+geom_histogram(aes(x=working_hours))+labs(x="Workinghoursperweek",y="Numberofcountries")+facet_grid(.~year)

ilo_data<-ilo_data%>%filter(year=="1996"|year=="2006")

ggplot(ilo_data)+geom_histogram(aes(x=working_hours))+labs(x="Workinghoursperweek",y="Numberofcountries")+facet_wrap(facets=~year)

Page 4: COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse The fct_reorder function ilo_data # A tibble: 34 x 4 country year hourly_compensation

DataCamp CommunicatingwithDataintheTidyverse

Afacetedscatterplot

Page 5: COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse The fct_reorder function ilo_data # A tibble: 34 x 4 country year hourly_compensation

DataCamp CommunicatingwithDataintheTidyverse

Stylingfacetedplotsstrip.backgroundstrip.text...

Page 6: COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse The fct_reorder function ilo_data # A tibble: 34 x 4 country year hourly_compensation

DataCamp CommunicatingwithDataintheTidyverse

Definingyourownthemefunctiontheme_green<-function(){theme(plot.background=element_rect(fill="green"),panel.background=element_rect(fill="lightgreen"))}

ggplot(ilo_data)+geom_histogram(aes(x=working_hours))+labs(x="Workinghoursperweek",y="Numberofcountries")+

theme_green()

Page 7: COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse The fct_reorder function ilo_data # A tibble: 34 x 4 country year hourly_compensation

DataCamp CommunicatingwithDataintheTidyverse

Let'spractice!

COMMUNICATINGWITHDATAINTHETIDYVERSE

Page 8: COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse The fct_reorder function ilo_data # A tibble: 34 x 4 country year hourly_compensation

DataCamp CommunicatingwithDataintheTidyverse

Acustomplottoemphasizechange

COMMUNICATINGWITHDATAINTHETIDYVERSE

TimoGrossenbacherDataJournalist

Page 9: COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse The fct_reorder function ilo_data # A tibble: 34 x 4 country year hourly_compensation

DataCamp CommunicatingwithDataintheTidyverse

Page 10: COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse The fct_reorder function ilo_data # A tibble: 34 x 4 country year hourly_compensation

DataCamp CommunicatingwithDataintheTidyverse

Thedotplot

[1]NewYorkTimes(https://www.nytimes.com/2017/11/17/upshot/income-inequality-united-states.html){{0}}

Page 11: COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse The fct_reorder function ilo_data # A tibble: 34 x 4 country year hourly_compensation

DataCamp CommunicatingwithDataintheTidyverse

Dotplotswithggplot2ggplot((ilo_data%>%filter(year==2006)))+geom_dotplot(aes(x=working_hours))+labs(x="Workinghoursperweek",y="Shareofcountries")

Page 12: COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse The fct_reorder function ilo_data # A tibble: 34 x 4 country year hourly_compensation

DataCamp CommunicatingwithDataintheTidyverse

Dotplotswithggplot2:thegeom_path()function

geom_path()connectstheobservationsintheorderinwhichtheyappearinthedata.

?geom_path

ilo_data%>%arrange(country)

#Atibble:34x4countryyearhourly_compensationworking_hours<fctr><fctr><dbl><dbl>1Austria199624.7531.998082Austria200630.4631.817313Belgium199625.2531.653854Belgium200631.8530.211545CzechRep.19962.9439.726926CzechRep.20066.7738.40000#...with28morerows

Page 13: COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse The fct_reorder function ilo_data # A tibble: 34 x 4 country year hourly_compensation

DataCamp CommunicatingwithDataintheTidyverse

Dotplotswithggplot2:thegeom_path()functionggplot()+geom_path(aes(x=numeric_variable,y=numeric_variable))

ggplot()+geom_path(aes(x=numeric_variable,y=factor_variable))

ggplot()+geom_path(aes(x=numeric_variable,y=factor_variable),arrow=arrow(___))

Page 14: COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse The fct_reorder function ilo_data # A tibble: 34 x 4 country year hourly_compensation

DataCamp CommunicatingwithDataintheTidyverse

Let'stryoutgeom_path!

COMMUNICATINGWITHDATAINTHETIDYVERSE

Page 15: COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse The fct_reorder function ilo_data # A tibble: 34 x 4 country year hourly_compensation

DataCamp CommunicatingwithDataintheTidyverse

Polishingthedotplot

COMMUNICATINGWITHDATAINTHETIDYVERSE

TimoGrossenbacherDataJournalist

Page 16: COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse The fct_reorder function ilo_data # A tibble: 34 x 4 country year hourly_compensation

DataCamp CommunicatingwithDataintheTidyverse

Page 17: COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse The fct_reorder function ilo_data # A tibble: 34 x 4 country year hourly_compensation

DataCamp CommunicatingwithDataintheTidyverse

FactorlevelsTheorderoffactorlevelsdeterminetheorderofappearanceinggplot2.

ilo_data$country

[1]AustriaBelgiumCzechRep.Finland[5]FranceGermanyHungary......17Levels:AustriaBelgiumCzechRep.FinlandFrance...UnitedKingdom

Page 18: COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse The fct_reorder function ilo_data # A tibble: 34 x 4 country year hourly_compensation

DataCamp CommunicatingwithDataintheTidyverse

ReorderingfactorswiththeforcatspackageNeedstobeloadedwithlibrary(forcats)

fct_dropfordroppinglevels

fct_revforreversingfactorlevels

fct_reorderforreorderingthem.

[1]Learnmoreattidyverse.org(http://forcats.tidyverse.org/)

Page 19: COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse The fct_reorder function ilo_data # A tibble: 34 x 4 country year hourly_compensation

DataCamp CommunicatingwithDataintheTidyverse

Thefct_reorderfunctionilo_data

#Atibble:34x4countryyearhourly_compensationworking_hours<fctr><fctr><dbl><dbl>1Austria199624.7531.998082Austria200630.4631.817313Belgium199625.2531.653854Belgium200631.8530.211545CzechRep.19962.9439.726926CzechRep.20066.7738.40000

ilo_data<-ilo_data%>%mutate(country=fct_reorder(country,working_hours,mean))

ilo_data$country

[1]AustriaBelgiumCzechRep.Finland[5]FranceGermanyHungary......17Levels:NetherlandsNorwayGermanySweden...CzechRep.

Page 20: COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse The fct_reorder function ilo_data # A tibble: 34 x 4 country year hourly_compensation

DataCamp CommunicatingwithDataintheTidyverse

Thefct_reorderfunction

Page 21: COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse The fct_reorder function ilo_data # A tibble: 34 x 4 country year hourly_compensation

DataCamp CommunicatingwithDataintheTidyverse

Nudginglabelswithhjustandvjustggplot(ilo_data)+geom_path(aes(...))+geom_text(aes(...,hjust=ifelse(year=="2006",1.4,-0.4)))

Page 22: COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse The fct_reorder function ilo_data # A tibble: 34 x 4 country year hourly_compensation

DataCamp CommunicatingwithDataintheTidyverse

Let'spractice!

COMMUNICATINGWITHDATAINTHETIDYVERSE

Page 23: COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse The fct_reorder function ilo_data # A tibble: 34 x 4 country year hourly_compensation

DataCamp CommunicatingwithDataintheTidyverse

Finalizingtheplotfordifferentaudiences

anddevices

COMMUNICATINGWITHDATAINTHETIDYVERSE

TimoGrossenbacherDataJournalist

Page 24: COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse The fct_reorder function ilo_data # A tibble: 34 x 4 country year hourly_compensation

DataCamp CommunicatingwithDataintheTidyverse

Page 25: COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse The fct_reorder function ilo_data # A tibble: 34 x 4 country year hourly_compensation

DataCamp CommunicatingwithDataintheTidyverse

coord_cartesianvs.xlim/ylimggplot_object+coord_cartesian(xlim=c(0,100),ylim=c(10,20))

ggplot_object+xlim(0,100)+ylim(10,20)

Page 26: COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse The fct_reorder function ilo_data # A tibble: 34 x 4 country year hourly_compensation

DataCamp CommunicatingwithDataintheTidyverse

coord_cartesianvs.xlim/ylim

[1]TakenfromRStudioDataVisualizationCheatSheet(https://github.com/rstudio/cheatsheets/raw/master/data-visualization-2.1.pdf)

Page 27: COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse The fct_reorder function ilo_data # A tibble: 34 x 4 country year hourly_compensation

DataCamp CommunicatingwithDataintheTidyverse

Page 28: COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse The fct_reorder function ilo_data # A tibble: 34 x 4 country year hourly_compensation

DataCamp CommunicatingwithDataintheTidyverse

Desktopvs.Mobileaudiences

Page 29: COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse The fct_reorder function ilo_data # A tibble: 34 x 4 country year hourly_compensation

DataCamp CommunicatingwithDataintheTidyverse

Let'sproducetheseplots!

COMMUNICATINGWITHDATAINTHETIDYVERSE