This webpage is a semi-opinionated guide to using ggplot2
for data visualization within social science research. This site was designed as a workshop for graduate students in the social sciences, but folks interested in learning data visualization with R may generally find the code vignettes useful.
Instead of focusing on the basics of the library (which many guides already do really well–see the resources page) before diving into real-world examples, this workshop takes a somewhat different tack by using common social science use cases to motivate code vignettes that then show how to use ggplot in practice.
The key vignettes linked above are focused on use cases where a graphic can be especially useful at conveying an idea to your reader. In other words, these are instances where we have a clear reason to prefer figures to tables. While these use cases are not intended to be exhaustive, they are intended to be somewhat distinct in data and focus from each other.
If you want to follow along on your own computer, you’ll need to have a recent version of R installed along with the a few accompanying software libraries that we’ll use to jump start these vignettes.
First and foremost of these libraries is the tidyverse
meta-library which contains ggplot2
and other useful libraries like dplyr
that will be referenced within the workshop material. We will also use libraries like cobalt
within Case A about covariate balance, fixest
within Case B about design-based methods and sf
within Case C about spatial clustering.
#all cases
library(tidyverse)
#case a
library(cobalt)
#case b
library(fixest)
library(did)
library(broom)
#case c
library(sf)
library(tigris)
library(ggspatial)
library(biscale)
library(cowplot)
library(ggpattern)
#case d
library(snakecase)
library(patchwork)
To get started in an RStudio session, clone the GitHub repository (which includes all of the necessary data) and follow along with the .Rmd files that correspond to the different vignettes.
sessionInfo()
## R version 4.1.3 (2022-03-10)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS Big Sur/Monterey 10.16
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] patchwork_1.1.1 snakecase_0.11.0 ggpattern_0.4.2 cowplot_1.1.1
## [5] biscale_0.2.0 ggspatial_1.1.5 tigris_1.6 sf_1.0-7
## [9] broom_0.7.12 did_2.1.1 fixest_0.10.4 cobalt_4.3.2
## [13] forcats_0.5.1 stringr_1.4.0 dplyr_1.0.8 purrr_0.3.4
## [17] readr_2.1.2 tidyr_1.2.0 tibble_3.1.6 ggplot2_3.3.5
## [21] tidyverse_1.3.1
##
## loaded via a namespace (and not attached):
## [1] nlme_3.1-157 fs_1.5.2 lubridate_1.8.0
## [4] httr_1.4.2 dreamerr_1.2.3 numDeriv_2016.8-1.1
## [7] tools_4.1.3 BMisc_1.4.4 backports_1.4.1
## [10] bslib_0.3.1 rgdal_1.5-29 utf8_1.2.2
## [13] R6_2.5.1 KernSmooth_2.23-20 DBI_1.1.2
## [16] colorspace_2.0-3 withr_2.5.0 sp_1.4-6
## [19] tidyselect_1.1.2 compiler_4.1.3 cli_3.2.0
## [22] rvest_1.0.2 xml2_1.3.3 sandwich_3.0-1
## [25] sass_0.4.1 scales_1.1.1 classInt_0.4-3
## [28] proxy_0.4-26 rappdirs_0.3.3 digest_0.6.29
## [31] foreign_0.8-82 rmarkdown_2.13 pkgconfig_2.0.3
## [34] htmltools_0.5.2 dbplyr_2.1.1 fastmap_1.1.0
## [37] rlang_1.0.2 readxl_1.4.0 rstudioapi_0.13
## [40] jquerylib_0.1.4 generics_0.1.2 zoo_1.8-10
## [43] jsonlite_1.8.0 car_3.0-12 magrittr_2.0.3
## [46] Formula_1.2-4 Rcpp_1.0.8.3 munsell_0.5.0
## [49] fansi_1.0.3 abind_1.4-5 lifecycle_1.0.1
## [52] stringi_1.7.6 yaml_2.3.5 carData_3.0-5
## [55] grid_4.1.3 maptools_1.1-3 crayon_1.5.1
## [58] lattice_0.20-45 haven_2.4.3 hms_1.1.1
## [61] knitr_1.38 pillar_1.7.0 uuid_1.0-4
## [64] ggpubr_0.4.0 ggsignif_0.6.3 reprex_2.0.1
## [67] glue_1.6.2 evaluate_0.15 data.table_1.14.2
## [70] modelr_0.1.8 vctrs_0.4.0 tzdb_0.3.0
## [73] cellranger_1.1.0 gtable_0.3.0 assertthat_0.2.1
## [76] xfun_0.30 e1071_1.7-9 rstatix_0.7.0
## [79] class_7.3-20 units_0.8-0 ellipsis_0.3.2