ggplot
Andy Grogan-Kaylor
2022-10-27
b
to make text bigger. Press
s
to make text smaller.These are simulated data designed to be similar to the data that might come from a social service agency.
The data contain the following (simulated) variables:
ID
, age
, gender
,
race_ethnicity
, family_income
,
program
, mental_health_T1
,
mental_health_T2
, latitude
,
longitude
.
The mental health variables are scaled to have an average of 100. Lower numbers indicate lower mental health, while higher numbers indicate higher mental health.
There are some differences in mental health status in the data and an interesting exercise could be to use software like Excel, Google Sheets, Tableau or R to try to see which factors predict these differences.
ggplot
aes
theticgeom
etryggplot(clients,
aes(x = program,
y = mental_health_T2)) +
stat_summary(fun = mean, # summarizing y
geom = "bar") # with bars
ggplot(clients,
aes(x = program,
y = mental_health_T2)) +
stat_summary(fun = mean, # summarizing y
geom = "bar") + # with bars
theme_minimal()
# install.packages("ggdist")
library(ggdist) # distribution plots
ggplot(clients,
aes(x = program,
y = mental_health_T2)) +
stat_dots() + # dotplot geometry
theme_minimal()
ggplot(clients,
aes(x = program,
y = mental_health_T2)) +
geom_boxplot() + # boxplot geometry
theme_minimal()
e.g. U.N. Blue.
Here the use of color adds aesthetic appeal. We do this by
placing fill
and color
in the
geom
etry.
ggplot(clients,
aes(x = program,
y = mental_health_T2)) +
stat_dots(fill = "#009edb", # fill color
color = "black") + # outline color
theme_minimal()
Compare the minimal and maximal philosophies.
Here, we place fill
color in the aes
thetic
so that color adds additional information, i.e. the gender of
respondents.
(We still retain color
in the geom
to
govern outline color)
ggplot(clients,
aes(x = program,
fill = gender, # color as `information`
y = mental_health_T2)) +
stat_dots(color = "black") + # dotplot geometry
theme_minimal()
viridis
color palette
ggplot(clients,
aes(x = program,
fill = gender, # color as `information`
y = mental_health_T2)) +
stat_dots(color = "black") + # dotplot geometry
scale_fill_viridis_d() + # beautiful colors
theme_minimal()
ggplot(clients,
aes(x = program,
fill = gender, # color as `information`
y = mental_health_T2)) +
stat_dots(color = "black") + # dotplot geometry
scale_fill_viridis_d() + # beautiful colors
coord_flip() + # flip the coordinates
theme_minimal()
facet_wrap()
ggplot(clients,
aes(x = program,
fill = gender, # color as `information`
y = mental_health_T2)) +
stat_dots(color = "black") + # dotplot geometry
scale_fill_viridis_d() + # beautiful colors
coord_flip() + # flip the coordinates
facet_wrap(~neighborhood) + # facet on neighborhood
theme_minimal()
ggplot(clients,
aes(x = program,
fill = gender, # color as `information`
y = mental_health_T2)) +
stat_dots(color = "black") + # dotplot geometry
scale_fill_viridis_d() +
coord_flip() +
labs(title = "Program Enrollment",
subtitle = "By Gender Identity",
caption = "Higher Mental Health Scores Are Better",
y = "Mental Health At Time 2",
x = "Program") +
theme_minimal() +
theme(plot.title = element_text(size = rel(1.5),
color = "darkblue"))