-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlab-02.Rmd
More file actions
187 lines (142 loc) · 5.83 KB
/
Copy pathlab-02.Rmd
File metadata and controls
187 lines (142 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
---
title: "Lab 02 - Plastic waste"
author: "Cailey Fay "
date: "10.6.25"
output: github_document
---
## Load packages and data
```{r load-packages, message=FALSE}
library(tidyverse)
```
```{r load-data, message=FALSE}
plastic_waste <- read.csv("data/plastic-waste.csv")
```
## Exercises
Warm up: Top left pane is for scripts / syntax / markdown files. Bottom left is the console where you can punch in commands and R spits stuff out. Top right pane is the environment, lets you see what data files are in operation, and under the git tab you can make your way to committing changes. Bottom right pane is where you can see plots, info on how to use a particular function or package, and for accessing files.
There are 240 observations in the plastic waste dataset.
### Exercise 1
To get the distribution of plastic waste per capita:
```{r plastic-waste-continent}
#plain old histogram of plastic waste per capita
ggplot(data = plastic_waste, aes(x = plastic_waste_per_cap)) +
geom_histogram(binwidth = 0.2)
```
To see the countries where it is > 3.5 kg/person:
```{r}
plastic_waste%>%
filter(plastic_waste_per_cap > 3.5)
#creating the df that filters out the outliers
df_filtered <- plastic_waste %>%
filter(plastic_waste_per_cap < 3.5)
```
1.1 Plotting the distribution of plastic waste per capita faceted by continent.
```{r}
ggplot(data = df_filtered,
aes(x=plastic_waste_per_cap)) +
geom_histogram(binwidth = .1) +
facet_wrap(~continent)
```
Conclusion: African and Asian counties are positively skewed, and tend to have lower plastic waste per cap than North America and Europe. Less can be said about Oceania and South America, since there are low frequencies / not a lot of countries with data points.
With density plots
```{r}
ggplot(data = df_filtered,
aes(x=plastic_waste_per_cap)) +
geom_density()
#getting fancier
ggplot(data=df_filtered,
mapping = aes(x=plastic_waste_per_cap,
color = continent)) +
geom_density()
#even fancier
ggplot(data=df_filtered,
mapping = aes(x=plastic_waste_per_cap,
color = continent,
fill = continent)) + geom_density()
#Better but not quite there yet
ggplot(data=df_filtered,
mapping = aes(
x=plastic_waste_per_cap,
color = continent,
fill = continent)) +
geom_density(alpha=0.7)
```
### Exercise 2
2.1 Recreating the plots with lower alpha
```{r plastic-waste-density}
ggplot(data=df_filtered,
mapping = aes(
x=plastic_waste_per_cap,
color = continent, fill = continent)) +
geom_density(alpha=.2)
```
2.2 Color and fill are aesthetics, but alpha is part of geom_density because of the way that the layering occurs to make the graph. The density part is adding on to the plot, and the alpha is directly related to the "see-through-ness" of this new addition.
### Exercise 3
To do box plots
```{r}
ggplot(
data = plastic_waste,
mapping = aes(
x = continent,
y = plastic_waste_per_cap
)
) +
geom_boxplot()
```
3.1 Violin plots tell us more about the shape of the distribution than box plots. Box plots mainly show us how spread out the data is overall, and where the median/quartiles/outliers are. While we get more of a sense of the shape with the violin plots, we do not know where the median falls, or other specific descriptive information.
```{r plastic-waste-violin}
ggplot(
data= df_filtered,
mapping = aes(
x = continent,
y = plastic_waste_per_cap
)
) +
geom_violin()
```
### Exercise 4
4.1 Relationship between plastic waste per capita and mismanaged plastic waste per capita
```{r plastic-waste-mismanaged}
ggplot(data=df_filtered,
mapping= aes(
x=plastic_waste_per_cap,
y=mismanaged_plastic_waste_per_cap) ) + geom_point()
```
There is somewhat of a positive relationship between plastic waste per cap and mismanaged plastic waste per cap. As plastic waste increases, the proportion of mismanaged plastic waste also increases, however, there are some countries that have a lot of plastic waste but mismanage very little of it.
4.2: European and north american countries tend to have less mismanaged plastic waste per cap, whereas African and Asian countries appear to mismanage plastic waste more.
```{r plastic-waste-mismanaged-continent}
ggplot(data=df_filtered,
mapping= aes(x=plastic_waste_per_cap,
y=mismanaged_plastic_waste_per_cap,
color=continent) ) + geom_point()
```
4.3
Without filtering out the asian countries that have massive outlier level total populations, there isn't a massive difference based on coastal vs total population. The correlation for the total pop and plastic waste per cap is -.09, whereas it is -.12 for coastal pop and plastic waste per cap. Therefore the coastal pop / plastic waste relationalship is stronger.
```{r plastic-waste-population-total}
ggplot(data=df_filtered,
mapping= aes(
x=plastic_waste_per_cap,
y=total_pop,
color=continent) ) + geom_point()
ggplot(data=df_filtered,
mapping= aes(
x=plastic_waste_per_cap,
y=coastal_pop,
color=continent) ) + geom_point()
cor(df_filtered$plastic_waste_per_cap, df_filtered$total_pop)
cor(df_filtered$plastic_waste_per_cap, df_filtered$coastal_pop)
```
### Exercise 5
Here is my attempt. I don't think I picked out the right "smooth" function, but I've invested as much time as I can already.
```{r}
CoastalProportion <- df_filtered %>%
mutate(CP = coastal_pop / total_pop)
ggplot(data = CoastalProportion,
mapping = aes(x= CP,
y=plastic_waste_per_cap)) +
geom_point(aes(color = continent)) +
geom_smooth() +
labs(title= "Plastic Waste v Coastal Population Proportion",
subtitle= "I did it!",
x="Coastal Proportion",
y= "Plastic Waste Per Capita")
```