ggplot2ggplot2 include:
ggplot(): the main function to create a plotaes(): the function to specify the mapping of variables to axesgeom_*() or stat_*(): functions to add layers to the plotlabs(): the function to change labels of axes and titletheme(): the function to adjust non-data plot elements such as text size,In this lesson, we will focus on labels, themes, and facets.
labs(): titles and axeslabs() functionWe have the following options for labelling:
ggplot() +
geom_*() +
labs(
x = "Label for x-axis",
y = "Label for y-axis",
color = "Label for color legend",
fill = "Label for fill legend",
title = "Title for the whole plot",
subtitle = "Appears smaller, under the title",
caption = "Appears at the bottom of the plot, good for context"
) +
theme() .rda file.xlsx file, we lose the labels and the SRH level order.
facet_wrap()facet_grid()facet_wrap()srh as the facet variable to create a histogram of age for each level of self-reported healthfacet_grid()srh and diab as the facet variables to create a histogram of age for each level of self-reported health and diabetes statusggplot2: check out the page on complete themes for more!
theme() function
ggplot(
hrs_00,
aes(x = age_yr, y = srh)
) +
geom_boxplot() +
geom_jitter(alpha = 0.2) +
labs(
x = "Age (years)",
y = "Self-reported health",
title = "Self-reported health by age",
) +
theme(
plot.title = element_text(size = 30, face = "bold", color = "blue"),
axis.title.x = element_text(size = 20, face = "italic", color = "red"),
axis.title.y = element_text(size = 20, color = "red"),
axis.text.y = element_text(size = 16, angle = 45)
)ggsave() to save an R object (that’s a plot)Let’s say I saved my plot from Example 8 as plot_age_srh:
R where we want the image to end up in our files
R the name of the plot
ggplot2
ggsave() to save plots as images
ggplot2: it has a lot of capabilities!ggplot2 Cheatsheet in html formggplot2 Cheatsheet in pdf formggsave()ggplot2: https://ggplot2-book.org/ggplot2: https://socviz.co/index.html#preface
Data visualization: Design choices