Make a plot with ggplot
February 3, 2025
ggplotggplot ThemesUse theme() if you want to tweak the display of an existing theme.
The following lists basic ggplot themes:
| theme | theme |
|---|---|
| theme_grey() | theme_gray() |
| theme_bw() | theme_linedraw() |
| theme_light() | theme_dark() |
| theme_minimal() | theme_classic() |
| theme_void() | theme_test() |
ggthemes package provides the following themes:
theme_economist(), theme_wsj(), theme_fivethirtyeight(), theme_gdocs(), theme_map()hrbrthemes package provides the following themes:
theme_ipsum(), theme_ipsum_rc(), theme_ft_rc(), and more.gapminder datagapminder package include the gapminder data frame.ggplot function what our data is and what our aesthetic mapping is:p <- ggplot(data = gapminder)
p <- ggplot(data = gapminder,
mapping = aes(x = gdpPercap,
y = lifeExp))geom_*()) to the ggplot().ggplot() function what our tidy data is.ggplot(data = ...)ggplot() what relationships we want to see.ggplot(mapping = aes(...))p <- ggplot(data = ... , aes(...))ggplot() how we want to see the relationships in our data.geom_*().geoms as needed, by adding them to the p object one at a time.color to continent in the aes() function:color to "purple" outside of the aes() function:aes() function and inside geom_*() function.scale_*() and labs() functions to ggplot()p + geom_point(alpha = 0.3) +
geom_smooth(method = "gam") +
scale_x_log10(labels = scales::dollar) +
labs(x = "GDP Per Capita", y = "Life Expectancy in Years",
title = "Economic Growth and Life Expectancy",
subtitle = "Data points are country-years",
caption = "Source: Gapminder.")ggsave() to save ggplot output as a .png or .pdf file.ggave() that we want to save that object.