Background

The purpose of the assignment was to explore principles of data visualization with ggplot2.

…………………………………………………………………….

Preliminary EDA

#Read in data on the fastest growing companies in the US, as compiled by Inc. magazine:
inc <- read.csv("https://raw.githubusercontent.com/charleyferrari/CUNY_DATA_608/master/module1/Data/inc5000_data.csv", header= TRUE)

Previewing the first 6 entries and summary statistics:

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         Revenue         
##  Min.   :   1   Length:5001        Min.   :  0.340   Min.   :2.000e+06  
##  1st Qu.:1252   Class :character   1st Qu.:  0.770   1st Qu.:5.100e+06  
##  Median :2502   Mode  :character   Median :  1.420   Median :1.090e+07  
##  Mean   :2502                      Mean   :  4.612   Mean   :4.822e+07  
##  3rd Qu.:3751                      3rd Qu.:  3.290   3rd Qu.:2.860e+07  
##  Max.   :5000                      Max.   :421.480   Max.   :1.010e+10  
##                                                                         
##    Industry           Employees           City              State          
##  Length:5001        Min.   :    1.0   Length:5001        Length:5001       
##  Class :character   1st Qu.:   25.0   Class :character   Class :character  
##  Mode  :character   Median :   53.0   Mode  :character   Mode  :character  
##                     Mean   :  232.7                                        
##                     3rd Qu.:  132.0                                        
##                     Max.   :66803.0                                        
##                     NA's   :12

We then perform some further non-visual exploration of the data to further our familiarity and understanding:

##Table dimensions:
dim(inc) #5001 rows x 8 cols
## [1] 5001    8
##Variable characteristics:
str(inc)
## 'data.frame':    5001 obs. of  8 variables:
##  $ Rank       : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ Name       : chr  "Fuhu" "FederalConference.com" "The HCI Group" "Bridger" ...
##  $ 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   : chr  "Consumer Products & Services" "Government Services" "Health" "Energy" ...
##  $ Employees  : int  104 51 132 50 220 63 27 75 97 15 ...
##  $ City       : chr  "El Segundo" "Dumfries" "Jacksonville" "Addison" ...
##  $ State      : chr  "CA" "VA" "FL" "TX" ...
##Companies per industry:
ind_count <- table(inc$Industry)
ind_count
## 
##      Advertising & Marketing Business Products & Services 
##                          471                          482 
##            Computer Hardware                 Construction 
##                           44                          187 
## Consumer Products & Services                    Education 
##                          203                           83 
##                       Energy                  Engineering 
##                          109                           74 
##       Environmental Services           Financial Services 
##                           51                          260 
##              Food & Beverage          Government Services 
##                          131                          202 
##                       Health              Human Resources 
##                          355                          196 
##                    Insurance                  IT Services 
##                           50                          733 
##   Logistics & Transportation                Manufacturing 
##                          155                          256 
##                        Media                  Real Estate 
##                           54                           96 
##                       Retail                     Security 
##                          203                           73 
##                     Software           Telecommunications 
##                          342                          129 
##         Travel & Hospitality 
##                           62
##Companies per state:
state_count <- table(inc$State)
state_count
## 
##  AK  AL  AR  AZ  CA  CO  CT  DC  DE  FL  GA  HI  IA  ID  IL  IN  KS  KY  LA  MA 
##   2  51   9 100 701 134  50  43  16 282 212   7  28  17 273  69  38  40  37 182 
##  MD  ME  MI  MN  MO  MS  MT  NC  ND  NE  NH  NJ  NM  NV  NY  OH  OK  OR  PA  PR 
## 131  13 126  88  59  12   4 137  10  27  24 158   5  26 311 186  46  49 164   1 
##  RI  SC  SD  TN  TX  UT  VA  VT  WA  WI  WV  WY 
##  16  48   3  82 387  95 283   6 130  79   2   2

…………………………………………………………………….

Question 1

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.

#Import libraries
library(ggplot2)
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v tibble  3.0.4     v dplyr   1.0.2
## v tidyr   1.1.2     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.0
## v purrr   0.3.4
## Warning: package 'tibble' was built under R version 4.0.3
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
#Visualize state count
##count companies per state, reorder based on count, display blue bars, horizontally, with specified labels, and minimal theme.

inc %>% 
  count(State) %>% 
  ggplot(aes(x = reorder(State, n), y = n)) +
  geom_bar(stat = "identity", fill = "steelblue") +
  coord_flip() +
  labs(title = "Distribution of Companies by State", x= "State", y = "Company Count") +
  theme_minimal()

Question 2

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.

#State with 3rd most companies: NY
ny_st <- filter(inc, `State` == 'NY') #filter for NY
ny <- ny_st[complete.cases(ny_st), ] #filter out incomplete cases

##Initial boxplot of Employee Breakdown by Industry
ny %>% 
  ggplot(aes(x = reorder(Industry, Employees), y = Employees)) +
  geom_boxplot(color = "blue", fill = "blue", alpha=0.2, outlier.color = "red", outlier.fill = "red", outlier.size = 2) +
  coord_flip() +
  labs(title = "New York: Employee Breakdown by Industry", x= "Industry", y = "Employees") +
  #ylim(0,1200) +
  theme_minimal()

Based on the guidelines provided above, I elected to display a boxplot. A boxplot is useful for noting the average / median, 1QR, 3QR, outliers, etc. Thus we could plot the average, show our variability, and capture our outliers on ONE plot.

The plot above provides these indicators but we’re much too “zoomed out” due to the large outlier value for ‘Business Products and Services’, thus we zoom in our Employees axis to gain greater insight into our Employee-Industry data:

##Zoomed in boxplot of Employee Breakdown by Industry
ny %>% 
  ggplot(aes(x = reorder(Industry, Employees), y = Employees)) +
  geom_boxplot(color = "blue", fill = "blue", alpha=0.2, outlier.color = "red", outlier.fill = "red", outlier.size = 2) +
  coord_flip() +
  labs(title = "New York: Employee Breakdown by Industry", x= "Industry", y = "Employees") +
  ylim(0,1200) +
  theme_minimal()
## Warning: Removed 7 rows containing non-finite values (stat_boxplot).

While we could certainly zoom in again (ie. limit our yrange upto 100 or so) to gain greater insight into our industries with smaller employee ranges, the boxplot above provides a clearer idea of the level of employability based on industry. The range is noted by the light blue box, the median values are demarkated with dark blue lines, and outlier values are noted with red dots.

Question 3

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.

# Which Industries generate the most revenue per employee? 

inc <- inc[complete.cases(inc), ] #filter our incomplete cases

#First we group by industry, sum corresponding Employee and Revenue columns, and then account for their product with the addition of the 'rev_emp' column, then we plot the corresponding revenue per employee vs. Industry:

inc %>%
  group_by(Industry) %>%
  summarise(Employees = sum(Employees), Revenue = sum(Revenue)) %>%
  mutate( rev_emp = Revenue / Employees) %>%
  
  ggplot(aes(x = reorder(Industry, rev_emp), y = rev_emp)) +
  geom_bar(stat = "identity", fill = "steelblue") +
  coord_flip() +
  labs(title = "Revenue per Employee by Industry", x= "Industry", y = "Revenue Per Employee ($)") +
  theme_minimal()
## `summarise()` ungrouping output (override with `.groups` argument)

From the above breakdown, we can see that the top (3) industries for revenue per employee are: Computer Hardware, Energy, and Construction. While the bottom (3) industries are: Engineering, Security, and Human Resources.

We also see that these values seem high. I would’ve anticipated lower Revenue Per Employee but then again I’ve never dug in to data of this nature …