Re-creating a Voronoi-Style Map with R

I’ve written some “tutorial”-like content recently—seehere,here, andhere—butI’ve been lacking on ideas for “original” content since then. With that said,I thought it would to try to re-create something with R. (Not too long agoI saw thatAndrew Heiss did something akin to this withCharles Minard’s well-known visualization of Napoleon’s 1812.)

The focus of my re-creation here is the price contour map shown on thefront page of the website for the Electric Reliability Council of Texas,the independent system operator of electric power flowfor about 90 percent of Texas residents, as well as the employer of yours truly).

Without going too much into the details of what kind of informationand insight the map is supposed to provide , it’s sufficient for thereader to simply understand the following:

  • “cooler” colors (i.e. blue and green)generally indicate regions of electric energy stability—where electric powergeneration is sufficiently meeting electric demand at a given pointin time;

  • and “warmer” colors (i.e. orange and red) generally represent theopposite—regions where there is some “stress” at a given point in timedue to either the existence or threat of power imbalance.

Anyways, aside from my familiarity with the map (just from visitingthe website so often), I thought that it wouldbe interesting to try to re-create it because it presents some “challenges”that I had not really tackled before (even in my previous endeavorswith {ggplot2} and maps)—namely,(1) manipulating KML dataand (2) creating Voronoishapes.

Unfortunately, the code needed to re-produce the map is not exactly trivial,so I’m going to leave details-hungry readersa link to the repositoryto explore the techniques that I used on their own time. To summarize the process,here’s what I did:

I downloaded the price data CSVs for a specific settlementinterval—7:15 AM interval on November 15, 2018—from public linkson ERCOT’s website.

Additionally, I downloaded the “static” KML data upon which the map is built.

Finally, I downloaded “dynamic” KML data representing the price pointsthemselves (for both the Real-Time Market (RTM) and the Day-Ahead Market (DAM).

I should Note that with this step and the others that follow,I repeated the actions for both data sets—those representing theRTM and DAM prices and points.

With the KML files for the price points, I extracted thelongitude and latitude value pairs from the points defined in the KMLusing the {tidykml} package.Additionally, I converted the legend (embedded in the KML) into a vectorto be used for the final recreated plot(s) themselves.

With the CSVs with the price data, I did a bit of “data munging” to convertthe data for the two markets into an equivalent, easy-to-work-with format, andI joined this with the coordinate data from the previous step.

With the KML files representing the map’s static data, I createda SpatialPolygonsDataFrame encompassing both the Texas state and county boundaries,as well as the ERCOT region boundaries (which is separate). This was probably the most difficult part for me to figure out. Nonetheless,with the help of the R community’s packages geared towards working with spatialdata—most notably, the {sp}and {sf} packages—as well as some greatdocumentation and tutorials—I foundthis oneparticularly helpful, although one may consider its techniques “outdated” since it doesn’tutilize {sf} for interpolation.

Next was the “heart” of the process—the interpolation to create the Voronoishapes. This may be the part that is most interesting to the reader,since a similar process could be applied to one’s own project.

Finally, I used {ggplot2} to create the visualizations themselves.Notably, one must be careful with the order of calls togeom_polygon(), geom_sf(), and coord_sf() in order to getthe ordering of layers correct.

As for other details about the implementation, this process provided a perfectexample of how/why functions can be so useful—it was really nice to just calla custom function twice to repeat each step forthe two data sets, which certainly saved me some time and effort.

So what does the final product look like? Below, I’ve placed my re-creationsand the actual image side by side (with the help of the{magick} package).

Not bad, right? OK, so the sizing isn’t perfect, but I would say it’s close enough.

To take things one step further, I tried “subtracting” the values between the twore-created maps. There is some value to doing this—to visualize the discrepanciesbetween prices in the two markets (which, hypothetically, should be equal).

Because this map displays information that is inherently different than thoseof the stand-alone RTM and DAM maps, I used a different colorpalette—specifically, the “E” palette that comes with theawesome {viridis} package,which is conveniently made available in{ggplot2} ever since the v3 release.

I really enjoyed this challenge to myself. I can certainly see myself doing somethingsimilar in the future, perhaps with a focus with which I’m less familiar (to givemyself an even greater challenge).


Related