Linking to GEOS 3.10.2, GDAL 3.4.2, PROJ 8.2.1; sf_use_s2() is TRUE
library(rgeoboundaries)
Registered S3 method overwritten by 'hoardr':
method from
print.cache_info httr
library(countrycode)library(lwgeom)
Linking to liblwgeom 3.0.0beta1 r16016, GEOS 3.10.2, PROJ 8.2.1
library(jsonlite)
Attaching package: 'jsonlite'
The following object is masked from 'package:purrr':
flatten
library(here)
here() starts at /Users/jgol0005/Code/reports/report-energy-transition
Before we get started with the electricity data, let’s set up the spatial data we’re going to use to locate and name countries. First the names, using {countrycode}:
Warning in st_centroid.sf(., of_largest_polygon = TRUE): st_centroid assumes
attributes are constant over geometries of x
Warning in st_centroid.sfc(st_geometry(x), of_largest_polygon =
of_largest_polygon): st_centroid does not give correct centroids for longitude/
latitude data
The original version of this analysis used data from IRENA’s Excel downloading tool, but it seems to be playing up. I’ve downloaded the data from IRENASTAT instead, but it’s slightly differently shaped (partly because installed capacity and generation are separate tables; partly because IRENASTAT has a 100k row export limit).
The tables used (which can be updated by downloading them from IRENASTAT and placing them in data as irena-src-[measure][grid_status].csv) are:
Installed electricity capacity by country/area (MW) by Country/area, Technology, Grid connection and Year
Electricity generation (GWh) by Country/area, Technology, Grid connection and Year
The IRENASTAT data, in this form, doesn’t classify technologies as renewable or non-renewable, so we’ll have to do that ourselves. We’ll also have to aggregate on- and off-grid values.
I’ve used irena_bound %>% pull(tech) %>% unique() to check which technologies are in the data, and then compared those technologies against both OLD-irena-stats.csv (which did categorise them) and the dashboards on IRENA’s website to group most of the technologies.
Most notably, nuclear does not appear to be considered renewable by IRENA.
`summarise()` has grouped output by 'country', 'measure', 'renewable_status'.
You can override using the `.groups` argument.
Finally, we also need ISO codes for the new data. I’m going to cheat here and steal them from OLD-irena-stats.csv for consistency’s sake.
here("data", "OLD-irena-stats.csv") %>%read_csv(col_types ="-cc-------") %>%set_names(c("country", "iso")) %>%distinct() %>%arrange(country) ->iso_map# a few country names have text encoding errors or have otherwise changed since# the last dataset. we'll patch their codes here# ({countrycodes} will standardise the names)irena_wide %>%left_join(iso_map, by ="country") %>%mutate(iso =recode(country,"R�union"="REU","T�rkiye"="TUR","Saint Barth�lemy"="BLM","C�te d'Ivoire"="CIV","Cura�ao"="CUW","Kosovo"="XKX","China, Hong Kong Special Administrative Region"="HKG",.default = iso)) %>%select(iso, everything()) ->irena_joined
# widen by renewable status to calculate proportion of renewablesirena_joined %>%mutate(total_gen_gwh = totalgen_gwh_renewable + totalgen_gwh_nonrenewable,total_cap_mw = totalcap_mw_renewable + totalcap_mw_nonrenewable,renewprop_gen_gwh = totalgen_gwh_renewable / total_gen_gwh,renewprop_cap_mw = totalcap_mw_renewable / total_cap_mw) %>%select(iso, country, year, starts_with("total"), starts_with("renewprop")) ->proportions
Finally, we’ll join the centroids and export to geoJSON.
# first export a "tall" csv for deckgl...proportions %>%left_join(country_points, by ="iso") %>%select(-starts_with("country")) %>%mutate(country =standardise_countries(iso),point =st_coordinates(.$geometry),lon = point[, "X"],lat = point[, "Y"]) %>%select(iso, country, lon, lat, year, ends_with("gwh"), ends_with("mw")) %>%write_csv(here("data", "irena-totals.csv"))# ... then a wide geojson for other map usersproportions %>%pivot_wider(names_from = year,values_from =c(total_gen_gwh, total_cap_mw, renewprop_gen_gwh, renewprop_cap_mw),names_sep =".") %>%left_join(country_points, by ="iso") %>%select(-starts_with("country")) %>%mutate(country =standardise_countries(iso)) %>%select(iso, country, everything()) %>%st_write(here("data", "irena-totals.geojson"), delete_dsn =TRUE) ->totals_wide
Deleting source `/Users/jgol0005/Code/reports/report-energy-transition/data/irena-totals.geojson' using driver `GeoJSON'
Writing layer `irena-totals' to data source
`/Users/jgol0005/Code/reports/report-energy-transition/data/irena-totals.geojson' using driver `GeoJSON'
Writing 4736 features with 94 fields and geometry type Point.