Mapping Power Outages In Maine With R

UPDATE: A Shiny (dynamic) version of this is now available.

We had yet-another power outage this morning due to the weird weather patterns of the week and it was the final catalyst I needed to crank out some R code to map the affected counties.

Central Maine Power provides an outage portal where folks can both report outages and see areas impacted by outages. They use an SAP web service that generates the outage table and the aforelinked page just embeds that URL ( as an iframe. We can use the XML package in R to grab that HTML file, parse it, extract the table and then send the data to ggplot.

It should be a good starting point for anyone wishing to do something similar. The next itch to scratch for me on this is a live D3 map that uses the outage table with drill-down capabilities to the linked data.

cmp.url <- ""
# get outage table (first one on the cmp.url page)
cmp.node <- getNodeSet(htmlParse(cmp.url),"//table")[[1]] <- readHTMLTable(cmp.node,
                         trim=TRUE, stringsAsFactors=FALSE)
# clean up the table to it's easier to work with <-[-nrow(,] # get rid of last row$subregion <- tolower($subregion)$total.customers <- as.numeric(gsub(",","",$total.customers))$without.power <- as.numeric(gsub(",","",$without.power))
# get maine map with counties
county.df <- map_data('county')
me <- subset(county.df, region=="maine")
# get a copy with just the affected counties
out <- subset(me, subregion %in%$subregion)
# add outage into to it
out <- join(out,
# plot the map
gg <- ggplot(me, aes(long, lat, group=group))
gg <- gg + geom_polygon(fill=NA, colour='gray50', size=0.25)
gg <- gg + geom_polygon(data=out, aes(long, lat, group=group, fill=without.power), 
                        colour='gray50', size=0.25)
gg <- gg + scale_fill_gradient2(low="#FFFFCC", mid="#FD8D3C", high="#800026")
gg <- gg + coord_map()
gg <- gg + theme_bw()
gg <- gg + labs(x="", y="", title="CMP (Maine) Customers Without Power by County")
gg <- gg + theme(panel.border = element_blank(),
                 panel.background = element_blank(),
                 panel.grid = element_blank(),
                 axis.text = element_blank(),
                 axis.ticks = element_blank(),

Plot_Zoom(click for larger)

Comments on this post

No comments.

Leave a Reply

Trackbacks and Pinbacks on this post

  1. […] decided to forego the D3 map mentioned in the previous post in favor of a Shiny one since I had 90% of the mapping code […]

  2. […] started with a local R version and migrated to a Shiny version and is now in full D3 […]

TrackBack URL