Visualizing Per Capita Fuel Consumption using geom_map

Libraries and Tools

In this post, we will be looking a means to visualize US state data here. For this project we will be using the ggplot2library for visualization and the tidyr and dplyr libraries for data modification.

library(dplyr)
## Warning: package 'dplyr' was built under R version 3.3.2
library(plotly)
## Warning: package 'plotly' was built under R version 3.3.2
## Warning: package 'ggplot2' was built under R version 3.3.2
library(tidyr)
## Warning: package 'tidyr' was built under R version 3.3.2
library(gridExtra)
## Warning: package 'gridExtra' was built under R version 3.3.2
library(ggthemes)
## Warning: package 'ggthemes' was built under R version 3.3.2

Data Set import and modification

We import the data set and modify certain aspects for our analysis. The data set consists of information on fuel usage/consumption, population estimates, migration statistics etc for all US states from the year 2010-2014. In this project, we will be using fuel consumption and population estimate variables.

energy= read.csv("C:/Users/User/Documents/us_energy/Energy Census and Economic Data US 2010-2014.csv",header=T,stringsAsFactors = F)
str(energy)
## 'data.frame': 52 obs. of 191 variables:
## $ StateCodes : chr "AK" "AL" "AR" "AZ" ...
## $ Region : int 4 3 4 3 4 4 1 3 3 3 ...
## $ Division : int 9 6 8 7 9 8 1 5 5 5 ...
## $ Coast : int 1 1 0 0 1 0 1 0 1 1 ...
## $ Great.Lakes : int 0 0 0 0 0 0 0 0 0 0 ...
## $ TotalC2010 : int 653221 1931522 1120632 1383531 7760629 1513547 764970 190529 250212 4282673 ...
## $ TotalC2011 : int 653637 1905207 1122544 1424944 7777115 1470445 739130 183806 272568 4141711 ...
## $ TotalC2012 : int 649341 1879716 1067642 1395839 7564063 1440781 725019 172963 273728 4029903 ...
## $ TotalC2013 : int 621107 1919365 1096438 1414383 7665241 1470844 754901 175560 273716 4076406 ...
## $ TotalC2014 : int 603119 1958221 1114409 1422590 7620082 1477177 750019 178929 274013 4121680 ...
## $ TotalP2010 : int 1738207 1419613 1247709 580948 2532205 2561459 203188 95 3575 510201 ...
## $ TotalP2011 : int 1641980 1400108 1391190 617956 2634789 2750097 195792 271 3976 500907 ...
## $ TotalP2012 : int 1563102 1433370 1472778 598039 2334863 2921385 205073 277 3530 442188 ...
## $ TotalP2013 : int 1513859 1463647 1432074 594994 2390424 2838193 207118 323 3818 542570 ...
## $ TotalP2014 : int 1475129 1353725 1454325 635050 2413494 3041634 197271 360 4189 553738 ...
## $ TotalE2010 : num 6474 21631 12269 19373 117090 ...
## $ TotalE2011 : num 8050 24448 14179 22629 135098 ...
## $ TotalE2012 : num 7884 24193 13756 22872 135932 ...
## $ TotalE2013 : num 7282 24127 14102 22841 137051 ...
## $ TotalE2014 : num 6891 24147 13885 22610 137720 ...
## $ TotalPrice2010 : num 20.1 17.8 16.9 22.2 21 ...
## $ TotalPrice2011 : num 24.9 20.1 19.7 25.6 24.4 ...
## $ TotalPrice2012 : num 25.1 20 20 26.4 25 ...
## $ TotalPrice2013 : num 24.6 19 19.5 26.1 24.9 ...
## $ TotalPrice2014 : num 24.4 18.6 18.9 25.9 25.3 ...
## $ TotalC10.11 : num 100.1 98.6 100.2 103 100.2 ...
## $ TotalC11.12 : num 99.3 98.7 95.1 98 97.3 ...
## $ TotalC12.13 : num 95.7 102.1 102.7 101.3 101.3 ...
## $ TotalC13.14 : num 97.1 102 101.6 100.6 99.4 ...
## $ TotalP10.11 : num 94.5 98.6 111.5 106.4 104.1 ...
## $ TotalP11.12 : num 95.2 102.4 105.9 96.8 88.6 ...
## $ TotalP12.13 : num 96.8 102.1 97.2 99.5 102.4 ...
## $ TotalP13.14 : num 97.4 92.5 101.6 106.7 101 ...
## $ TotalE10.11 : num 124 113 116 117 115 ...
## $ TotalE11.12 : num 97.9 99 97 101.1 100.6 ...
## $ TotalE12.13 : num 92.4 99.7 102.5 99.9 100.8 ...
## $ TotalE13.14 : num 94.6 100.1 98.5 99 100.5 ...
## $ TotalPrice10.11 : num 124 113 116 115 116 ...
## $ TotalPrice11.12 : num 100.7 99.3 101.6 103.1 102.5 ...
## $ TotalPrice12.13 : num 98 95.3 97.8 98.6 99.5 ...
## $ TotalPrice13.14 : num 99.3 98 96.6 99.6 101.9 ...
## $ BiomassC2010 : int 4178 169088 94865 29289 280124 28210 35716 1027 6901 246789 ...
## $ BiomassC2011 : int 4247 179611 97160 28565 282581 30883 33958 1028 6781 245557 ...
## $ BiomassC2012 : int 4032 181878 96248 27267 280963 30680 32800 819 5972 244646 ...
## $ BiomassC2013 : int 5168 194432 95356 26492 294412 35382 34153 855 5977 253961 ...
## $ BiomassC2014 : int 5476 186649 95963 31481 298473 36073 36011 920 6249 250043 ...
## $ CoalC2010 : int 14548 718684 293689 457909 54972 382623 28711 71 30253 637416 ...
## $ CoalC2011 : int 15481 651032 306119 459909 55264 368871 6081 48 17881 552730 ...
## $ CoalC2012 : int 15521 547004 296732 420570 43832 370085 9290 77 17384 482984 ...
## $ CoalC2013 : int 14819 565051 327099 454865 38151 363532 7679 4 18254 505155 ...
## $ CoalC2014 : int 18225 575912 339214 447849 39486 350526 9097 48 10238 557882 ...
## $ CoalP2010 : int 33556 493094 718 167930 0 551825 0 0 0 0 ...
## $ CoalP2011 : int 33524 468671 2985 174841 0 586847 0 0 0 0 ...
## $ CoalP2012 : int 31332 488084 2077 161374 0 629645 0 0 0 0 ...
## $ CoalP2013 : int 24917 469162 1433 163691 0 529096 0 0 0 0 ...
## $ CoalP2014 : int 22944 414366 1864 173337 0 528242 0 0 0 0 ...
## $ CoalE2010 : num 49.9 2135.6 510.6 829.1 161.4 ...
## $ CoalE2011 : num 59.6 2009.7 592.2 917.2 173.1 ...
## $ CoalE2012 : num 63 1809 673 880 134 ...
## $ CoalE2013 : num 72.6 1731.6 789.4 946.8 129.5 ...
## $ CoalE2014 : num 88.8 1677.3 820.9 944.6 135.3 ...
## $ CoalPrice2010 : num 3.43 2.97 1.74 1.81 2.94 1.59 3.45 2.63 3.35 3.48 ...
## $ CoalPrice2011 : num 3.85 3.09 1.93 1.99 3.13 1.73 3.68 3.26 3.41 3.55 ...
## $ CoalPrice2012 : num 4.06 3.31 2.27 2.09 3.05 1.86 3.59 3.08 3.35 3.51 ...
## $ CoalPrice2013 : num 4.9 3.06 2.41 2.08 3.39 1.93 4.21 3.15 3.2 3.44 ...
## $ CoalPrice2014 : num 4.87 2.91 2.42 2.11 3.43 1.95 4.27 3 3.08 3.33 ...
## $ ElecC2010 : int 21315 310023 164439 248506 882107 180555 103697 40524 39599 788887 ...
## $ ElecC2011 : int 21562 303652 163530 255708 893745 182400 101879 39450 39181 768009 ...
## $ ElecC2012 : int 21893 294055 159885 256116 885544 183174 100628 38415 39304 752941 ...
## $ ElecC2013 : int 21387 299751 159283 258159 891666 182343 101761 37824 38719 757189 ...
## $ ElecC2014 : int 21034 308765 160638 260328 895939 182189 100157 38193 38687 771379 ...
## $ ElecE2010 : num 912 7833 3393 7059 33382 ...
## $ ElecE2011 : num 1005 7846 3447 7279 33919 ...
## $ ElecE2012 : num 1035 7666 3456 7361 34853 ...
## $ ElecE2013 : num 1005 7901 3687 7669 37028 ...
## $ ElecE2014 : num 1056 8363 3704 7764 39424 ...
## $ ElecPrice2010 : num 43.3 26.4 21.6 28.4 38.2 ...
## $ ElecPrice2011 : num 47.1 27.1 22 28.5 38.4 ...
## $ ElecPrice2012 : num 47.8 27.3 22.6 28.7 39.8 ...
## $ ElecPrice2013 : num 48.4 26.5 23.3 29.7 42 ...
## $ ElecPrice2014 : num 51.3 27.2 23.2 29.8 44.5 ...
## $ FossFuelC2010 : int 634777 1785688 909827 1292346 5741492 1370758 562060 58155 171414 3491560 ...
## $ FossFuelC2011 : int 635981 1783839 932878 1253992 5550201 1304903 557282 55887 201518 3357837 ...
## $ FossFuelC2012 : int 629768 1740315 918078 1248032 5687343 1282352 544358 49025 221203 3327632 ...
## $ FossFuelC2013 : int 600660 1689491 936796 1289119 5755210 1303825 560141 53357 213411 3273949 ...
## $ FossFuelC2014 : int 581369 1724891 939788 1256577 5702418 1313834 559695 55217 213655 3350291 ...
## $ GeoC2010 : int 153 138 773 339 124981 721 20 6 414 9516 ...
## $ GeoC2011 : int 214 135 750 345 124092 741 19 59 401 9799 ...
## $ GeoC2012 : int 186 141 808 345 121269 759 21 22 430 10056 ...
## $ GeoC2013 : int 186 141 808 345 119556 759 21 22 430 10056 ...
## $ GeoC2014 : int 186 141 808 345 117226 759 21 22 430 10056 ...
## $ GeoP2010 : int 0 0 0 0 12600 0 0 0 0 0 ...
## $ GeoP2011 : int 0 0 0 0 12552 0 0 0 0 0 ...
## $ GeoP2012 : int 0 0 0 0 12519 0 0 0 0 0 ...
## $ GeoP2013 : int 0 0 0 0 12307 0 0 0 0 0 ...
## $ GeoP2014 : int 0 0 0 0 12102 0 0 0 0 0 ...
## $ HydroC2010 : int 13982 84919 35697 64606 326152 15398 3811 0 0 1731 ...
## $ HydroC2011 : int 13066 86313 28738 89135 413488 20235 5513 0 0 1769 ...
## $ HydroC2012 : int 14988 70754 20921 63918 255384 14247 2970 0 0 1432 ...
## [list output truncated]
energy <- filter(energy,StateCodes!="US")
energy$avg_pop <- (energy$POPESTIMATE2010+energy$POPESTIMATE2011+energy$POPESTIMATE2012+energy$POPESTIMATE2013+energy$POPESTIMATE2014)/5

