Também podemos dividir os Gráficos de Dispersão com base em uma variável de grupo. Aqui, vamos usar novamente a variável dos Distritos do início do módulo de aprendizagem:
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()`).
Infelizmente, não é possível ver muita coisa aqui, pois tudo ainda está sendo mostrado em um único gráfico. Para contornar isso, podemos usar facetas!
Também podemos representar quatro gráficos separados em um único. Para isso, usamos a função facet_grid()
:
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()`).
Outra opção é usar a função facet_wrap()
. Ela cria campos uniformes em colunas e linhas, nos quais os gráficos individuais são exibidos:
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()`).
No entanto, agora temos a legenda duplicada, então vamos nos livrar de uma delas:
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()`).
Por fim, também podemos adicionar uma linha de regressão aqui:
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()`).
Vamos avançar para outro tipo de representação de dados pseudo-métricos!