title | week | type | subtitle | reading | tasks | |||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Wealth over time |
3 |
Case Study |
Data wrangling plus more advanced ggplot |
|
|
- The ggplot2 vignette{target='blank'}
- R4DS Chapter 3 - Data visualization{target='blank'}
- The Hans Rosling The River of Myths{target='blank'}
- Watch the Hons Rosling video{target="blank"}
- Recreate layered graphics with ggplot including raw and transformed data
- Save graphical output as a .png file
- Save your script as a .R or .Rmd in your course repository
Hans Rosling{target="blank"} is one of the most popular data scientists on the web. His original TED talk{target="blank"} was very popular when it came out. We are going to create some graphics using his formatted data as our weekly case study. Note that we need to remove Kuwait from the data (discussion on this{target="blank"})
In this exercise you will recreate the two graphics shown below using gapminder
dataset from library(gapminder)
(get them to match as closely as you can). Specific instructions/steps are listed in the 'Detailed Steps' section.
- Use
library(ggplot2); library(gapminder); library(dplyr)
to load the necessary packages.- Use
filter()
to remove "Kuwait" from thegapminder
dataset for reasons noted in the background
- Use
- Plot #1 (the first row of plots)
- Use
ggplot()
and thetheme_bw()
to duplicate the first plot using the filtered dataset (without Kuwait) - Specify the appropriate aesthetic mapping (
aes()
) to color by contintent and adjust the size of the point withsize=pop/100000
. Remeber that if you adjust the data like this you will also need to update the legend later. - Use
scale_y_continuous(trans = "sqrt")
to get the correct scale on the y-axis. - Use
facet_wrap(~year,nrow=1)
to divide the plot into separate panels. - Use
labs()
to specify more informative x, y, size, and color keys. - the result will look something like this (with the details filled in):
ggplot(...) + geom_point() + facet_wrap(...) + scale_y_continuous(...) +
theme_bw() + labs(...)
- Use
- Prepare the data for the second plot
- Use
group_by()
to group bycontinent
andyear
- Use
summarize()
with the below commands to calculate the data for the black continent average line on the second plot:gdpPercapweighted = mean(x = gdpPercap, w = pop)
pop = sum(as.numeric(pop))
- Use
- Plot #2 (the second row of plots)
- Use
ggplot()
and thetheme_bw()
to duplicate the second plot. In this plot you will add elements from both the raw gapminder dataset and your dataset summarized by continent. You will need to use the new data you summarized to add the black lines and dots showing the continent average. So it will look something like this:ggplot(gapminder,...) + geom_line() + geom_point() + geom_line(data=newdata,...) +
geom_point(data=newdata,...) + facet_wrap() + theme_bw() + labs(...)
- Use
- Use
ggsave()
orpng()
and save each plot as a.png
with a width of 15 inches - Click
Source
to confirm that your script runs from start to finish without errors and saves the graphics. - Save your script as an .R or .Rmd in your course repository.
It's possible to do some data aggregation like this 'within' a ggplot call using the stat_summary()
and friends. See here for more details.
Adapted from BYU M335 Data Science Course