Visualizing electricity in Costa Rica using highcharter and plotly

Data Acquisition and Introduction

library(plyr)
library(dplyr)
library(plotly)
library(highcharter)
library(lubridate)
library(viridis)
elec <- read.csv("SEN oct-2016.csv",header = T,stringsAsFactors = F)
str(elec)
## 'data.frame': 60264 obs. of 9 variables:
## $ Planta..CENCE. : chr "Aeroenergia" "Aeroenergia" "Aeroenergia" "Aeroenergia" ...
## $ Hora : int 0 1 2 3 4 5 6 7 8 9 ...
## $ Dia : chr "10/1/2016" "10/1/2016" "10/1/2016" "10/1/2016" ...
## $ Mes : int 10 10 10 10 10 10 10 10 10 10 ...
## $ Ano : int 2016 2016 2016 2016 2016 2016 2016 2016 2016 2016 ...
## $ Tipo.de.Empresa : chr "Privada" "Privada" "Privada" "Privada" ...
## $ Fuente : chr "Eolica" "Eolica" "Eolica" "Eolica" ...
## $ Tipo.de.Fuente : chr "Eolica" "Eolica" "Eolica" "Eolica" ...
## $ Produccion..MWh.: num 0.9639 0.0452 0.4453 1.0051 1.6287 ...
<br />elec <- filter(elec,Tipo.de.Empresa!="#N/A")
elec <- filter(elec,Tipo.de.Fuente!="#N/A")
#elec$Dia <- paste(elec$Dia,elec$Hora,":00")
elec$Hora <- gsub(" ","",paste(elec$Hora,":00"))
add_zero <- function(text){
if(nchar(text)==4){
return(gsub(" ","",paste("0",text)))
}
else{
return(text)
}
}
elec$Hora<- unlist(lapply(elec$Hora,add_zero))

Costa Rica is a country in Central America which is bordered by Nicaragua to the north and Panama to the South. It gained independence in 1847  and has remained one of the most progressive , stable and prosperous nations in the Americas. It also belongs to a group of countries that do not have a standing army.

Recently, a data set was posted on Kaggle which catered to the electrical energy production of Costa Rica for the period of October 2016. I wanted to see what I could learn about Costa Rica’s energy production through one month’s worth of data.

The above data tells us about the electrical production/consumption by Power Grid companies in Costa Rica. The data consists of rows that contain N/A values which have been removed using the filter() function.
Let’s start diving into the dataset by asking ourselves a few questions about

  • Private and Public Sector Firms
  • How production/consumption vary over time
  • What are the major sources of energy?

Private vs Public

Number of Private and Public Sector companies

elec%>%select(Tipo.de.Empresa,Planta..CENCE.)%>% distinct() %>% group_by(Tipo.de.Empresa) %>% summarise(n=n()) %>%
hchart("column",x=Tipo.de.Empresa,y=n) %>%
hc_title(text="Number of Private and Public Companies")

private_public.png

There are more private companies compared to public companies that provide electricity in Costa Rica.

Average Production

elec %>% group_by(Tipo.de.Empresa) %>% summarise(n=mean(Produccion..MWh.)) %>%
hchart("column",x=Tipo.de.Empresa,y=n) %>%
hc_title(text="Average Production of Electricity")

avergae_public_private.png

We see that on average, public sector companies produce more electricity compared to private sector companies.
Let’s look at this further by getting day wise statistic.

Day wise plot

elec$Dia %>% group_by(Tipo.de.Empresa,day) %>% summarise(n=mean(Produccion..MWh.)) %>% hchart("column",x=day,y=n,group=Tipo.de.Empresa)%>% hc_add_theme(hc_theme_db())

day_wise_public_private.png

Average production of electricity by public sector companies went down during the weekends. Average production by private sector companies went up on Sundays.

Time Series Representaion

elec %>% group_by(Dia,Tipo.de.Empresa) %>% summarise(n=mean(Produccion..MWh.)) %>% ggplot(aes(x=Dia, y=n, colour=Tipo.de.Empresa)) +
geom_line()

time_series_pub_pri

ggplotly()

Here we use the ggplotly() function to make the ggplot2 plot object interactive. This allows one to make more informed decisions . We have not included an interactive plot currently, but feel free to examine it at the link provided below.

 

Average production went the lowest on the 10th of October 2016 for public sector power grid companies. The lowest for private companies was on the 3rd of October 2016.

Composition of the Source of Energy

ax <- list(
zeroline=FALSE,
showline=FALSE,
showticklabels=FALSE,
showgrid=FALSE
)
privada <- elec %>% group_by(Tipo.de.Empresa,Tipo.de.Fuente) %>% summarise(n=sum(Produccion..MWh.)) %>% filter(Tipo.de.Empresa=="Privada",n>0) %>% mutate(Percentage=100*(n/sum(n)))
publica <- elec %>% group_by(Tipo.de.Empresa,Tipo.de.Fuente) %>% summarise(n=sum(Produccion..MWh.)) %>% filter(Tipo.de.Empresa=="Publica",n>0) %>% mutate(Percentage=100*(n/sum(n)))
plot_ly() %>%
add_pie(data = privada,
labels=privada$Tipo.de.Fuente,
values = privada$Percentage,
name = "Private",
domain = list(x = c(0.52, 1), y = c(0.5, 1)))%>%
add_pie(data = publica,
labels=publica$Tipo.de.Fuente,
values = publica$Percentage,
name = "Public",
domain = list(x = c(0, 0.48), y = c(0.5, 1))) %>%
layout(title = "Public and Private",
xaxis=ax,
yaxis=ax)

