We can also divide Scatterplots based on a grouping variable. Here we use the variable of Districts from the beginning of the learning module:
groupscatter <- ggplot(
pss,
aes(
trstplt,
trstlgl,
color = district
)
) +
geom_point() +
geom_jitter(
width = 1,
height = 1
) +
scale_color_manual(
name = "Distrikte",
values = beyonce_palette(25)
) +
scale_x_continuous(
breaks = seq(
0,
10,
1
)
) +
scale_y_continuous(
breaks = seq(
0,
10,
1
)
) +
labs(
x = "Trust in Politicians",
y = "Trust in Legal System",
title = "Group Scatterplot"
)
groupscatter
## Warning: Removed 23 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Removed 23 rows containing missing values or values outside the scale range
## (`geom_point()`).
Unfortunately, there is not much to see here as everything is still displayed within one plot. To avoid this, we can use facets!
However, we can also display four individual plots in one. To do this, we use the facet_grid()
function:
groupscatter +
facet_grid(. ~ district)
## Warning: Removed 23 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Removed 23 rows containing missing values or values outside the scale range
## (`geom_point()`).
Another way is possible with the facet_wrap()
function. This creates evenly spaced fields in both columns and rows, where the individual plots are then displayed:
groupscatter +
facet_wrap(~district)
## Warning: Removed 23 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Removed 23 rows containing missing values or values outside the scale range
## (`geom_point()`).
Now we have the legend duplicated, so we want to get rid of one:
groupscatter2 <- ggplot(
pss,
aes(
trstplt,
trstlgl,
color = district
)
) +
geom_point() +
geom_jitter(
width = 1,
height = 1
) +
scale_color_manual(values = beyonce_palette(25)) +
scale_x_continuous(
breaks = seq(
0,
10,
1
)
) +
scale_y_continuous(
breaks = seq(
0,
10,
1
)
) +
labs(
x = "Trust in Politicians",
y = "Trust in Legal System",
title = "Group Scatterplot"
) +
facet_wrap(~ district) +
theme(legend.position = "none")
groupscatter2
## Warning: Removed 23 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Removed 23 rows containing missing values or values outside the scale range
## (`geom_point()`).
Finally, we can also add a regression line here:
groupscatter2 +
geom_smooth(
method = "lm",
se = TRUE,
color = "darkgray"
)
## Warning: Removed 23 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 23 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Removed 23 rows containing missing values or values outside the scale range
## (`geom_point()`).
Let’s move on to another type of visualization for pseudo-metric data!