Harold Nelson
1/27/2021
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.0 ✓ purrr 0.3.4
## ✓ tibble 3.0.5 ✓ dplyr 1.0.3
## ✓ tidyr 1.0.2 ✓ stringr 1.4.0
## ✓ readr 1.3.1 ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
Make the figures big enough to read.
Normally an aesthetic is a statement of the form visual attribute = variable. Visual attribute is something you can see like color or size. Variable refers to a column in the data frame.
There are two exceptions to this general pattern.
We may see the visual attribute slot filled by “group.” This is not a visual attribute. It’s an instruction as to how to structure the graph.
In some cases the variable slot is filled by something of the form ..xxxx.. .The two dots indicate that xxxx will not be found in the incoming dataframe, but will be constructed by ggplot2 prior to drawing the graphic.
Look at the facet_wrap example in 4.3, GDP per capita on Five Continents. What happens if you copy this and re-run it? Can you fix the problems?
p = ggplot(data=gapminder,
mapping = aes(x=year,y=gdpPercap))
p + geom_line(aes(group = country),alpha=1) +
geom_smooth(size = 1.1,
method = "loess",
color = "red",
se = FALSE) +
facet_wrap(~continent,ncol=5) +
scale_y_log10(labels=scales::dollar) +
labs(x="year",
y = "GDP per capita",
title = "GDP per capita on Five Continents")
## `geom_smooth()` using formula 'y ~ x'
Can you change The font-size, spacing or angle of the years displayed on the x-axis?
Here is one suggested solution.
Then fiddle around until it looks good to you.
p = ggplot(data=gapminder,
mapping = aes(x=year,y=gdpPercap))
p + geom_line(aes(group = country),alpha=1) +
geom_smooth(size = 1.1,
method = "loess",
color = "red",
se = FALSE) +
facet_wrap(~continent,ncol=5) +
scale_y_log10(labels=scales::dollar) +
labs(x="year",
y = "GDP per capita",
title = "GDP per capita on Five Continents") +
theme(axis.text.x = element_text(angle=45, hjust=1,size = 10))
## `geom_smooth()` using formula 'y ~ x'
Let’s do as Healy suggests and remove the additional layers of the plot one-by-one. Observe what each of these did to the overall graph.
We already know what the theme layer did. Remove that one and the one before that as well. What happens?
p = ggplot(data=gapminder,
mapping = aes(x=year,y=gdpPercap))
p + geom_line(aes(group = country),alpha=1) +
geom_smooth(size = 1.1,
method = "loess",
color = "red",
se = FALSE) +
facet_wrap(~continent,ncol=5) +
scale_y_log10(labels=scales::dollar)
## `geom_smooth()` using formula 'y ~ x'
Remove the layer that scales the y-axis.
p = ggplot(data=gapminder,
mapping = aes(x=year,y=gdpPercap))
p + geom_line(aes(group = country),alpha=1) +
geom_smooth(size = 1.1,
method = "loess",
color = "red",
se = FALSE) +
facet_wrap(~continent,ncol=5)
## `geom_smooth()` using formula 'y ~ x'
Why did this do more than remove the dollar signs?
Remove the facet_wrap.
p = ggplot(data=gapminder,
mapping = aes(x=year,y=gdpPercap))
p + geom_line(aes(group = country),alpha=1) +
geom_smooth(size = 1.1,
method = "loess",
color = "red",
se = FALSE)
## `geom_smooth()` using formula 'y ~ x'
Almost all of the information is lost.
Remove the geom_smooth()
p = ggplot(data=gapminder,
mapping = aes(x=year,y=gdpPercap))
p + geom_line(aes(group = country),alpha=1)
## Change 5
Remove the geom_line().
Now all of the information is gone. We have a useless but valid ggplot object!
Can you center the title? Go back to a previous version of the graph.
p = ggplot(data=gapminder,
mapping = aes(x=year,y=gdpPercap))
p + geom_line(aes(group = country),alpha=1) +
geom_smooth(size = 1.1, method = "loess", se = FALSE) +
facet_wrap(~continent,ncol=5) +
scale_y_log10(labels=scales::dollar) +
labs(x="year",
y = "GDP per capita",
title = "GDP per capita on Five Continents") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(axis.text.x = element_text(angle=45, hjust=1,size = 10))
## `geom_smooth()` using formula 'y ~ x'