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
inc <- as.data.table(inc)

# list rows of data that have missing values 
inc[!complete.cases(inc),]
##     Rank                             Name Growth_Rate   Revenue
##  1:  183           First Flight Solutions       22.32   2700000
##  2: 1064                         Popchips        3.98  93300000
##  3: 1124                       Vocalocity        3.72  42900000
##  4: 1653                     Higher Logic        2.36   6000000
##  5: 1686      Global Communications Group        2.30   3600000
##  6: 2197              JeffreyM Consulting        1.68  12100000
##  7: 2743               Excalibur Exhibits        1.27   9900000
##  8: 3001       Heartland Business Systems        1.12 156300000
##  9: 3978                             SSEC        0.68  80400000
## 10: 4112 Carolinas Home Medical Equipment        0.64   3300000
## 11: 4566                         Oakbrook        0.48   8900000
## 12: 4968                   Popcorn Palace        0.35   5500000
##                         Industry Employees          City State
##  1:   Logistics & Transportation        NA  Emerald Isle    NC
##  2:              Food & Beverage        NA San Francisco    CA
##  3:           Telecommunications        NA       Atlanta    GA
##  4:                     Software        NA    Washington    DC
##  5:           Telecommunications        NA     Englewood    CO
##  6: Business Products & Services        NA      Bellevue    WA
##  7: Business Products & Services        NA       houston    TX
##  8:                  IT Services        NA  Little Chute    WI
##  9:                Manufacturing        NA       Horsham    PA
## 10:                       Health        NA      Matthews    NC
## 11:                  Real Estate        NA       Madison    WI
## 12:              Food & Beverage        NA Schiller Park    IL
inc.final <- na.omit(inc)

# Growth rate by indsutry
datatable((arrange(inc.final[, .(mean_growth_rate = mean(Growth_Rate),
                median_growth_rate = median(Growth_Rate),
                min_growth_rate = min(Growth_Rate),
                max_growth_rate = max(Growth_Rate)), by = .(Industry)], desc(mean_growth_rate))), options = list(
  columnDefs = list(list(className = 'dt-center')),
  pageLength = 5,
  lengthMenu = c(5, 10, 15, 20)
))
# Revenue summary by industry
datatable((arrange(inc.final[, .(mean_rev = mean(Revenue),
            median_rev = median(Revenue),
            min_rev = min(Revenue),
            max_rev = max(Revenue)), by = .(Industry)], desc(mean_rev))), options = list(
  columnDefs = list(list(className = 'dt-center')),
  pageLength = 5,
  lengthMenu = c(5, 10, 15, 20)
))
# number of employees by industry
datatable((arrange(inc.final[, .(sum_employee = sum(Employees),
                    mean_employee = mean(Employees),
                    min_employee = min(Employees),
                    max_employee = max(Employees)), by =.(Industry)], desc(sum_employee))), options = list(
  columnDefs = list(list(className = 'dt-center')),
  pageLength = 5,
  lengthMenu = c(5, 10, 15, 20)
))
# number of cities by industry
kable(head(arrange(inc.final[, .(count = length(unique(City))), by =.(Industry)], desc(count)),10))
Industry count
IT Services 388
Business Products & Services 292
Health 257
Advertising & Marketing 252
Manufacturing 223
Software 202
Financial Services 191
Retail 164
Construction 157
Consumer Products & Services 152
# number of state by industry
kable(head(arrange(inc.final[, .(count = length(unique(State))), by =.(Industry)], desc(count)),10))
Industry count
Health 44
Software 44
IT Services 44
Advertising & Marketing 43
Business Products & Services 42
Financial Services 39
Manufacturing 38
Retail 37
Construction 36
Consumer Products & Services 34
# number of cities by state
kable(head(arrange(inc.final[, .(count = length(unique(City))), by =.(State)], desc(count)),10))
State count
CA 204
IL 104
FL 102
NJ 97
NY 90
PA 80
OH 79
MA 72
TX 66
VA 56
# number of companies by industry
kable(head(arrange(inc.final[, .(count = length(unique(Name))), by = .(Industry)], desc(count)),10))
Industry count
IT Services 732
Business Products & Services 480
Advertising & Marketing 471
Health 354
Software 341
Financial Services 260
Manufacturing 255
Consumer Products & Services 203
Retail 203
Government Services 202
# number of companies by cities
kable(head(arrange(inc.final[, .(count = length(unique(Name))), by = .(City)], desc(count)),10))
City count
New York 160
Chicago 90
Austin 88
Houston 76
San Francisco 74
Atlanta 73
San Diego 67
Seattle 52
Boston 43
Denver 42

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.comp <- arrange(inc[, .(num_comp = length(Name)),
                      by =.(State)], desc(num_comp))
plot.comp <- ggplot(data.comp, 
                aes(reorder(State, num_comp), num_comp)) +
  geom_point(size=0.5) + 
  geom_segment(aes(x=State, xend=State, y=0, yend=num_comp)) +
  geom_text(aes(label = paste0(num_comp)),
                  color = "red", size = 2.5, hjust = -0.1)+
  scale_y_continuous(breaks = seq(0,800,100),labels = comma) +
  labs(title = "Number Of Companies By State",
       x = "State",
       y = "Number Of Companies") +
  coord_flip()

plot.comp

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
# exploring state with 3rd most companies
head(arrange(inc.final[, .(count = length(unique(Name))), by = .(State)], desc(count)))
##   State count
## 1    CA   700
## 2    TX   386
## 3    NY   311
## 4    VA   283
## 5    FL   282
## 6    IL   272
data.se <- inc[complete.cases(inc),][State == 'NY']
median.label <- paste0("Median Number of Employees(NY):  ", median(data.se$Employees))
plot.se <- ggplot(data.se,aes(Industry, Employees)) +
  geom_boxplot(outlier.shape = NA) +
  geom_hline(yintercept = median(data.se$Employees),
               color="red", 
               linetype="dashed") +
  scale_y_continuous(limits = quantile(data.se$Employees, c(0.1,0.9))) +
  labs(title = "Number of Employees by Industry in the state of NY",
     x = "Industry",
     y = "Number of Employees") +
  geom_text(aes(x=3, label=median.label, y = 150), 
            size = 3,
            colour="red") +
  coord_flip()

plot.se

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
data.rpe <-inc[complete.cases(inc),]%>%
                      select(Revenue,Industry,Employees)%>%
                      group_by(Industry)%>%
                      summarise(Rev_ind=sum(Revenue),Emp_ind=sum(Employees))%>%
                      mutate(RPE = Rev_ind/Emp_ind)%>%
                      arrange(desc(RPE))%>%
                      select(Industry,RPE)

plot.rpe <- ggplot(data.rpe, aes(x=Industry,y=RPE),value) +   
  stat_summary(fun.y = "sum", geom = "bar", position = "identity", aes(fill=RPE)) + 
  labs(title="Revenue Per Employee Distribution Per Industry",
         y="Revenue Per Employee", 
         x="Industry") + 
  coord_flip()

plot.rpe