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 GESIS Panel data again.

library(dplyr)
library(haven)

gp_covid <- 
  read_sav(
    "./data/ZA5667_v1-1-0.sav"
  ) %>% 
  sjlabelled::set_na(na = c(-1:-99, 97:98))

1

Create a scatter plot for the variables political_orientation and hzcy001a.
Remember the order of layers: data + aesthetics + geoms
library(ggplot2)

ggplot(
  gp_covid,
  aes(political_orientation, hzcy001a)
) +
  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.

2

Add some jitter to the scatter plot.
In contrast to the base R function jitter does not have to be added; it just replaces the original geom using geom_jitter()
ggplot(
  gp_covid,
  aes(political_orientation, hzcy001a)
) +
  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.

3

Boxplots are boring, right? Try to plot the relationship between hzcy001a and political_orientation as a violin plot! Have a look here for reference: https://ggplot2.tidyverse.org/reference/

What’s the difference to a boxplot?
You can’t find the proper geom? Try geom_violin().
ggplot(
  gp_covid,
  aes(political_orientation, hzcy001a)
) +
  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.

# In contrast to simple boxplots, violin plots show densities and can, e.g.,
# help to assess whether a variable is normally distributed or not.