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. lets read this in:

inc <- read.csv("https://raw.githubusercontent.com/charleyferrari/CUNY_DATA_608/master/module1/Data/inc5000_data.csv", header= TRUE)
# Import dplyr and ggplot2 
library(dplyr)
library(ggplot2)
library(tidyverse)
library(knitr)

And lets 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:

Lets examine the data

# Columns
nrow(inc)
## [1] 5001
# Rows
ncol(inc)
## [1] 8
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 ...
# Levels
levels(inc$Industry)
##  [1] "Advertising & Marketing"      "Business Products & Services"
##  [3] "Computer Hardware"            "Construction"                
##  [5] "Consumer Products & Services" "Education"                   
##  [7] "Energy"                       "Engineering"                 
##  [9] "Environmental Services"       "Financial Services"          
## [11] "Food & Beverage"              "Government Services"         
## [13] "Health"                       "Human Resources"             
## [15] "Insurance"                    "IT Services"                 
## [17] "Logistics & Transportation"   "Manufacturing"               
## [19] "Media"                        "Real Estate"                 
## [21] "Retail"                       "Security"                    
## [23] "Software"                     "Telecommunications"          
## [25] "Travel & Hospitality"
levels(inc$State)
##  [1] "AK" "AL" "AR" "AZ" "CA" "CO" "CT" "DC" "DE" "FL" "GA" "HI" "IA" "ID"
## [15] "IL" "IN" "KS" "KY" "LA" "MA" "MD" "ME" "MI" "MN" "MO" "MS" "MT" "NC"
## [29] "ND" "NE" "NH" "NJ" "NM" "NV" "NY" "OH" "OK" "OR" "PA" "PR" "RI" "SC"
## [43] "SD" "TN" "TX" "UT" "VA" "VT" "WA" "WI" "WV" "WY"

Top 10 fastest growing companies in US

# Sorted by Growth_Rate
top_10 <- inc %>% arrange(desc(Growth_Rate)) %>% head(10) %>% select(c(Rank, Name, Growth_Rate, Revenue, City, State))
kable(top_10)
Rank Name Growth_Rate Revenue City State
1 Fuhu 421.48 1.179e+08 El Segundo CA
2 FederalConference.com 248.31 4.960e+07 Dumfries VA
3 The HCI Group 245.45 2.550e+07 Jacksonville FL
4 Bridger 233.08 1.900e+09 Addison TX
5 DataXu 213.37 8.700e+07 Boston MA
6 MileStone Community Builders 179.38 4.570e+07 Austin TX
7 Value Payment Systems 174.04 2.550e+07 Nashville TN
8 Emerge Digital Group 170.64 2.390e+07 San Francisco CA
9 Goal Zero 169.81 3.310e+07 Bluffdale UT
10 Yagoozon 166.89 1.860e+07 Warwick RI

Top 10 cities in US

