Plastic pollution is a major and growing problem, negatively affecting oceans and wildlife health. Our World in Data has a lot of great data at various levels including globally, per country, and over time. For this lab we focus on data from 2010.
Additionally, National Geographic ran a data visualization communication contest on plastic waste as seen here.
Open the project for this assignment in the course Posit Cloud workspace.
Open the Quarto document lab-02.qmd and render it before making any changes.
Make sure the document renders without errors.
This first render is a useful check that the project, packages, data, and files are all working correctly.
Before we introduce the data, letโs warm up with some simple exercises.
View(plastic_waste) into the Console to do this.Hint: If youโre not sure, run the command
?NA, which will lead you to the documentation.
NA โ what does this mean?๐งถ โ Throughout this lab, remember to render your document regularly. After rendering, check the output to make sure your code, figures, and written responses appear as you expect.
Weโll use the tidyverse package for this analysis. Run the following code to load this package.
If the package is not installed, run the following in your Console:
You only need to install a package once.
Do not put install.packages() commands in the code you render for your assignment.
The dataset for this assignment can be found as a CSV file in the data folder of your project.
You can read it in using the following:
The variable descriptions are as follows:
code: 3 Letter country codeentity: Country namecontinent: Continent nameyear: Yeargdp_per_cap: GDP per capita constant 2011 international $, rateplastic_waste_per_cap: Amount of plastic waste per capita in kg/daymismanaged_plastic_waste_per_cap: Amount of mismanaged plastic waste per capita in kg/daymismanaged_plastic_waste: Tonnes of mismanaged plastic wastecoastal_pop: Number of individuals living on/near coasttotal_pop: Total population according to GapminderLetโs start by taking a look at the distribution of plastic waste per capita in 2010.
## Warning: Removed 51 rows containing non-finite outside the scale
## range (`stat_bin()`).
One country stands out as an unusual observation at the top of the distribution. One way of identifying this country is to filter the data for countries where plastic waste per capita is greater than 3.5 kg/person.
## # A tibble: 1 ร 10
## code entity continent year gdp_per_cap
## <chr> <chr> <chr> <dbl> <dbl>
## 1 TTO Trinidad and Tobago North America 2010 31261.
## plastic_waste_per_cap mismanaged_plastic_waste_per_cap
## <dbl> <dbl>
## 1 3.6 0.19
## # โน 3 more variables: mismanaged_plastic_waste <dbl>,
## # coastal_pop <dbl>, total_pop <dbl>
Did you expect this result? You might consider doing some research on Trinidad and Tobago to see why plastic waste per capita is so high there, or whether this is a data error. If it is a data error, you will want to filter out this observation for the following exercises.
NOTE: From this point onwards the plots and the output of the code are not displayed in the lab instructions, but you can and should run the code and view the results yourself.
Another way of visualizing numerical data is using density plots.
And compare distributions across continents by colouring density curves by continent.
ggplot(
data = plastic_waste,
mapping = aes(
x = plastic_waste_per_cap,
color = continent
)
) +
geom_density()The resulting plot may be a little difficult to read, so letโs also fill the curves in with colours as well.
ggplot(
data = plastic_waste,
mapping = aes(
x = plastic_waste_per_cap,
color = continent,
fill = continent
)
) +
geom_density()The overlapping colours make it difficult to tell whatโs happening with the distributions in continents plotted first, and hence covered by continents plotted over them.
We can change the transparency level of the fill color to help with this.
The alpha argument takes values between 0 and 1: 0 is completely transparent and 1 is completely opaque.
There is no way to tell what value will work best, so you just need to try a few.
ggplot(
data = plastic_waste,
mapping = aes(
x = plastic_waste_per_cap,
color = continent,
fill = continent
)
) +
geom_density(alpha = 0.7)This still doesnโt look greatโฆ
Recreate the density plots above using a different (lower) alpha level that works better for displaying the density curves for all continents.
โ๏ธ Describe why we defined the color and fill of the curves by mapping aesthetics of the plot but we defined the alpha level as a characteristic of the plotting geom.
๐งถ โ Complete Exercises 1โ3 and render the document. Check that your plots appear correctly and that your written responses for Exercises 1 and 3 are included and refer to the output produced by your code.
And yet another way to visualize this relationship is using side-by-side box plots.
ggplot(
data = plastic_waste,
mapping = aes(
x = continent,
y = plastic_waste_per_cap
)
) +
geom_boxplot()Remember: We use geom_point() to make
scatterplots.
โ๏ธ Visualize the relationship between plastic waste per capita and mismanaged plastic waste per capita using a scatterplot. Describe the relationship.
โ๏ธ Colour the points in the scatterplot by continent. Does there seem to be any clear distinction between continents with respect to how plastic waste per capita and mismanaged plastic waste per capita are associated? Discuss.
โ๏ธ Visualize the relationship between plastic waste per capita and total population as well as plastic waste per capita and coastal population. You will need to make two separate plots. Do either of these pairs of variables appear to be more strongly linearly associated? Discuss.
๐งถ โ Complete Exercises 4โ7 and render the document. Check that all of your plots appear correctly and that each exercise marked with โ๏ธ includes a written response that interprets the visualization.
We donโt expect you to complete all of the exercises within the lab session. Ideally, you should have got to this point. If you still have some time left, move on to the remaining exercise below. If not, finish it after the lab session.
If you havenโt had time to finish the exercises above, please ask for help before you leave!
Hint: The x-axis is a calculated variable. One country with plastic waste per capita over 3 kg/day has been filtered out. And the data are not only represented with points on the plot but also a smooth curve. The term โsmoothโ should help you pick which geom to use.
Hint: Not sure how to get the same color scheme? Revisit the slides from this week where we introduce color-blind friendly palettes.
๐งถ โ Complete Exercise 8 and render your document one final time. Read through the rendered document from beginning to end. Make sure all code runs, all figures appear correctly, and every exercise marked with โ๏ธ includes a written response before you submit your work.