United States economy with R visualizations

Ana Preciado
3 min readJun 29, 2018

--

Bar plots

First we will look at how private vs public spending (Asa a percent of GDP) has changed in the United State over the years. As the graph illustrates, private spending has slightly grown from 85.7% in 2010 to 87.5% in 2017. While the 1.7 percent points difference does not seem like a lot, 1.7% of the 2017 GDP would be the equivalent of $329,640.

Next, we will look at the sector breakdown of the ‘private industries’ as a percent of GDP.

The industries in the United States that conform more than 10% of the GDP in 2017 are: Finance, insurance, real estate, retail and leasing(20%); professional and business services (12%); Manufacturing (11%).

  • In the first category, we can see that ‘finance and insurance’ represents 7.5%, and ‘real estate and rental leasing’ represents 13.4%.
  • In the second category, (professional and business services), we have that ‘Professional, scientific, and technical services’ constitute 7.1%. It includes legal services, computer system related services, and miscellaneous constitute 7.1%. Management of enterprises constitutes only 1.9%, and administrative+waste management services 3.1%.
  • In the manufacturing category, we can see that chemical products represents 2%, ‘food, beverages & tobacco products’ (1.5%), and ‘computer and electronics’ (1.5%).

Map Visualizations

Using data from the BEA, the following graphs represent the 2017 GDP per state, and per capita per state (using 2017 population estimates from the United States Census Bureau).

GDP per state
GDP per capita per state (2017)

The states with the highest GDP per capita are District of Columbia (0.188), New York (0.077), and Massachusetts (0.077). The states with the lowest GDP per capita are Mississippi (0.037), Arkansas(0.0415), Idaho (0.0418).

R Code:

install.packages(‘readxl’)
library(readxl)
setwd(‘/Users/anapreciado/Downloads/’)
file <- read_excel(“GDPbyInd_VA_1947–2017.xlsx”, sheet=’VAPercentGDP’)
fileRel <- file[5:107,]
colnames(fileRel) <- fileRel[1,]
fileRel <- fileRel[-1,]
fileRel <- fileRel[,-1]
PrivGov <- fileRel[c(2,90),]
PrivGov <- as.data.frame(PrivGov)
rownames(PrivGov) <- PrivGov[,1]
PrivGov <- PrivGov[,c(65:72)]
PrivGov <- as.matrix(PrivGov)
par(mar=c(8.1, 4.1, 4.1, 5.1), xpd=TRUE)
barplot(PrivGov, main=’Private vs government spending as percent of GDP’,
col=c(“darkblue”,”red”))
legend(“bottomright”, legend=rownames(PrivGov), fill= c(“darkblue”, “red”), inset=c(0.5,-0.4),cex=.8, bty=’n’)

#Second Graph — Private Sector Breakdown
PrivSec <- fileRel[c(3,6,10,11,12,34,35,40,49,54,65,74,82,89),]
PrivSec <- as.data.frame(PrivSec)
rownames(PrivSec) <- PrivSec[,1]
PrivSec2017 <- PrivSec[,72]
PrivSec2017 <- as.data.frame(PrivSec2017)
rownames(PrivSec2017) <- PrivSec[,1]
PrivSec2017 <- as.matrix(PrivSec2017)
PrivSec2017 <- as.integer((PrivSec2017))
par(mar=c(12, 4.1, 4.1, 5.1), xpd=TRUE)
barplot(PrivSec2017, main=”Percentage of GDP”, names.arg=rownames(PrivSec), las=2,cex.names=0.4)
abline(h=10, col=’red’, lwd=1, lty=3)

#GDP per state
setwd(‘/Users/anapreciado/Downloads/’)
GDPState <- read.csv(‘download (1).csv’, header=F)
install.packages(‘ggplot2’)
library(ggplot2)
install.packages(‘maps’)
library(maps)
install.packages(‘ggmap’)
library(ggmap)
install.packages(‘devtools’)
install.packages(‘dplyr’)
install.packages(‘stringr’)
install.packages(‘mapdata’)
library(devtools)
library(dplyr)
library(stringr)
library(mapdata)
install.packages(‘devtools’)
install.packages(‘dplyr’)
install.packages(‘stringr’)
install.packages(‘mapdata’)
library(devtools)
library(dplyr)
library(stringr)

states <- map_data(“state”)
GDPState2 <- GDPState[7:57,c(2,3)]
colnames(GDPState2) <- c(‘region’, ‘GDP’)
GDPState2$region <- tolower(GDPState2$region)

#left merge
states2 <- merge(states,GDPState2,by=’region’, x.all=TRUE)

#plot
head(states2)
ggplot(data=states2)+geom_polygon(aes(x=long, y=lat, fill=GDP, group=group), color=’white’)

#GDP per capita per state
#lets do the GDP per capita
CensusData <- read_excel(“nst-est2017–01.xlsx”)
CensusData2 <- CensusData[c(3,9:59),c(1,11)]
colnames(CensusData2) <- c(“region”,”year2017")
CensusData2 <- CensusData2[-1,]
CensusData2$region <- substring(CensusData2$region,2)
CensusData2$region <- tolower(CensusData2$region)
states2 <- merge(states2,CensusData2,by=’region’, x.all=TRUE)
states2$GDPPerson <- as.numeric(states2$GDP)/as.numeric(states2$year2017)
ggplot(data=states2)+geom_polygon(aes(x=long, y=lat, fill=GDPPerson, group=group), color=’white’)

#In barplot
SortingThing <- aggregate(GDPPerson ~ region, data=states2, FUN=’mean’)
SortingThing <- SortingThing[order(SortingThing$GDPPerson),]
str(SortingThing)
plotting <- as.vector(SortingThing$GDPPerson)
barplot(plotting, names.arg=SortingThing$region, las=2)

--

--