Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
carve11 authored Jun 12, 2024
1 parent e730c67 commit 53bd5b3
Show file tree
Hide file tree
Showing 26 changed files with 15,688 additions and 0 deletions.
172 changes: 172 additions & 0 deletions capstone_project_bev.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
options(encoding="UTF-8")
library(tidyverse)
library(lubridate)
library(rjstat)
library(ggplot2)
library(plotly)
library(htmlwidgets)

LINE_SIZE <- 0.8
PLOT_MARGIN <- unit(c(0.5,0.5,0.5,0.5), 'cm')
TITLE_FNT = list(size = 16)
LABEL_FNT = list(size = 14)
AXIS_FNT = list(size = 12)

fpath_root <- "/Users/jonasgravekristensen/Documents/Kursus/Coursera" %>%
paste("Google Data Analytics Certification", sep = "/") %>%
paste("Course 8 - Capstone project", sep = "/")

fpath_data <- paste(fpath_root, "data_capstone_project", sep = "/")
fpath_template <- paste(fpath_root, "templates", sep = "/")

fnames <- list(
"first_time_reg" = "14020_20240610-083200.json",
"ghg_road_traffic_main" = "13931_20240610-085437.json",
"ghg_road_traffic_vehicle_type" = "13931_20240610-085621.json",
"milage_type_fuel" = "12577_20240610-085102.json"
)

df_json <- function(fname) {
f <- paste(fpath_data, fname, sep = "/")
fromJSONstat(f) %>%
mutate_all(type.convert, as.is=TRUE)
}

html_ouput <- function(plot_obj, fname) {
f <- paste(fpath_template, fname, sep = "/")
saveWidget(plot_obj, file = f, selfcontained = FALSE)
}

plotly_style <- function(p_obj) {
p_obj %>% layout(
title = list(font = TITLE_FNT),
xaxis = list(tickfont = AXIS_FNT),
yaxis = list(tickfont = AXIS_FNT, title = list(font = LABEL_FNT)),
legend = list(title = list(font = LABEL_FNT), font = AXIS_FNT)
)
}

# ----------------------------------------
# first registration of passenger cars
# ----------------------------------------
df_first_time_reg <- df_json(fnames["first_time_reg"])

head(df_first_time_reg)
str(df_first_time_reg)
glimpse(df_first_time_reg)

fuel_type <- unique(df_first_time_reg$"type of fuel")

# split `month` column to `year` and `month`,
# convert to numeric values and create date
df_first_time_reg <- separate(df_first_time_reg, month, into=c('year', 'month'), sep='M')
df_first_time_reg <- df_first_time_reg %>%
mutate(date = make_date(year, month))

# calculate total number of cars registred per month
# and calculate fraction of total per fuel type
df_first_time_reg <- df_first_time_reg %>%
group_by(date) %>%
mutate(num_cars_month = sum(value)) %>%
mutate("Share (%)" = round(value/num_cars_month*100, 2))

df_first_time_reg <- filter(
df_first_time_reg,
(`type of fuel` != 'Other fuel')
)

p1 <- ggplot(data = df_first_time_reg) +
geom_line(
mapping = aes(x = date, y = value, color = `type of fuel`),
size = LINE_SIZE
) +
theme(
axis.title.x = element_blank(),
axis.title.y = element_text(margin = margin(r = 5, unit = "pt")),
plot.margin = PLOT_MARGIN,
legend.position='hidden'
) +
labs(
title = "First time registered passenger cars by fuel type",
colour = "Type of fuel",
y="Number of cars"
)
p2 <- ggplot(data = df_first_time_reg) +
geom_line(
mapping = aes(x = date, y = `Share (%)`, color = `type of fuel`),
size = LINE_SIZE
) +
theme(
axis.title.x = element_blank(),
axis.title.y = element_text(margin = margin(r = 15, unit = "pt")),
plot.margin = PLOT_MARGIN,
legend.position='bottom'
) +
labs(colour = "Type of fuel", y="Percent of total (%)")

first_time_reg_p <- subplot(
ggplotly(p1, height = 400) %>% plotly_style(),
style(ggplotly(p2, height = 400) %>% plotly_style(), showlegend = FALSE),
nrows = 2,
shareX = TRUE,
titleY = TRUE
)