public_private_pie.png

A large percentage of production by public sector was from renewable resources such as wind energy and geothermal energy.According to a report, about 90% of Costa Rica’s electrical energy needs were satisfied via renewable resources.

Type of Production

Time Series

colors <- c("#487098","#484898","#34348d","#19198b","#208582","#942f59","#dd125b","#000000","#c61051","#000FFF")
elec %>% group_by(Tipo.de.Fuente,Dia) %>% summarise(n=sum(Produccion..MWh.)) %>% filter(Tipo.de.Fuente!="#N/A")%>%
ggplot(aes(x=Dia, y=n, colour=Tipo.de.Fuente)) +
geom_line()+scale_color_manual(values=colors)
ggplotly()

type_prod_time.png

ggplotly()

As per the above plot, most energy production was through the use of water resources.Hydroelectricity is the largest source of electricity in Costa Rica as seen from the pie charts above.This is then followed by geothermal,biomass and solar resources.Costa Rica has a geographical advantage over other countries due to the rich presence of water bodies.

Day Wise

colors <- c("#487098","#484898","#34348d","#19198b","#208582","#942f59","#dd125b","#000000","#c61051","#000FFF")
elec %>% group_by(Tipo.de.Fuente,day) %>% summarise(n=mean(Produccion..MWh.)) %>%
hchart("column",x=day,y=n,group=Tipo.de.Fuente) %>% hc_add_theme(hc_theme_google()) %>% hc_colors(colors)

day_wise_fuel.png

Hour Wise

colors %>% group_by(Tipo.de.Fuente,Hora) %>% summarise(n=mean(Produccion..MWh.)) %>%
hchart("column",x=Hora,y=n,group=Tipo.de.Fuente) %>% hc_add_theme(hc_theme_google()) %>% hc_colors(colors)

hour_wise_fuel.png

Energy Consumption went up(on average) at around afternoon time. The times of the lowest consumptions were the early morniing hours.

Percentage of each Type of Production

temp <- elec %>% group_by(Tipo.de.Fuente) %>% summarise(n=mean(Produccion..MWh.,na.rm=T)) %>% mutate(n=100*(n/sum(n)))
plot_ly() %>%
add_pie(data=temp,
labels=temp$Tipo.de.Fuente,
values =temp$n,
name = "Source",
domain = list(x = c(-3, 1), y = c(-5, 1))) %>%
layout(title = "Source Wise Energy Production",
xaxis=ax,
yaxis=ax)

source_wise_pie.png

We see that a large percentage of Costa Rica’s energy needs were met via renewable means. Costa Rica is currently home to six active volcanoes and various inactive volcanoes. Geothermal production accounted for around 18.6% of the total production in October 2016.

Plants

Plant Wise Pie Chart

We now look at the power plants that were involved in supplying the country during the month of October. We will visualize each company’s share in terms of total energy supplied.

temp <- elec %>% group_by(Planta..CENCE.) %>% summarise(n=sum(Produccion..MWh.)) %>% arrange(desc(n)) %>% filter(n>0) %>% mutate(n=(n/sum(n))*100)
plot_ly() %>%
add_pie(data=temp,
labels=temp$Planta..CENCE.,
values =temp$n,
name = "Company wise map",
domain = list(x = c(5, 1), y = c(-5, 1))) %>%
layout(title = "Company Wise Energy Production",
xaxis=ax,
yaxis=ax)

company_wise_production.png

The largest production was from Reventazon followed by Angostura.
What do the top five plants by production use to produce electricity?

Top Five

DT::datatable(elec %>% group_by(Planta..CENCE.,Tipo.de.Fuente) %>% summarise(n=sum(Produccion..MWh.)) %>% filter(Planta..CENCE.=="Reventazon"|Planta..CENCE.=="Angostura"|Planta..CENCE.=="Cachi"|Planta..CENCE.=="Ventanas-Garita"|Planta..CENCE.=="Dengo"))

table.png
The table shows that 3 out of the five largest producers of electricity use water reservoirs.Costa Rica is home to the Arenal Lake , situated in the Northern Highlands of Costa Rica. This lake grew in size through the installation of the Arenal Dam in 1979 which now generates about 17% of Costa Rica’s electric energy.

Conclusion

Through visualization libraries, we were able to dig deeper into the data set to be able to provide some insights. Costa Rica is one  of the only countries in the world to heavily rely on renewable sources of energy. According to a report by The Guardian, Costa Rica relied 100% on renewable sources in 2015.The article can be found here. This is certainly a major step towards tackling the issues of climate change. With a strict carbon policy coupled with availability of natural resources, Costa Rica aims to be carbon neutral by the year 2021.

Thank You for reading and please do comment below if you have any feed back.You can find a more interactive presentation here

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s