df_current <- read.csv("C:/Francis_studies/STFX/third_sem/coding_and_health_analytics/project/final_plots/Final_Report_Documents/World Energy Consumption Datasets/electricity_production.csv")
df_current[is.na(df_current)] = 0
electricity_production_current <- df_current[,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_current<-electricity_production_current %>%
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_current <- mutate(electricity_production_current,total_renewable = (biofuel_electricity + hydro_electricity +
nuclear_electricity + solar_electricity +
wind_electricity + other_renewable_electricity))
grouped <- df2_current %>%
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_current, 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)
}
fig1 <- electricity_production_current %>%
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_current %>%
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_current %>%
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_current %>%
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