Visualizing electricity in Costa Rica using highcharter and plotly

Data Acquisition and Introduction

elec <- read.csv("SEN oct-2016.csv",header = T,stringsAsFactors = F)
## '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 ...
## $ : chr "Privada" "Privada" "Privada" "Privada" ...
## $ Fuente : chr "Eolica" "Eolica" "Eolica" "Eolica" ...
## $ : chr "Eolica" "Eolica" "Eolica" "Eolica" ...
## $ Produccion..MWh.: num 0.9639 0.0452 0.4453 1.0051 1.6287 ...
<br />elec <- filter(elec,!="#N/A")
elec <- filter(elec,!="#N/A")
#elec$Dia <- paste(elec$Dia,elec$Hora,":00")
elec$Hora <- gsub(" ","",paste(elec$Hora,":00"))
add_zero <- function(text){
return(gsub(" ","",paste("0",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(,Planta..CENCE.)%>% distinct() %>% group_by( %>% summarise(n=n()) %>%
hchart("column",,y=n) %>%
hc_title(text="Number of Private and Public Companies")


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

Average Production

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


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(,day) %>% summarise(n=mean(Produccion..MWh.)) %>% hchart("column",x=day,y=n,>% hc_add_theme(hc_theme_db())


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, %>% summarise(n=mean(Produccion..MWh.)) %>% ggplot(aes(x=Dia, y=n, +



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(
privada <- elec %>% group_by(, %>% summarise(n=sum(Produccion..MWh.)) %>% filter("Privada",n>0) %>% mutate(Percentage=100*(n/sum(n)))
publica <- elec %>% group_by(, %>% summarise(n=sum(Produccion..MWh.)) %>% filter("Publica",n>0) %>% mutate(Percentage=100*(n/sum(n)))
plot_ly() %>%
add_pie(data = privada,
values = privada$Percentage,
name = "Private",
domain = list(x = c(0.52, 1), y = c(0.5, 1)))%>%
add_pie(data = publica,
values = publica$Percentage,
name = "Public",
domain = list(x = c(0, 0.48), y = c(0.5, 1))) %>%
layout(title = "Public and Private",


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(,Dia) %>% summarise(n=sum(Produccion..MWh.)) %>% filter(!="#N/A")%>%
ggplot(aes(x=Dia, y=n, +



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(,day) %>% summarise(n=mean(Produccion..MWh.)) %>%
hchart("column",x=day,y=n, %>% hc_add_theme(hc_theme_google()) %>% hc_colors(colors)


Hour Wise

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


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( %>% summarise(n=mean(Produccion..MWh.,na.rm=T)) %>% mutate(n=100*(n/sum(n)))
plot_ly() %>%
values =temp$n,
name = "Source",
domain = list(x = c(-3, 1), y = c(-5, 1))) %>%
layout(title = "Source Wise Energy Production",


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.


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() %>%
values =temp$n,
name = "Company wise map",
domain = list(x = c(5, 1), y = c(-5, 1))) %>%
layout(title = "Company Wise Energy Production",


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., %>% summarise(n=sum(Produccion..MWh.)) %>% filter(Planta..CENCE.=="Reventazon"|Planta..CENCE.=="Angostura"|Planta..CENCE.=="Cachi"|Planta..CENCE.=="Ventanas-Garita"|Planta..CENCE.=="Dengo"))

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.


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



Leave a Reply

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

You are commenting using your 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 )

Connecting to %s