DataCamp Communicating with Data in the Tidyverse Visualizing aspects of data with facets COMMUNICATING WITH DATA IN THE TIDYVERSE Timo Grossenbacher Data Journalist
DataCamp CommunicatingwithDataintheTidyverse
Visualizingaspectsofdatawithfacets
COMMUNICATINGWITHDATAINTHETIDYVERSE
TimoGrossenbacherDataJournalist
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~.)
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)
DataCamp CommunicatingwithDataintheTidyverse
Afacetedscatterplot
DataCamp CommunicatingwithDataintheTidyverse
Stylingfacetedplotsstrip.backgroundstrip.text...
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()
DataCamp CommunicatingwithDataintheTidyverse
Let'spractice!
COMMUNICATINGWITHDATAINTHETIDYVERSE
DataCamp CommunicatingwithDataintheTidyverse
Acustomplottoemphasizechange
COMMUNICATINGWITHDATAINTHETIDYVERSE
TimoGrossenbacherDataJournalist
DataCamp CommunicatingwithDataintheTidyverse
DataCamp CommunicatingwithDataintheTidyverse
Thedotplot
[1]NewYorkTimes(https://www.nytimes.com/2017/11/17/upshot/income-inequality-united-states.html){{0}}
DataCamp CommunicatingwithDataintheTidyverse
Dotplotswithggplot2ggplot((ilo_data%>%filter(year==2006)))+geom_dotplot(aes(x=working_hours))+labs(x="Workinghoursperweek",y="Shareofcountries")
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
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(___))
DataCamp CommunicatingwithDataintheTidyverse
Let'stryoutgeom_path!
COMMUNICATINGWITHDATAINTHETIDYVERSE
DataCamp CommunicatingwithDataintheTidyverse
Polishingthedotplot
COMMUNICATINGWITHDATAINTHETIDYVERSE
TimoGrossenbacherDataJournalist
DataCamp CommunicatingwithDataintheTidyverse
DataCamp CommunicatingwithDataintheTidyverse
FactorlevelsTheorderoffactorlevelsdeterminetheorderofappearanceinggplot2.
ilo_data$country
[1]AustriaBelgiumCzechRep.Finland[5]FranceGermanyHungary......17Levels:AustriaBelgiumCzechRep.FinlandFrance...UnitedKingdom
DataCamp CommunicatingwithDataintheTidyverse
ReorderingfactorswiththeforcatspackageNeedstobeloadedwithlibrary(forcats)
fct_dropfordroppinglevels
fct_revforreversingfactorlevels
fct_reorderforreorderingthem.
[1]Learnmoreattidyverse.org(http://forcats.tidyverse.org/)
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.
DataCamp CommunicatingwithDataintheTidyverse
Thefct_reorderfunction
DataCamp CommunicatingwithDataintheTidyverse
Nudginglabelswithhjustandvjustggplot(ilo_data)+geom_path(aes(...))+geom_text(aes(...,hjust=ifelse(year=="2006",1.4,-0.4)))
DataCamp CommunicatingwithDataintheTidyverse
Let'spractice!
COMMUNICATINGWITHDATAINTHETIDYVERSE
DataCamp CommunicatingwithDataintheTidyverse
Finalizingtheplotfordifferentaudiences
anddevices
COMMUNICATINGWITHDATAINTHETIDYVERSE
TimoGrossenbacherDataJournalist
DataCamp CommunicatingwithDataintheTidyverse
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)
DataCamp CommunicatingwithDataintheTidyverse
coord_cartesianvs.xlim/ylim
[1]TakenfromRStudioDataVisualizationCheatSheet(https://github.com/rstudio/cheatsheets/raw/master/data-visualization-2.1.pdf)
DataCamp CommunicatingwithDataintheTidyverse
DataCamp CommunicatingwithDataintheTidyverse
Desktopvs.Mobileaudiences
DataCamp CommunicatingwithDataintheTidyverse
Let'sproducetheseplots!
COMMUNICATINGWITHDATAINTHETIDYVERSE