COMMUNICATING WITH DATA IN THE TIDYVERSE€¦ · DataCamp Communicating with Data in the Tidyverse...
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](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec547344cb03e2ac362f668/html5/thumbnails/1.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec547344cb03e2ac362f668/html5/thumbnails/2.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec547344cb03e2ac362f668/html5/thumbnails/3.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec547344cb03e2ac362f668/html5/thumbnails/4.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec547344cb03e2ac362f668/html5/thumbnails/5.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec547344cb03e2ac362f668/html5/thumbnails/6.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec547344cb03e2ac362f668/html5/thumbnails/7.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec547344cb03e2ac362f668/html5/thumbnails/8.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec547344cb03e2ac362f668/html5/thumbnails/9.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec547344cb03e2ac362f668/html5/thumbnails/10.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec547344cb03e2ac362f668/html5/thumbnails/11.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec547344cb03e2ac362f668/html5/thumbnails/12.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec547344cb03e2ac362f668/html5/thumbnails/13.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec547344cb03e2ac362f668/html5/thumbnails/14.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec547344cb03e2ac362f668/html5/thumbnails/15.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec547344cb03e2ac362f668/html5/thumbnails/16.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec547344cb03e2ac362f668/html5/thumbnails/17.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec547344cb03e2ac362f668/html5/thumbnails/18.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec547344cb03e2ac362f668/html5/thumbnails/19.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec547344cb03e2ac362f668/html5/thumbnails/20.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec547344cb03e2ac362f668/html5/thumbnails/21.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec547344cb03e2ac362f668/html5/thumbnails/22.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec547344cb03e2ac362f668/html5/thumbnails/23.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec547344cb03e2ac362f668/html5/thumbnails/24.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec547344cb03e2ac362f668/html5/thumbnails/25.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec547344cb03e2ac362f668/html5/thumbnails/26.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec547344cb03e2ac362f668/html5/thumbnails/27.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec547344cb03e2ac362f668/html5/thumbnails/28.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec547344cb03e2ac362f668/html5/thumbnails/29.jpg)
DataCamp CommunicatingwithDataintheTidyverse
Let'sproducetheseplots!
COMMUNICATINGWITHDATAINTHETIDYVERSE