Changing the State COdes

We replace the StateCOdes variable so that it can be read by the ggmap() function for plotting a map.

energy$StateCodes <- ifelse(energy$StateCodes=='AR',"arkansas",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='AL',"alabama",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='AK',"alaska",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='AZ',"arizona",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='CA',"california",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='CO',"colorado",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='CT',"connecticut",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='DC',"washington d.c",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='DE',"delaware",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='FL',"florida",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='GA',"georgia",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='HI',"hawaii",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='IA',"iowa",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='ID',"idaho",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='IL',"illinois",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='IN',"indiana",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='KS',"kansas",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='KY',"kentucky",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='LA',"louisiana",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='MA',"massachusetts",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='MD',"maryland",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='ME',"maine",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='MI',"michigan",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='MN',"minnesota",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='MO',"missouri",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='MS',"mississippi",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='MT',"montana",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='NC',"north carolina",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='ND',"north dakota",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='NE',"nebraska",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='NH',"new hampshire",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='NJ',"new jersey",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='NM',"new mexico",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='NV',"nevada",energy$StateCodes)

energy$StateCodes <- ifelse(energy$StateCodes=='NY',"new york",energy$StateCodes)
energy$StateCodes <- ifelse(energy$StateCodes=='OH',"ohio",energy$StateCodes)
energy$StateCodes <- ifelse(energy$StateCodes=='OK',"oklahoma",energy$StateCodes)
energy$StateCodes <- ifelse(energy$StateCodes=='OR',"oregon",energy$StateCodes)
energy$StateCodes <- ifelse(energy$StateCodes=='PA',"pennsylvania",energy$StateCodes)
energy$StateCodes <- ifelse(energy$StateCodes=='PR',"puerto rico",energy$StateCodes)
energy$StateCodes <- ifelse(energy$StateCodes=='RI',"rhode island",energy$StateCodes)
energy$StateCodes <- ifelse(energy$StateCodes=='SC',"south carolina",energy$StateCodes)
energy$StateCodes <- ifelse(energy$StateCodes=='SD',"south dakota",energy$StateCodes)
energy$StateCodes <- ifelse(energy$StateCodes=='TN',"tennessee",energy$StateCodes)
energy$StateCodes <- ifelse(energy$StateCodes=='TX',"texas",energy$StateCodes)
energy$StateCodes <- ifelse(energy$StateCodes=='UT',"utah",energy$StateCodes)
energy$StateCodes <- ifelse(energy$StateCodes=='VA',"virginia",energy$StateCodes)
energy$StateCodes <- ifelse(energy$StateCodes=='VT',"vermont",energy$StateCodes)
energy$StateCodes <- ifelse(energy$StateCodes=='WA',"washington",energy$StateCodes)
energy$StateCodes <- ifelse(energy$StateCodes=='WI',"wisconsin",energy$StateCodes)
energy$StateCodes <- ifelse(energy$StateCodes=='WV',"west virginia",energy$StateCodes)
energy$StateCodes <- ifelse(energy$StateCodes=='WY',"wyoming",energy$StateCodes)