html_ouput(first_time_reg_p, "first_time_reg.html")
# ----------------------------------------
# GHG emissions from road traffic
# ----------------------------------------
df_ghg_road_traffic <- df_json(fnames["ghg_road_traffic_main"])

head(df_ghg_road_traffic)
str(df_ghg_road_traffic)
glimpse(df_ghg_road_traffic)

df_ghg_vehicle_type <- df_json(fnames["ghg_road_traffic_vehicle_type"])

head(df_ghg_vehicle_type)
str(df_ghg_vehicle_type)
glimpse(df_ghg_vehicle_type)

# merge the two df
total <- rbind(df_ghg_road_traffic, df_ghg_vehicle_type)

ghg_plot <- ggplot(data = total) +
geom_line(
mapping = aes(x = year, y = value, color = `source (activity)`),
size = LINE_SIZE
) +
theme(axis.title.x = element_blank()) +
labs(
title = "Yearly emissions from road traffic, 2010-2023",
colour = "Emission source", y="Emissions (1000 tonnes CO2e, AR5)") +
scale_color_discrete(
breaks=c('Road traffic', 'Passenger cars', 'Heavy duty vehicles', 'Light duty vehicles', 'Motorcycles and mopeds')
)

html_ouput(ggplotly(ghg_plot, height = 400) %>% plotly_style(), "ghg_plot.html")

# in order to assess whether the change in GHG emissions is due to BEV or a
# change in distance covered we need to view the data of distance
df_milage <- df_json(fnames["milage_type_fuel"])

head(df_milage)
str(df_milage)
glimpse(df_milage)

milage_plot <- ggplot(data = df_milage) +
geom_line(
mapping = aes(x = year, y = value, color = `type of fuel`),
linewidth = LINE_SIZE
) +
theme(
axis.title.x = element_blank(),
axis.text.x = element_text(size = 10),
axis.text.y = element_text(size = 10),
) +
labs(
title = "Yearly distance, passenger cars by fuel type, 2010-2023",
colour = "Fuel type", y="Distance (million km)"
)

