-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_temp_100.Rmd
More file actions
251 lines (214 loc) · 6.65 KB
/
Copy pathproject_temp_100.Rmd
File metadata and controls
251 lines (214 loc) · 6.65 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
---
title: "project_temp_100"
author: "JosephKBS"
date: "10/14/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
1. Fetching datasets from the files
```{r}
library(ggplot2)
library(ggthemes)
co2emi <- read.csv("./data/annual-co2-emissions-per-country (1).csv")[22851:23019,3:4]
temp_ano <- read.csv("./data/temperature-anomaly.csv")[1:169,2:3]
glotemp <- read.csv("./data/globaltempfrom1880to2019.csv", header = TRUE)[2:141,1:14]
```
2. data cleaning
```{r}
# change to dataframe
df <- data.frame( rbind(co2emi[,2], temp_ano[,2], temp_ano[,1] ))
# transpose
df <- as.data.frame(t(as.matrix(df)))
# rowname
rownames(df)<-c()
colnames(df) <- c("co2", "temp_anomaly", "year")
head(df)
```
3. checking the types of data
```{r}
str(df)
```
4. scaling the data (difference in unit of measurement)
```{r}
df_norm <- scale(df[,1:2])
df_norm <- data.frame( cbind( df_norm, df$year) )
colnames(df_norm) <- c("co2" ,"temp_anomaly", "year")
head(df_norm)
```
5. visualize the data
```{r}
ggplot(df_norm, aes(year))+
geom_line(aes(y=co2, col="co2 emission") ) +
geom_line(aes(y=df_norm$temp_anomaly, col="temp anomaly")) +
labs(title="Global temperature anomaly and co2 emission",
caption="Source: U.S. EPICA, NOAA", x="Years", y="Scaled carbon dioxide emission")+
theme_economist()
```
6. Looking at the new data
```{r}
ggplot(df_norm, aes(x=year, y=temp_anomaly)) +
geom_point( color="blue", alpha=0.3, size=2) +
geom_line(aes(y=co2, col="co2")) +
labs(title="Carbon Dioxide Emission and global temperature anomlay",
subtitle="World wide data",
caption="Source: U.S. EPICA",x="Years", y="Amount(scaled)")+
theme_classic()
```
To check whether the type of the data.
```{r}
df_g <- data.frame( glotemp )
str(df_g)
```
adjust the length of data
```{r}
df_g <- data.frame( glotemp )
df_g_y<- as.numeric(df_g[,1])
df_g_1<- as.numeric(df_g[,2])
df_g_2<- as.numeric(df_g[,3])
df_g_3<- as.numeric(df_g[,4])
df_g_4<- as.numeric(df_g[,5])
df_g_5<- as.numeric(df_g[,6])
df_g_6<- as.numeric(df_g[,7])
df_g_7<- as.numeric(df_g[,8])
df_g_8<- as.numeric(df_g[,9])
df_g_9<- as.numeric(df_g[,10])
df_g_10<- as.numeric(df_g[,11])
df_g_11<- as.numeric(df_g[,12])
df_g_12<- as.numeric(df_g[,13])
df_g_a<- as.numeric(df_g[,14])
```
creating dataset
```{r}
df_g <- data.frame( rbind(df_g_y, df_g_1, df_g_2, df_g_3, df_g_4, df_g_5, df_g_6, df_g_7, df_g_8, df_g_9,df_g_10,df_g_11, df_g_12, df_g_a) )
df_g <- as.data.frame(t(as.matrix(df_g)))
rownames(df_g)<-c()
colnames(df_g) <- c("year","jan","feb","mar","apr","may","june","july","aug","sep","oct","nov","dec","ave")
df_g <- df_g[1:139,]
head(df_g)
str(df_g)
```
creating a new dataframe
```{r}
new_data <- data.frame(matrix(NA, nrow=12*139, ncol=3 ))
for (i in 1:139){
for(j in 1:12){
new_data[i+(139)*(j-1), 1] <- 1879+i
new_data[i+(139)*(j-1), 2] <- df_g[i,j+1]
new_data[i+(139)*(11), 2] <- df_g[i, 12]
new_data[i+(139)*0,3] <- "Jan"
new_data[i+(139)*1,3] <- "Feb"
new_data[i+(139)*2,3] <- "Mar"
new_data[i+(139)*3,3] <- "Apr"
new_data[i+(139)*4,3] <- "May"
new_data[i+(139)*5,3] <- "Jun"
new_data[i+(139)*6,3] <- "Jul"
new_data[i+(139)*7,3] <- "Aug"
new_data[i+(139)*8,3] <- "Sep"
new_data[i+(139)*9,3] <- "Oct"
new_data[i+(139)*10,3] <- "Nov"
new_data[i+(139)*11,3] <- "Dec"
}
}
ridge <- new_data
ridge <- ridge[,1:3]
colnames(ridge) <- c("Year","Temp","Month")
ridge$year <- as.integer(ridge$Year)
ridge$temp <- as.numeric(ridge$Temp)
ridge$month <- as.factor(ridge$Month)
head(ridge)
str(ridge)
```
Creating a ridge plot
```{r}
library(ggrepel)
library(grid)
library(ggridges)
p1 <- ggplot(ridge[,1:3], aes(x=Temp, y = as.factor(Year),
fill = stat(x))) +
geom_density_ridges_gradient(scale = 40, rel_min_height = 0.01) +
scale_fill_viridis_c(name = "Mean.[F]", option = "A") +
xlim(-0.8, 1.5) +
theme_classic() +
labs(title = 'Increasing Mean Temperature',
subtitle="Changes in Global Monthly Mean Temperature",
# caption="Source: NOAA National Climate Research Unit"
x="Mean Temperatures", y=" Years (1880-2018) ") +
theme(
#axis.text.x = element_blank(),
#axis.ticks.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank()) +
annotate(geom="text", x=1.3, y=10, label="1880",
color="black", size=5) +
annotate(geom="text", x=1.3, y=160, label="2018",
color="black", size=5)
p1
```
scale the co2 emission data
```{r}
co2emi <- read.csv("./data/annual-co2-emissions-per-country (1).csv")[22851:23019,3:4]
dim(co2emi)
co2emi <- co2emi[31:169,] # from 1880 to 2018
head( co2emi )
#scaling co2 emission data
co2_scale <- scale(co2emi[,2])
co2 <- data.frame( cbind( co2emi, co2_scale) )
colnames(co2) <- c("Year" ,"co2", "co2_scale")
head(co2)
co2_table <- data.frame(matrix(NA, nrow=12*139, ncol=3 ))
for ( i in 1:139){
for(j in 1:12){
co2_table[i+(139)*(j-1), 1] <- co2[i,1]
co2_table[i+(139)*(j-1), 2] <- co2[i,2]
co2_table[i+(139)*(j-1), 3] <- co2[i,3]
}
}
head(co2_table)
colnames(co2_table) <- c("Year" ,"co2", "co2_scale")
# ridge dataset scaling (monthly temp)
ridge <- ridge[,1:3]
ridge_scale <- scale(ridge$Temp)
ridge_data <- data.frame( cbind( ridge, ridge_scale) )
colnames(ridge_data) <- c("Year","Temp","Month","Scale")
head(ridge_data)
```
# ordering month from Jan to Dec
```{r}
unq <- unique(ridge_data$Month)
ridge_data$Month <- factor(unq, levels = month.abb)
```
# plotting monthly temperature anomaly compared to co2 emission.(scaled)
```{r}
p <- ggplot(ridge_data, aes(x=Year, y=Scale, color=factor(Month))) +
geom_point() +
scale_colour_tableau(palette="Classic Purple-Gray 12") +
# scale_fill_discrete( labels = c("Month")) +
labs(title="Increasing Carbon Dioxide emission and global temperature",
subtitle="Total amount of carbon dioxide emission and monthly temperature anomaly compared to 1950-1980.",
x="Years",
y="Temperature anomaly & co2 emission(Metric Ton, scaled)" ) + theme_classic()
p2 <- p + geom_line(aes(y=co2_table$co2_scale, col="Co2_Emission"),
size=1.5, color="#990000", label="co2") +
annotate(
geom = "curve", x = 1970, y = 2,
xend = 1976, yend = 0.7,
curvature = .3,
arrow = arrow(length = unit(2, "mm")),
color="red", size=1.1
) +
annotate(geom = "text",
x = 1950,
y = 2.3,
label = "Total Co2 Emission",
hjust = "left",
color="red")
p2
```
# adding up two plots
description : https://cran.r-project.org/web/packages/ggpubr/ggpubr.pdf
```{r}
library(ggpubr)
ggarrange(p1, p2, widths = c(1.5,2))
```