# Top 10 cities in US with the 1000 fastest growing companies
top_10_cities <- inc %>% arrange(desc(Growth_Rate)) %>% head(1000) %>% count(City, sort = TRUE) %>% head(10)
kable(top_10_cities)
City n
New York 35
San Francisco 26
Chicago 20
Austin 19
Atlanta 17
Houston 15
Irvine 15
Boston 14
San Diego 11
Washington 11
# How many unique industries in this dataset?
print(paste0("No of unique records: ", length(unique(inc$Industry))))
## [1] "No of unique records: 25"
unique(inc$Industry)
##  [1] Consumer Products & Services Government Services         
##  [3] Health                       Energy                      
##  [5] Advertising & Marketing      Real Estate                 
##  [7] Financial Services           Retail                      
##  [9] Software                     Computer Hardware           
## [11] Logistics & Transportation   Food & Beverage             
## [13] IT Services                  Business Products & Services
## [15] Education                    Construction                
## [17] Manufacturing                Telecommunications          
## [19] Security                     Human Resources             
## [21] Travel & Hospitality         Media                       
## [23] Environmental Services       Engineering                 
## [25] Insurance                   
## 25 Levels: Advertising & Marketing ... Travel & Hospitality
# Sorting by industries with the 1000 fastest growing companies in the US
inc %>% arrange(desc(Growth_Rate)) %>% head(1000) %>% count(Industry, sort=TRUE) %>% head(10)
## # A tibble: 10 x 2
##    Industry                         n
##    <fct>                        <int>
##  1 IT Services                    120
##  2 Advertising & Marketing        112
##  3 Software                        83
##  4 Health                          77
##  5 Business Products & Services    67
##  6 Financial Services              65
##  7 Consumer Products & Services    61
##  8 Retail                          59
##  9 Government Services             56
## 10 Energy                          38
# Top 10 industries with the most cases
countind <- inc %>% count(Industry)
arrange(countind, desc(n)) %>% top_n(10)
## Selecting by n
## # A tibble: 10 x 2
##    Industry                         n
##    <fct>                        <int>
##  1 IT Services                    733
##  2 Business Products & Services   482
##  3 Advertising & Marketing        471
##  4 Health                         355
##  5 Software                       342
##  6 Financial Services             260
##  7 Manufacturing                  256
##  8 Consumer Products & Services   203
##  9 Retail                         203
## 10 Government Services            202
glimpse(inc)
## Observations: 5,001
## Variables: 8
## $ Rank        <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,...
## $ Name        <fct> Fuhu, FederalConference.com, The HCI Group, Bridge...
## $ Growth_Rate <dbl> 421.48, 248.31, 245.45, 233.08, 213.37, 179.38, 17...
## $ Revenue     <dbl> 1.179e+08, 4.960e+07, 2.550e+07, 1.900e+09, 8.700e...
## $ Industry    <fct> Consumer Products & Services, Government Services,...
## $ Employees   <int> 104, 51, 132, 50, 220, 63, 27, 75, 97, 15, 149, 16...
## $ City        <fct> El Segundo, Dumfries, Jacksonville, Addison, Bosto...
## $ State       <fct> CA, VA, FL, TX, MA, TX, TN, CA, UT, RI, VA, CA, FL...

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.

# Counts by state
statecount <- group_by(inc, State) %>% summarize(Count=n())

# Plot
ggplot(statecount, aes(x=reorder(State,Count),Count)) + 
  geom_bar(stat="identity", fill="#4dbcc5") +
  geom_text(aes(label=round(Count, digits=2)), vjust=0.2, size=2, position=position_dodge(width = 1), hjust=1) +
  theme_minimal() +
  theme(axis.text.x=element_text(size=6, vjust=0.5)) +
  theme(axis.text.y=element_text(size=6, vjust=0.5)) +
  labs( x="State", y="No of Companies") +
  coord_flip() +
  ggtitle("Companies by State")

Quesiton 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.

# Employee count by Industry
nyemps <- filter(inc, State=="NY") %>% select(Industry, Name, Employees)
# Graph
nyemp <- group_by(nyemps, Industry) %>% summarize(m = mean(Employees), max= max(Employees), min = min(Employees)) %>% na.omit()

upper <- nyemp$max
lower <- nyemp$min

ggplot(nyemp, aes(x = Industry, y =m, ymax=max,  ymin = min, lower = lower, upper= upper)) + 
  geom_boxplot(outlier.shape = NA) + coord_flip() +
  labs(title="Employees (NY) by Industry", y = "Mean")

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.

# Get revenue per Employee Data
rev_per_emp <- select(inc, Industry, Revenue, Employees) %>%
  na.omit() %>% group_by(Industry) %>%
  summarise(TotalRev = sum(Revenue), TotalEmp = sum(Employees)) %>%
  mutate(RevEmployee = TotalRev / TotalEmp)

# Graph
ggplot(data = rev_per_emp, aes(x = reorder(Industry, RevEmployee), y = RevEmployee)) + 
  geom_bar(stat="identity", fill="#333333") +
  geom_text(data = filter(rev_per_emp, RevEmployee>150000),
            aes(x = Industry, y = RevEmployee, label=scales::dollar_format()(RevEmployee)), 
            hjust=1.1, vjust=0.4, color="#FFFFFF", size=3) +
  geom_text(data = filter(rev_per_emp, RevEmployee<150000),
            aes(x = Industry, y = RevEmployee, label=scales::dollar_format()(RevEmployee)), 
            hjust=-0.1, vjust=0.4, color="#333333", size=3) +
  coord_flip() + 
  ggtitle("Revenue per Employee per Industry") + labs(x = "", y = "") +
  theme(panel.background = element_blank(),
        axis.ticks = element_blank(),
        axis.text.x = element_blank(),
        axis.text.y = element_text(size = 8, margin = margin(r=-20)))