History Plotting repeats. Let’s dive into ggplot2
and repeat what we’ve done in base R
using the “grammar of graphics”.
Here’s the Allbus 2021 data again.
library(dplyr)
library(haven)
allbus_2021_dvI <-
read_sav(
"./data/allbus_2021ZA5280_v1-0-0.sav"
) %>%
sjlabelled::set_na(na = c(-1:-99, 97, 98))
pa01
(political_orientation) and fr03b
(children suffer under working mothers).
data + aesthetics + geoms
library(ggplot2)
ggplot(
allbus_2021_dvI,
aes(pa01, fr03b)
) +
geom_point()
## Don't know how to automatically pick scale for object of type haven_labelled/vctrs_vctr/double. Defaulting to continuous.
## Don't know how to automatically pick scale for object of type haven_labelled/vctrs_vctr/double. Defaulting to continuous.
## Warning: Removed 1951 rows containing missing values (geom_point).
base R
function jitter does not have to be added; it just replaces the original geom
using geom_jitter()
ggplot(
allbus_2021_dvI,
aes(pa01, fr03b)
) +
geom_jitter()
## Don't know how to automatically pick scale for object of type haven_labelled/vctrs_vctr/double. Defaulting to continuous.
## Don't know how to automatically pick scale for object of type haven_labelled/vctrs_vctr/double. Defaulting to continuous.
## Warning: Removed 1951 rows containing missing values (geom_point).
Boxplots are boring, right? Try to plot the relationship between pa01
(political_orientation) and fr03b
(children suffer under working mothers) as a violin plot! Have a look here for reference: https://ggplot2.tidyverse.org/reference/
geom
? Try geom_violin()
.
ggplot(
allbus_2021_dvI,
aes(pa01, fr03b)
) +
geom_violin()
## Don't know how to automatically pick scale for object of type haven_labelled/vctrs_vctr/double. Defaulting to continuous.
## Don't know how to automatically pick scale for object of type haven_labelled/vctrs_vctr/double. Defaulting to continuous.
## Warning: Removed 1951 rows containing non-finite values (stat_ydensity).
# In contrast to simple boxplots, violin plots show densities and can, e.g.,
# help to assess whether a variable is normally distributed or not.