library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.0 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.1 ✔ stringr 1.5.0
## ✔ readr 2.1.2 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
##
## The following object is masked from 'package:ggplot2':
##
## last_plot
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following object is masked from 'package:graphics':
##
## layout
library(dplyr)
df <- read.csv("C:/Francis_studies/STFX/third_sem/coding_and_health_analytics/project/electricity_production.csv")
df[is.na(df)] = 0
electricity_production <- df[,c('continent','year','biofuel_electricity','hydro_electricity',
'nuclear_electricity','solar_electricity',
'wind_electricity','other_renewable_electricity',
"fossil_electricity","renewables_consumption",
"fossil_fuel_consumption","renewables_electricity",
"coal_electricity","gas_electricity","oil_electricity",
"coal_consumption","gas_consumption",'oil_consumption')]
electricity_production <- electricity_production %>%
select( continent,year,
biofuel_electricity,
hydro_electricity,
nuclear_electricity,
solar_electricity,
wind_electricity,
other_renewable_electricity,
renewables_electricity,
fossil_electricity,
coal_electricity,
gas_electricity,
oil_electricity,
renewables_consumption,
fossil_fuel_consumption,
coal_consumption,
gas_consumption,
oil_consumption)
# renewable electricity production
df2 <- mutate(electricity_production,total_renewable = (biofuel_electricity + hydro_electricity +
nuclear_electricity + solar_electricity +
wind_electricity + other_renewable_electricity))
grouped <- df2 %>%
group_by(continent,year) %>%
summarise(renewable_production= sum(total_renewable))
## `summarise()` has grouped output by 'continent'. You can override using the
## `.groups` argument.
oil_coal_gas <- mutate(electricity_production, total_fossil = oil_electricity+gas_electricity+coal_electricity)
grouped1 <- oil_coal_gas%>%
group_by(continent,year)%>%
summarise(production = sum(total_fossil))
## `summarise()` has grouped output by 'continent'. You can override using the
## `.groups` argument.
accumulate_by <- function(dat, var) {
var <- lazyeval::f_eval(var, dat)
lvls <- plotly:::getLevels(var)
dats <- lapply(seq_along(lvls), function(x) {
cbind(dat[var %in% lvls[seq(1, x)], ], frame = lvls[[x]])
})
dplyr::bind_rows(dats)
}
# df3 <- grouped
# fig <- df3 %>%
# filter(between(year,1980,2019), continent %in% c("Asia", "Europe","Africa","Americas","Oceania"))
# fig <- fig %>% accumulate_by(~year)
fig1 <- electricity_production %>%
group_by(continent,year)%>%
summarise(production = sum(renewables_electricity))
## `summarise()` has grouped output by 'continent'. You can override using the
## `.groups` argument.
fig1<-fig1 %>%
filter(between(year,1980,2019), continent %in% c("Asia", "Europe","Africa","Americas","Oceania"))%>%
accumulate_by(~year)
fig2 <- electricity_production %>%
group_by(continent,year)%>%
summarise(consumption = sum(renewables_consumption))
## `summarise()` has grouped output by 'continent'. You can override using the
## `.groups` argument.
fig2<-fig2 %>%
filter(between(year,1980,2019), continent %in% c("Asia", "Europe","Africa","Americas","Oceania"))%>%
accumulate_by(~year)
fig3 <- electricity_production %>%
group_by(continent,year)%>%
summarise(consumption = sum(fossil_fuel_consumption))
## `summarise()` has grouped output by 'continent'. You can override using the
## `.groups` argument.
fig3<-fig3 %>%
filter(between(year,1980,2019), continent %in% c("Asia", "Europe","Africa","Americas","Oceania"))%>%
accumulate_by(~year)
fig4 <- electricity_production %>%
group_by(continent,year)%>%
summarise(production = sum(fossil_electricity))
## `summarise()` has grouped output by 'continent'. You can override using the
## `.groups` argument.
fig4<-fig4 %>%
filter(between(year,1980,2019), continent %in% c("Asia", "Europe","Africa","Americas","Oceania"))%>%
accumulate_by(~year)
fig5 <- grouped1
# fig5 %>%filter(fossil_production_sum > 0)
fig5<-grouped1 %>%
filter(between(year,1980,2019), continent %in% c("Asia", "Europe","Africa","Americas","Oceania"))%>%
accumulate_by(~year)
pltly1 <-plot_ly() %>%
add_trace(
x = ~year,
y = ~production,
split = ~continent,
frame = ~frame,
type = 'scatter',
mode = 'lines',
data = fig4,
opacity = 1.0
) %>%
add_trace(
x = ~year,
y = ~production,
split = ~continent,
frame = ~frame,
type = 'scatter',
mode = 'lines',
data = fig1,
opacity = 0.5
) %>%
animation_opts(
frame = 100,
transition = 0,
redraw = FALSE
) %>%
layout(title = "Electricity production from fossil and renewable sources")%>%
animation_slider(
hide = T
) %>%
animation_button(
x = 1, xanchor = "right", y = 0, yanchor = "bottom"
)
pltly1
pltly2 <-plot_ly() %>%
add_trace(
x = ~year,
y = ~consumption,
split = ~continent,
frame = ~frame,
type = 'scatter',
mode = 'lines',
data = fig2,
opacity = 1.0
) %>%
add_trace(
x = ~year,
y = ~consumption,
split = ~continent,
frame = ~frame,
type = 'scatter',
mode = 'lines',
data = fig3,
opacity = 0.5
) %>%
animation_opts(
frame = 100,
transition = 0,
redraw = FALSE
) %>%
layout(title = "Electricity consumption from fossil and renewable sources")%>%
animation_slider(
hide = T
) %>%
animation_button(
x = 1, xanchor = "right", y = 0, yanchor = "bottom"
)
pltly2