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:

# Insert your code here, create more chunks as necessary
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 ...
library(kableExtra)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:kableExtra':
## 
##     group_rows
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
head(inc) %>% kable() %>% kable_styling()
Rank Name Growth_Rate Revenue Industry Employees City State
1 Fuhu 421.48 1.179e+08 Consumer Products & Services 104 El Segundo CA
2 FederalConference.com 248.31 4.960e+07 Government Services 51 Dumfries VA
3 The HCI Group 245.45 2.550e+07 Health 132 Jacksonville FL
4 Bridger 233.08 1.900e+09 Energy 50 Addison TX
5 DataXu 213.37 8.700e+07 Advertising & Marketing 220 Boston MA
6 MileStone Community Builders 179.38 4.570e+07 Real Estate 63 Austin TX
### Top 10 companies by revenue from this big list
top10_by_revenue = inc %>% arrange(desc(Revenue)) %>% head(10)
top10_by_revenue$Revenue = sapply(top10_by_revenue$Revenue, function(x) paste(round((x / 1e9), 1), " Billion"))
top10_by_revenue %>% kable() %>% kable_styling()
Rank Name Growth_Rate Revenue Industry Employees City State
4788 CDW 0.41 10.1 Billion Computer Hardware 6800 Vernon Hills IL
3853 ABC Supply 0.73 4.7 Billion Construction 6549 Beloit WI
4936 Coty 0.36 4.6 Billion Consumer Products & Services 10000 New York NY
4997 Dot Foods 0.34 4.5 Billion Food & Beverage 3919 Mt. Sterling IL
4716 Westcon Group 0.44 3.8 Billion IT Services 3000 Tarrytown NY
4246 American Tire Distributors 0.59 3.5 Billion Consumer Products & Services 3341 Huntersville NC
4052 Kum & Go 0.65 2.8 Billion Retail 4589 West Des Moines IA
4802 Boise Cascade 0.41 2.8 Billion Construction 4470 Boise ID
1397 EnvisionRxOptions 2.88 2.7 Billion Health 625 Twinsburg OH
2522 DLA Piper 1.41 2.4 Billion Business Products & Services 4036 Chicago IL
### Top 10 states in this list by frequency or the number of companies in these states
table(inc$State) %>% sort(decreasing = TRUE) %>% head(10) %>% kable() %>% kable_styling()
Var1 Freq
CA 701
TX 387
NY 311
VA 283
FL 282
IL 273
GA 212
OH 186
MA 182
PA 164

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.

library(ggplot2)
# Answer Question 1 here

inc %>% count(State) %>%
  ggplot(aes(x=reorder(State, n), y=n)) +
  geom_bar(stat = 'identity') +
  coord_flip() +
  theme_classic(base_size = 11) +
  xlab("state wise counts") +
  ylab("state") +
  ggtitle("Top growing companies in the U.S. - state wise counts")

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.

# Answer Question 2 here
## Finding the state with the 3rd largest number of companies
inc_complete = inc[complete.cases(inc), ]

third_most_companies_state <- toString(as.data.frame(list(sort(table(inc_complete$State), decreasing = TRUE)))[3,1])

ny_df <- inc_complete[inc_complete$State == third_most_companies_state,]

ggplot(ny_df, aes(x=factor(Industry), y=Employees)) +
  geom_boxplot() +
  xlab("Industry") +
  ylab("Employees counts") +
  ggtitle("NY - industry wise counts") +
  stat_summary(fun.y=mean, colour="darkred", geom="point", 
               shape=18, size=3) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

### Removing the outliers
max_value <- vector()

for (i in levels(ny_df$Industry))
  {
  upper.limit <- quantile(ny_df[ny_df$Industry == i, ]$Employees)[4] +
    1.5*IQR(ny_df[ny_df$Industry == i, ]$Employees)
    
    max_value <- c(max_value, upper.limit)
    
  }

#Now we have the max value for each category for the data notn outlier. We will use the minimum value as 0
## Plotting without the outliers

ggplot(ny_df, aes(x=factor(Industry), y=Employees)) +
  geom_boxplot(outlier.colour = NA) +
  xlab("Industry") +
  ylab("Employees counts") +
  ggtitle("NY - industry wise counts") +
  stat_summary(fun.y=mean, colour="darkred", geom="point", 
               shape=18, size=3) +
  coord_cartesian(ylim = c(0, max(max_value))) +
  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.

# Answer Question 3 here
inc_revenue_per_employee_df <- group_by(inc_complete, Industry) %>% summarise(Revenue_per_employee = sum(Revenue) / sum(Employees))

ggplot(inc_revenue_per_employee_df, aes(x=reorder(Industry, Revenue_per_employee), y=Revenue_per_employee)) + 
  geom_bar(stat = 'Identity') +
  coord_flip() +
  xlab("Industry") +
  ylab("Revenue per employee (in dollars)") +
  ggtitle("Revenue per employee - industry wise") +
  scale_y_continuous(labels = scales::comma)