Principles of Data Visualization and Introduction to ggplot2
I have provided you with data about the 5,000 fastest growing companies in the US, as compiled by Inc. magazine. Let’s read this in:
inc <- read.csv("https://raw.githubusercontent.com/charleyferrari/CUNY_DATA_608/master/module1/Data/inc5000_data.csv", header= TRUE)
And let’s preview this data:
head(inc)
## Rank Name Growth_Rate Revenue
## 1 1 Fuhu 421.48 1.179e+08
## 2 2 FederalConference.com 248.31 4.960e+07
## 3 3 The HCI Group 245.45 2.550e+07
## 4 4 Bridger 233.08 1.900e+09
## 5 5 DataXu 213.37 8.700e+07
## 6 6 MileStone Community Builders 179.38 4.570e+07
## Industry Employees City State
## 1 Consumer Products & Services 104 El Segundo CA
## 2 Government Services 51 Dumfries VA
## 3 Health 132 Jacksonville FL
## 4 Energy 50 Addison TX
## 5 Advertising & Marketing 220 Boston MA
## 6 Real Estate 63 Austin TX
summary(inc)
## Rank Name Growth_Rate
## Min. : 1 (Add)ventures : 1 Min. : 0.340
## 1st Qu.:1252 @Properties : 1 1st Qu.: 0.770
## Median :2502 1-Stop Translation USA: 1 Median : 1.420
## Mean :2502 110 Consulting : 1 Mean : 4.612
## 3rd Qu.:3751 11thStreetCoffee.com : 1 3rd Qu.: 3.290
## Max. :5000 123 Exteriors : 1 Max. :421.480
## (Other) :4995
## Revenue Industry Employees
## Min. :2.000e+06 IT Services : 733 Min. : 1.0
## 1st Qu.:5.100e+06 Business Products & Services: 482 1st Qu.: 25.0
## Median :1.090e+07 Advertising & Marketing : 471 Median : 53.0
## Mean :4.822e+07 Health : 355 Mean : 232.7
## 3rd Qu.:2.860e+07 Software : 342 3rd Qu.: 132.0
## Max. :1.010e+10 Financial Services : 260 Max. :66803.0
## (Other) :2358 NA's :12
## City State
## New York : 160 CA : 701
## Chicago : 90 TX : 387
## Austin : 88 NY : 311
## Houston : 76 VA : 283
## San Francisco: 75 FL : 282
## Atlanta : 74 IL : 273
## (Other) :4438 (Other):2764
Think a bit on what these summaries mean. Use the space below to add some more relevant non-visual exploratory information you think helps you understand this data:
# Insert your code here, create more chunks as necessary
# review bottom of dataframe
tail(inc)
## Rank Name Growth_Rate Revenue
## 4996 4996 cSubs 0.34 1.34e+07
## 4997 4997 Dot Foods 0.34 4.50e+09
## 4998 4998 Lethal Performance 0.34 6.80e+06
## 4999 4999 ArcaTech Systems 0.34 3.26e+07
## 5000 5000 INE 0.34 6.80e+06
## 5001 5000 ALL4 0.34 4.70e+06
## Industry Employees City State
## 4996 Business Products & Services 19 Montvale NJ
## 4997 Food & Beverage 3919 Mt. Sterling IL
## 4998 Retail 8 Wellington FL
## 4999 Financial Services 63 Mebane NC
## 5000 IT Services 35 Bellevue WA
## 5001 Environmental Services 34 Kimberton PA
# review structure of dataframe
# >>> 5001 companies (two companies tied for 5000th place)
str(inc)
## 'data.frame': 5001 obs. of 8 variables:
## $ Rank : int 1 2 3 4 5 6 7 8 9 10 ...
## $ Name : Factor w/ 5001 levels "(Add)ventures",..: 1770 1633 4423 690 1198 2839 4733 1468 1869 4968 ...
## $ Growth_Rate: num 421 248 245 233 213 ...
## $ Revenue : num 1.18e+08 4.96e+07 2.55e+07 1.90e+09 8.70e+07 ...
## $ Industry : Factor w/ 25 levels "Advertising & Marketing",..: 5 12 13 7 1 20 10 1 5 21 ...
## $ Employees : int 104 51 132 50 220 63 27 75 97 15 ...
## $ City : Factor w/ 1519 levels "Acton","Addison",..: 391 365 635 2 139 66 912 1179 131 1418 ...
## $ State : Factor w/ 52 levels "AK","AL","AR",..: 5 47 10 45 20 45 44 5 46 41 ...
# review which states are represented
# >>> 50 states + DC (Washington, DC) + PR (Puerto Rico)
sort(unique(inc$State))
## [1] AK AL AR AZ CA CO CT DC DE FL GA HI IA ID IL IN KS KY LA MA MD ME MI
## [24] MN MO MS MT NC ND NE NH NJ NM NV NY OH OK OR PA PR RI SC SD TN TX UT
## [47] VA VT WA WI WV WY
## 52 Levels: AK AL AR AZ CA CO CT DC DE FL GA HI IA ID IL IN KS KY LA ... WY
Create a graph that shows the distribution of companies in the dataset by State (ie how many are in each state). There are a lot of States, so consider which axis you should use. This visualization is ultimately going to be consumed on a ‘portrait’ oriented screen (ie taller than wide), which should further guide your layout choices.
# Answer Question 1 here
library(ggplot2)
# create bar chart
# re-order the states by company count, from highest to lowest
ggplot(inc, aes(x = reorder(State, rep(1, length(State)), FUN = sum))) +
geom_bar() +
# flip axes for portrait orientation
coord_flip() +
labs(title = "Distribution of 'Inc. 5000' Companies by State",
subtitle = "50 US States, DC & Puerto Rico",
#x = "State",
y = "Number of Companies") +
# remove label for state axis
theme(axis.title.y = element_blank())
Lets dig in on the state with the 3rd most companies in the data set. Imagine you work for the state and are interested in how many people are employed by companies in different industries. Create a plot that shows the average and/or median employment by industry for companies in this state (only use cases with full data, use R’s complete.cases() function.) In addition to this, your graph should show how variable the ranges are, and you should deal with outliers.
# Answer Question 2 here
# 12 companies have incomplete data in the dataset
nrow(inc) - sum(complete.cases(inc))
## [1] 12
# subset data to include NY companies with full data
NYcos <- inc[complete.cases(inc) & inc$State == "NY", ]
str(NYcos)
## 'data.frame': 311 obs. of 8 variables:
## $ Rank : int 26 30 37 38 48 70 71 124 126 153 ...
## $ Name : Factor w/ 5001 levels "(Add)ventures",..: 529 3822 4972 1037 912 19 2608 3591 3684 3668 ...
## $ Growth_Rate: num 84.4 73.2 67.4 67 53.6 ...
## $ Revenue : num 13700000 8100000 18000000 7100000 5900000 27900000 6900000 11500000 9800000 15400000 ...
## $ Industry : Factor w/ 25 levels "Advertising & Marketing",..: 5 1 1 1 10 1 1 24 21 25 ...
## $ Employees : int 17 79 27 89 32 75 42 28 17 42 ...
## $ City : Factor w/ 1519 levels "Acton","Addison",..: 929 929 929 929 1135 929 929 929 574 162 ...
## $ State : Factor w/ 52 levels "AK","AL","AR",..: 35 35 35 35 35 35 35 35 35 35 ...
summary(NYcos)
## Rank Name Growth_Rate
## Min. : 26 1st Equity : 1 Min. : 0.350
## 1st Qu.:1186 33Across : 1 1st Qu.: 0.670
## Median :2702 5Linx Enterprises : 1 Median : 1.310
## Mean :2612 Access Display Group: 1 Mean : 4.371
## 3rd Qu.:4005 Adafruit : 1 3rd Qu.: 3.580
## Max. :4981 AdCorp Media Group : 1 Max. :84.430
## (Other) :305
## Revenue Industry Employees
## Min. :2.000e+06 Advertising & Marketing : 57 Min. : 1.0
## 1st Qu.:4.300e+06 IT Services : 43 1st Qu.: 21.0
## Median :8.800e+06 Business Products & Services: 26 Median : 45.0
## Mean :5.872e+07 Consumer Products & Services: 17 Mean : 271.3
## 3rd Qu.:2.570e+07 Telecommunications : 17 3rd Qu.: 105.5
## Max. :4.600e+09 Education : 14 Max. :32000.0
## (Other) :137
## City State
## New York :160 NY :311
## Brooklyn : 15 AK : 0
## Rochester: 9 AL : 0
## Buffalo : 5 AR : 0
## Fairport : 5 AZ : 0
## new york : 5 CA : 0
## (Other) :112 (Other): 0
# will cut-off employee count to remove outliers from chart
# but outliers will be included in mean/median/width, etc.
quantile(NYcos$Employees, probs = c(0, 0.25, 0.5, 0.75, 0.9, 0.95, 0.99))
## 0% 25% 50% 75% 90% 95% 99%
## 1.0 21.0 45.0 105.5 300.0 555.5 2273.8
# create box plot
ggplot(NYcos, aes(x = Industry, y = Employees)) +
geom_boxplot() +
# add means
stat_summary(fun.y = "mean", geom = "point", shape = 23, size = 2, fill = "red") +
# flip axes for portrait orientation
# limit employee count to 600 which is >95%-ile
coord_flip(ylim = c(0, 600)) +
# reverse order of industries so alphabetical from top down
scale_x_discrete(limits = rev(levels(NYcos$Industry))) +
labs(title = "Employment by Industry for 'Inc. 5000' Companies in NY State",
subtitle = "Box Plot with Median (Solid Line) & Mean (Red Diamond)",
y = "Employees per Company") +
# remove label for industry axis
theme(axis.title.y = element_blank())
Now imagine you work for an investor and want to see which industries generate the most revenue per employee. Create a chart that makes this information clear. Once again, the distribution per industry should be shown.
# Answer Question 3 here
library(dplyr)
# compute revenue per employee in $K
revpem <- inc[complete.cases(inc), ] %>% mutate(rev = Revenue / Employees / 1000)
# will cut-off rev per employee to remove outliers from chart
# but outliers will be included in mean/median/width, etc.
quantile(revpem$rev, probs = c(0, 0.25, 0.5, 0.75, 0.9, 0.95, 0.99))
## 0% 25% 50% 75% 90% 95%
## 1.801242 125.000000 198.657718 375.000000 750.000000 1221.739130
## 99%
## 3053.000000
# create box plot
# re-order the industries by median rev per employee, from top to bottom
ggplot(revpem, aes(x = reorder(Industry, rev, FUN = median), y = rev)) +
geom_boxplot() +
# add means
stat_summary(fun.y = "mean", geom = "point", shape = 23, size = 2, fill = "blue") +
# flip axes for portrait orientation
# limit rev per employee to 1.2MM which is ~95%-ile
coord_flip(ylim = c(0, 1200)) +
labs(title = "Revenue per Employee by Industry for 'Inc. 5000' Companies",
subtitle = "Box Plot with Median (Solid Line) & Mean (Blue Diamond)",
y = "Revenue per Employee ($K)") +
theme(axis.title.y = element_blank())