html_ouput(ggplotly(milage_plot, height = 300) %>% plotly_style(), "milage_plot.html")
1 change: 1 addition & 0 deletions data_capstone_project/12577_20240610-085102.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":"2.0","class":"dataset","label":"12577: Road traffic volumes, by contents, vehicle type, type of fuel and year","source":"Statistics Norway","updated":"2024-04-03T06:00:00Z","note":["A more detailed model for calculating road traffic volumes was developed in 2018 (see About the statistics). On 21 March 2019, new figures for 2018 and adjusted figures for 2016 and 2017 were published based on the new model. Although the change in method generally has limited effect on the statistics, there are some deviations from previously published figures for 2016 and 2017.","<b>Type of fuel</b><br></br>\n\n<b>Other fuels:</b>\nIncludes paraffin, gas and hydrogen up to and including 2015.<br></br>\n\n<b>Petrol and diesel hybrids:</b>\nPetrol and diesel hybrids were generally registered as regular petrol and diesel vehicles up to and including 2015.","The 2018 figures were updated on 24 March 2020 as a result of changes in the calculation of mileage for vehicles in the municipality of Rindal.","In March 2020, the 40-year-old Motor Vehicle Register of the Norwegian Public Roads Administration was replaced by a new Vehicle Register based on new technology and partly new content. Changes in the use of variables and code lists in the new register have resulted in some discrepancies between 2019 and 2020 at the most detailed levels of the statistics.\n<br>\nPrior to 2020, vehicles on Svalbard not registered for use on public roads were not included in the statistics."],"role":{"time":["Tid"],"metric":["ContentsCode"]},"id":["ContentsCode","Kjoretoytype","DrivstoffType","Tid"],"size":[1,1,4,14],"dimension":{"ContentsCode":{"label":"contents","category":{"index":{"Kjorelengde":0},"label":{"Kjorelengde":"Road traffic volumes (million km)"},"unit":{"Kjorelengde":{"base":"million km","decimals":1}}},"extension":{"elimination":false,"refperiod":{"Kjorelengde":"31.12"},"show":"value"},"link":{"describedby":[{"extension":{"Kjorelengde":"urn:ssb:conceptvariable:vardok:1819"}}]}},"Kjoretoytype":{"label":"vehicle type","category":{"index":{"15":0},"label":{"15":"All passenger cars"}},"extension":{"elimination":false,"show":"value"}},"DrivstoffType":{"label":"type of fuel","category":{"index":{"0":0,"1":1,"2":2,"18":3},"label":{"0":"All types of fuel","1":"Petrol","2":"Diesel","18":"Electricity"}},"extension":{"elimination":true,"eliminationValueCode":"0","show":"value"}},"Tid":{"label":"year","category":{"index":{"2010":0,"2011":1,"2012":2,"2013":3,"2014":4,"2015":5,"2016":6,"2017":7,"2018":8,"2019":9,"2020":10,"2021":11,"2022":12,"2023":13},"label":{"2010":"2010","2011":"2011","2012":"2012","2013":"2013","2014":"2014","2015":"2015","2016":"2016","2017":"2017","2018":"2018","2019":"2019","2020":"2020","2021":"2021","2022":"2022","2023":"2023"}},"extension":{"elimination":false,"show":"code"}}},"extension":{"px":{"infofile":"None","tableid":"12577","decimals":1,"official-statistics":true,"aggregallowed":true,"language":"en","matrix":"Kjorelengde","subject-code":"al"},"contact":[{"name":"Jan Sulavik","phone":"411 80 181","mail":"[email protected]","raw":"Jan Sulavik, Statistics Norway# +47 411 80 181#[email protected]"},{"name":"Geir Martin Pilskog","phone":"408 11 383","mail":"[email protected]","raw":"Geir Martin Pilskog, Statistics Norway# +47 408 11 383#[email protected]"}]},"value":[32737.3,33032,33876.2,33930.5,33841.1,34324.1,34848.5,35406.6,35988.2,35504.3,33872.7,35097.6,35310.2,35675.7,17906.9,16776.7,15799,14922.5,14048.1,13416.1,12172.2,11498.7,10721,9802.8,8605.5,7995,7174.3,6474.7,14813,16225.5,18009.8,18889.2,19478.4,20097.3,20424.7,20443.8,20136.7,19175.2,17393.8,16744.3,15324.7,14147.2,14.9,26.5,62.7,103,301.7,793.6,1163.2,1652.4,2401.9,3331.3,4206.7,5972.6,7994.8,9947.9]}
1 change: 1 addition & 0 deletions data_capstone_project/13931_20240610-085437.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":"2.0","class":"dataset","label":"13931: Greenhouse gases AR5, by contents, source (activity) and year","source":"Statistics Norway","updated":"2024-06-07T06:00:00Z","note":["Due to rounding error, the total may not match the sum of subgroups.","Does not include ocean transport and international air transport. Domestic air transport includes flights made by the Norwegian armed forces.","The table contains CO2 equivalents (GWP-values) according to AR5, the IPCCs Fifth Assessment Report, which are used in the Paris Agreement.","The biofuel mandate for shipping valid from 1 October 2023 is not included in the preliminary figures published on 7 June 2024."],"role":{"time":["Tid"],"metric":["ContentsCode"]},"id":["ContentsCode","UtslpTilLuft","Tid"],"size":[1,1,14],"dimension":{"ContentsCode":{"label":"contents","category":{"index":{"UtslippCO2ekvival":0},"label":{"UtslippCO2ekvival":"Emissions to air (1 000 tonnes CO2-equivalents, AR5)"},"unit":{"UtslippCO2ekvival":{"base":"1 000 tonnes CO2-equivalents","decimals":0}}},"extension":{"elimination":false,"refperiod":{"UtslippCO2ekvival":"During the year"},"show":"value"}},"UtslpTilLuft":{"label":"source (activity)","category":{"index":{"5":0},"label":{"5":"Road traffic"}},"extension":{"elimination":false,"show":"code_value"},"link":{"describedby":[{"extension":{"UtslpTilLuft":"urn:ssb:classification:klass:113"}}]}},"Tid":{"label":"year","category":{"index":{"2010":0,"2011":1,"2012":2,"2013":3,"2014":4,"2015":5,"2016":6,"2017":7,"2018":8,"2019":9,"2020":10,"2021":11,"2022":12,"2023":13},"label":{"2010":"2010","2011":"2011","2012":"2012","2013":"2013","2014":"2014","2015":"2015","2016":"2016","2017":"2017","2018":"2018","2019":"2019","2020":"2020","2021":"2021","2022":"2022","2023":"2023"}},"extension":{"elimination":false,"show":"code"}}},"extension":{"px":{"infofile":"None","tableid":"13931","decimals":0,"official-statistics":true,"aggregallowed":true,"language":"en","matrix":"UtslippCO2ekvival","subject-code":"al"},"contact":[{"name":"Trude Melby Bothner","phone":"408 11 425","mail":"[email protected]","raw":"Trude Melby Bothner, Statistics Norway# +47 408 11 425#[email protected]"},{"name":"Eirik Knutsen","phone":"9848 9464","mail":"[email protected]","raw":"Eirik Knutsen, Statistics Norway# +47 9848 9464#[email protected]"}]},"value":[9991,9928,9956,10004,10216,10249,9984,9116,9356,8721,8334,8695,8684,8009]}
1 change: 1 addition & 0 deletions data_capstone_project/13931_20240610-085621.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":"2.0","class":"dataset","label":"13931: Greenhouse gases AR5, by contents, source (activity) and year","source":"Statistics Norway","updated":"2024-06-07T06:00:00Z","note":["Due to rounding error, the total may not match the sum of subgroups.","Does not include ocean transport and international air transport. Domestic air transport includes flights made by the Norwegian armed forces.","The table contains CO2 equivalents (GWP-values) according to AR5, the IPCCs Fifth Assessment Report, which are used in the Paris Agreement.","The biofuel mandate for shipping valid from 1 October 2023 is not included in the preliminary figures published on 7 June 2024.",": = Confidential. Figures are not published so as to avoid identifying persons or companies."],"role":{"time":["Tid"],"metric":["ContentsCode"]},"id":["ContentsCode","UtslpTilLuft","Tid"],"size":[1,4,14],"dimension":{"ContentsCode":{"label":"contents","category":{"index":{"UtslippCO2ekvival":0},"label":{"UtslippCO2ekvival":"Emissions to air (1 000 tonnes CO2-equivalents, AR5)"},"unit":{"UtslippCO2ekvival":{"base":"1 000 tonnes CO2-equivalents","decimals":0}}},"extension":{"elimination":false,"refperiod":{"UtslippCO2ekvival":"During the year"},"show":"value"}},"UtslpTilLuft":{"label":"source (activity)","category":{"index":{"5.1":0,"5.2":1,"5.3":2,"5.4":3},"label":{"5.1":"Passenger cars","5.2":"Light duty vehicles","5.3":"Heavy duty vehicles","5.4":"Motorcycles and mopeds"}},"extension":{"elimination":false,"show":"code_value"},"link":{"describedby":[{"extension":{"UtslpTilLuft":"urn:ssb:classification:klass:113"}}]}},"Tid":{"label":"year","category":{"index":{"2010":0,"2011":1,"2012":2,"2013":3,"2014":4,"2015":5,"2016":6,"2017":7,"2018":8,"2019":9,"2020":10,"2021":11,"2022":12,"2023":13},"label":{"2010":"2010","2011":"2011","2012":"2012","2013":"2013","2014":"2014","2015":"2015","2016":"2016","2017":"2017","2018":"2018","2019":"2019","2020":"2020","2021":"2021","2022":"2022","2023":"2023"}},"extension":{"elimination":false,"show":"code"}}},"extension":{"px":{"infofile":"None","tableid":"13931","decimals":0,"official-statistics":true,"aggregallowed":true,"language":"en","matrix":"UtslippCO2ekvival","subject-code":"al"},"contact":[{"name":"Trude Melby Bothner","phone":"408 11 425","mail":"[email protected]","raw":"Trude Melby Bothner, Statistics Norway# +47 408 11 425#[email protected]"},{"name":"Eirik Knutsen","phone":"9848 9464","mail":"[email protected]","raw":"Eirik Knutsen, Statistics Norway# +47 9848 9464#[email protected]"}]},"value":[5546,5425,5425,5422,5482,5494,5341,4917,4933,4480,4164,4161,4008,null,1487,1486,1499,1515,1543,1551,1519,1355,1426,1347,1324,1492,1503,null,2825,2884,2895,2926,3043,3051,2969,2688,2846,2748,2694,2897,3037,null,132,133,137,141,149,153,155,156,152,145,151,145,136,null],"status":{"13":":","27":":","41":":","55":":"}}
Loading

0 comments on commit 53bd5b3

Please sign in to comment.