Extracting and merging the relevant data frames

We use th gather() function to tidy up our datarame, which would then be fed into the ggplot() function

biomass <- energy[,c(1,42:46,192)]
biomass <- biomass %>% gather(Year,biomass_c,2:6) %>% mutate(biomass_c=biomass_c/avg_pop)

biomass$Year <- as.numeric(gsub("BioMassC","",biomass$Year))
## Warning: NAs introduced by coercion
coal <- energy[,c(1,47:51,192)]
coal <- coal %>% gather(Year,coal_c,2:6) %>% mutate(coal_c=coal_c/avg_pop)
coal$Year <- as.numeric(gsub("CoalC","",coal$Year))

fossil <- energy[,c(1,82:86,192)]
fossil <- fossil %>% gather(Year,fossil_c,2:6) %>% mutate(fossil_c=fossil_c/avg_pop)
fossil$Year <- as.numeric(gsub("FossFuelC","",fossil$Year))

geo <- energy[,c(1,87:91,192)]
geo <- geo %>% gather(Year,geo_c,2:6) %>% mutate(geo_c=geo_c/avg_pop)
geo$Year <- as.numeric(gsub("GeoC","",geo$Year))

hydro <- energy[,c(1,97:101,192)]
hydro <- hydro %>% gather(Year,hydro_c,2:6) %>% mutate(hydro_c=hydro_c/avg_pop)
hydro$Year <- as.numeric(gsub("HydroC","",hydro$Year))

