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)

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:

# I think the summary gives us nearly all the info we need, but I like
# to examine the data closer to get into the specifics of the data
# For example, I like to check the number of rows in the df
nrow(inc)
## [1] 5001
# We can see there are 5,001 rows despite the data set being described 
# as the top 5,000 fastest growing companies. s a first step towards 
# figuring out why, I looked at the final rows of the 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
# We can see there are two companies ranked 5,000, hence 5,001 rows. I also 
# like to study some of the specific columns to get a deeper understanding 
# of each column, i.e., how many state codes are there in the data set? 
unique(unlist(inc$State))
##  [1] CA VA FL TX MA TN UT RI SC DC NJ OH WA ME NY CO GA IL AZ NC MD MN OK
## [24] PA CT IN MS WI WY MI MO KS OR NE AL HI NV IA KY ID AK LA DE AR NH VT
## [47] NM SD ND PR MT WV
## 52 Levels: AK AL AR AZ CA CO CT DC DE FL GA HI IA ID IL IN KS KY LA ... WY
# We see 52 codes, the 50 states plus DC and Puerto Rico. Next I check the
# Industry column to see how many they are there and if they're duplicative.
unique(unlist(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
# We see 25 different industry categories and none of them overlap.
# For the numerical columns Growth_Rate, Revenue and Employees, I like to
# do some binning to get a feel for how skewed they are. I could already 
# discern skew from the summary function and would usually explore this via
# plots, but the assignment asks for non-visual exploratory information.
# First, the bins for Growth_Rate:
br_gr = seq(min(inc$Growth_Rate),max(inc$Growth_Rate),by=(max(inc$Growth_Rate) - min(inc$Growth_Rate))/10)
ranges_gr = paste(head(br_gr,-1), br_gr[-1], sep=" - ")
freq_gr = hist(inc$Growth_Rate, breaks=br_gr, include.lowest=TRUE, plot=FALSE)
data.frame(range = ranges_gr, frequency = freq_gr$counts)
##                range frequency
## 1      0.34 - 42.454      4927
## 2    42.454 - 84.568        49
## 3   84.568 - 126.682        11
## 4  126.682 - 168.796         5
## 5   168.796 - 210.91         4
## 6   210.91 - 253.024         4
## 7  253.024 - 295.138         0
## 8  295.138 - 337.252         0
## 9  337.252 - 379.366         0
## 10  379.366 - 421.48         1
# We see that the extraordinary majority of growth rates are 42.454% or lower
# Second, the bins for Revenue:
br_rev = seq(min(inc$Revenue),max(inc$Revenue),by=(max(inc$Revenue) - min(inc$Revenue))/10)
ranges_rev = paste(head(br_rev,-1), br_rev[-1], sep=" - ")
freq_rev = hist(inc$Revenue, breaks=br_rev, include.lowest=TRUE, plot=FALSE)
data.frame(range = ranges_rev, frequency = freq_rev$counts)
##                      range frequency
## 1       2e+06 - 1011800000      4976
## 2  1011800000 - 2021600000        15
## 3  2021600000 - 3031400000         4
## 4  3031400000 - 4041200000         2
## 5   4041200000 - 5.051e+09         3
## 6   5.051e+09 - 6060800000         0
## 7  6060800000 - 7070600000         0
## 8  7070600000 - 8080400000         0
## 9  8080400000 - 9090200000         0
## 10   9090200000 - 1.01e+10         1
# Again, we see the overwhelming majority of Revenue amts in the lowest bin
# Lastly, the bins for number of employees:
br_emp = seq(min(inc$Employees[!is.na(inc$Employees)]),max(inc$Employees[!is.na(inc$Employees)]),by=(max(inc$Employees[!is.na(inc$Employees)]) - min(inc$Employees[!is.na(inc$Employees)]))/10)
ranges_emp = paste(head(br_emp,-1), br_emp[-1], sep=" - ")
freq_emp = hist(inc$Employees, breaks=br_emp, include.lowest=TRUE, plot=FALSE)
data.frame(range = ranges_emp, frequency = freq_emp$counts)
##                range frequency
## 1         1 - 6681.2      4968
## 2   6681.2 - 13361.4        14
## 3  13361.4 - 20041.6         5
## 4  20041.6 - 26721.8         0
## 5    26721.8 - 33402         1
## 6    33402 - 40082.2         0
## 7  40082.2 - 46762.4         0
## 8  46762.4 - 53442.6         0
## 9  53442.6 - 60122.8         0
## 10   60122.8 - 66803         1
# Again, the overwhelming majority of employee counts are in the 1st bin

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.

install.packages("ggplot2", repos='https://mirrors.nics.utk.edu/cran/')
library(ggplot2)
install.packages("forcats", repos='https://mirrors.nics.utk.edu/cran/')
library(forcats)
g <- ggplot(inc, aes(State))
g + geom_bar(aes(fct_infreq(factor(State)), fill=State), position = position_stack(reverse = TRUE), show.legend = F) + coord_flip() + ylab("Number of Companies in Top 5,000") + ggtitle("5,000 Fastest Growing Companies - Count by State")

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.

inc_ny <- subset(inc,State=="NY")
p <- ggplot(inc_ny, aes(inc_ny$Industry, inc_ny$Employees))
p + geom_boxplot(na.rm = TRUE) + ylim(0,1000) + theme(axis.text.x = element_text(angle = 90, hjust = 1))

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.

install.packages("dplyr", repos='https://mirrors.nics.utk.edu/cran/')
library(dplyr)
industries <- group_by(inc_ny, Industry)
inc_ny_sum <- summarize(industries, rev_per_employees = sum(Revenue)/sum(Employees))
## Warning: package 'bindrcpp' was built under R version 3.4.2
r <- ggplot(inc_ny_sum, aes(inc_ny_sum$Industry, inc_ny_sum$rev_per_employees))
r + geom_point() + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + scale_y_continuous(labels = scales::dollar) + xlab("Industry") + ylab("Revenue per Employee")