library(tidyverse)
library(psych)
library(kableExtra)

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:

Further Stats on the Data

# Insert your code here, create more chunks as necessary

kable(describe(inc)) %>% kable_styling() %>% scroll_box(width = "600px", height = "300px")
vars n mean sd median trimmed mad min max range skew kurtosis se
Rank 1 5001 2.501641e+03 1.443506e+03 2.502e+03 2.501731e+03 1.853250e+03 1.0e+00 5.0000e+03 4.9990e+03 -0.0004896 -1.200432 2.041222e+01
Name* 2 5001 2.501000e+03 1.443809e+03 2.501e+03 2.501000e+03 1.853250e+03 1.0e+00 5.0010e+03 5.0000e+03 0.0000000 -1.200720 2.041650e+01
Growth_Rate 3 5001 4.611826e+00 1.412369e+01 1.420e+00 2.136756e+00 1.215732e+00 3.4e-01 4.2148e+02 4.2114e+02 12.5495059 242.336616 1.997192e-01
Revenue 4 5001 4.822254e+07 2.405423e+08 1.090e+07 1.733497e+07 1.067472e+07 2.0e+06 1.0100e+10 1.0098e+10 22.1744453 722.656318 3.401441e+06
Industry* 5 5001 1.210038e+01 7.328351e+00 1.300e+01 1.205499e+01 8.895600e+00 1.0e+00 2.5000e+01 2.4000e+01 -0.1012333 -1.184515 1.036282e-01
Employees 6 4989 2.327180e+02 1.353128e+03 5.300e+01 8.177511e+01 5.337360e+01 1.0e+00 6.6803e+04 6.6802e+04 29.8104167 1268.671130 1.915720e+01
City* 7 5001 7.320010e+02 4.411164e+02 7.610e+02 7.317383e+02 6.049008e+02 1.0e+00 1.5190e+03 1.5180e+03 -0.0420954 -1.264827 6.237704e+00
State* 8 5001 2.480324e+01 1.563706e+01 2.300e+01 2.444364e+01 1.927380e+01 1.0e+00 5.2000e+01 5.1000e+01 0.1190507 -1.461030 2.211193e-01

Popularity of the industries

companies <- data.frame(table(inc$Industry))
colnames(companies)<- c("Industry", "Count")
kable(companies) %>% kable_styling() %>% scroll_box(width = "600px", height = "300px")
Industry Count
Advertising & Marketing 471
Business Products & Services 482
Computer Hardware 44
Construction 187
Consumer Products & Services 203
Education 83
Energy 109
Engineering 74
Environmental Services 51
Financial Services 260
Food & Beverage 131
Government Services 202
Health 355
Human Resources 196
Insurance 50
IT Services 733
Logistics & Transportation 155
Manufacturing 256
Media 54
Real Estate 96
Retail 203
Security 73
Software 342
Telecommunications 129
Travel & Hospitality 62

Correlation among the variables (numeric)

inc %>% select(Rank, Growth_Rate, Revenue, Employees) %>% cor(use = "complete.obs")
##                    Rank  Growth_Rate     Revenue  Employees
## Rank         1.00000000 -0.397587831 0.082234923  0.0541156
## Growth_Rate -0.39758783  1.000000000 0.006328055 -0.0178689
## Revenue      0.08223492  0.006328055 1.000000000  0.2779332
## Employees    0.05411560 -0.017868897 0.277933213  1.0000000

Rank has a strong positive correlation with Revenue while Employees have a negative relationship with Growth Rate.

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.

# Answer Question 1 here

data <- inc %>% 
  group_by(State) %>% 
  tally()

ggplot(data= data, aes(x= reorder(State, n), y=n)) +
  geom_bar(stat="identity", fill='darkgreen') + 
  ggtitle("Companies per State")+ xlab("State") +
  ylab('Count(Companies)') +
  theme_minimal() + coord_flip() +
  theme(text = element_text(size=8), axis.text.x = element_text(angle = 90, hjust = 1)) 

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.

Based on the previous graph, visually observing, we can see that New York is the state with the third most companies.

# Answer Question 2 here

NY <- inc[inc$State == 'NY',] #gather only NY data
NY <- NY[complete.cases(NY),] # remove incomplete cases (if any)


outliers <- NY %>% ggplot(aes(Industry, Employees)) + geom_boxplot(outlier.colour="red", outlier.shape=8, outlier.size=2) + coord_cartesian(ylim = c(0, 1500)) + ggtitle("Avg Employees per Industry")+ theme(axis.text.x = element_text(angle = 90, hjust = 1)) 

means <- NY %>% group_by(Industry) %>%
  summarise(meanEmp = mean(Employees)) %>% ggplot(aes(Industry, meanEmp))+ geom_bar(stat="identity", fill='darkgreen')+ ggtitle("Avg Employees per Industry") + theme(axis.text.x = element_text(angle = 90, hjust = 1)) 
  
gridExtra::grid.arrange(outliers, means,nrow = 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.

# Answer Question 3 here

ggplot(data=inc, aes(x = Industry, y = Revenue))+ geom_bar(stat="identity", fill='darkgreen') + ggtitle("Revenue per Industry")+ theme(axis.text.x = element_text(angle = 90, hjust = 1)) + coord_flip()