natgas <- energy[,c(1,107:111,192)]
natgas <- natgas %>% gather(Year,natgas_c,2:6) %>% mutate(natgas_c=natgas_c/avg_pop)
natgas$Year <- as.numeric(gsub("NatGasC","",natgas$Year))
states_map <- map_data("state")
## Warning: package 'maps' was built under R version 3.3.2

Mapping the average values over the Years

plot1 <-
biomass %>% group_by(StateCodes) %>% summarise(biomass=mean(biomass_c)) %>%
ggplot(aes(map_id = StateCodes)) +
geom_map(aes(fill = (biomass)), map = states_map, color ="black") +
expand_limits(x = states_map$long, y = states_map$lat) +
theme_few()+
theme(legend.position = "bottom",
axis.ticks = element_blank(),
axis.title = element_blank(),
axis.text = element_blank()) +
scale_fill_gradient(low="green", high="red") +
guides(fill = guide_colorbar(barwidth = 10, barheight = .5)) +
ggtitle(" Per Capita Biomass Usage")

plot2 <-
coal %>% group_by(StateCodes) %>% summarise(coal=mean(coal_c)) %>%
ggplot(aes(map_id = StateCodes)) +
geom_map(aes(fill = (coal)), map = states_map, color ="black") +
expand_limits(x = states_map$long, y = states_map$lat) +
theme_few()+
theme(legend.position = "bottom",
axis.ticks = element_blank(),
axis.title = element_blank(),
axis.text = element_blank()) +
scale_fill_gradient(low="green", high="red") +
guides(fill = guide_colorbar(barwidth = 10, barheight = .5)) +
ggtitle(" Per Capita Coal Usage")
plot3 <-
fossil %>% group_by(StateCodes) %>% summarise(fossil=mean(fossil_c)) %>%
ggplot(aes(map_id = StateCodes)) +
geom_map(aes(fill = (fossil)), map = states_map, color ="black") +
expand_limits(x = states_map$long, y = states_map$lat) +
theme_few()+
theme(legend.position = "bottom",
axis.ticks = element_blank(),
axis.title = element_blank(),
axis.text = element_blank()) +
scale_fill_gradient(low="green", high="red") +
guides(fill = guide_colorbar(barwidth = 10, barheight = .5)) +
ggtitle(" Per Capita Fossil Fuel Usage")

plot4 <-
geo %>% group_by(StateCodes) %>% summarise(geo=mean(geo_c)) %>%
ggplot(aes(map_id = StateCodes)) +
geom_map(aes(fill = (geo)), map = states_map, color ="black") +
expand_limits(x = states_map$long, y = states_map$lat) +
theme_few()+
theme(legend.position = "bottom",
axis.ticks = element_blank(),
axis.title = element_blank(),
axis.text = element_blank()) +
scale_fill_gradient(low="green", high="red") +
guides(fill = guide_colorbar(barwidth = 10, barheight = .5)) +
ggtitle(" Per Capita Geo Thermal Energy Usage")

plot5 <-
hydro %>% group_by(StateCodes) %>% summarise(hydro=mean(hydro_c)) %>%
ggplot(aes(map_id = StateCodes)) +
geom_map(aes(fill = (hydro)), map = states_map, color ="black") +
expand_limits(x = states_map$long, y = states_map$lat) +
theme_few()+
theme(legend.position = "bottom",
axis.ticks = element_blank(),
axis.title = element_blank(),
axis.text = element_blank()) +
scale_fill_gradient(low="green", high="red") +
guides(fill = guide_colorbar(barwidth = 10, barheight = .5)) +
ggtitle("Per Capita Hydro electric Usage")

plot6 <-
natgas %>% group_by(StateCodes) %>% summarise(natgas=mean(natgas_c)) %>%
ggplot(aes(map_id = StateCodes)) +
geom_map(aes(fill = (natgas)), map = states_map, color ="black") +
expand_limits(x = states_map$long, y = states_map$lat) +
theme_few()+
theme(legend.position = "bottom",
axis.ticks = element_blank(),
axis.title = element_blank(),
axis.text = element_blank()) +
scale_fill_gradient(low="green", high="red") +
guides(fill = guide_colorbar(barwidth = 10, barheight = .5)) +
ggtitle("Per Capita Natural Gas Usage")
grid.arrange(plot1,plot2,plot3,plot4,plot5,plot6,nrow=3)


As per the plots above, Washington State has the largest usage of hydro electric energy followed by Montana and Oregon. Natural Gas usage is dominated by Louisiana and Wyoming. This is true due to the immense amount of oil and gas fields available in these states. Geo Thermal Energy usage is dominated by Nevada due to the large presence of underground reservoirs.Vermont has close to nil coal usage. It depends highly on renewable sources of energy.Wyoming dominates coal usage followed by North Dakota.

Conclusion

We were able to visualize energy consumption data much more effectively using geographic plots as compared to using traditional line plots. We were able to discern per capita energy consumption by fuel.

Sources

Geothermal Energy in Nevada

Coal in North Dakota

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 )

Connecting to %s