knitr::opts_chunk$set(warning = FALSE) 

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:

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
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         Revenue         
##  Min.   :   1   Length:5001        Min.   :  0.340   Min.   :2.000e+06  
##  1st Qu.:1252   Class :character   1st Qu.:  0.770   1st Qu.:5.100e+06  
##  Median :2502   Mode  :character   Median :  1.420   Median :1.090e+07  
##  Mean   :2502                      Mean   :  4.612   Mean   :4.822e+07  
##  3rd Qu.:3751                      3rd Qu.:  3.290   3rd Qu.:2.860e+07  
##  Max.   :5000                      Max.   :421.480   Max.   :1.010e+10  
##                                                                         
##    Industry           Employees           City              State          
##  Length:5001        Min.   :    1.0   Length:5001        Length:5001       
##  Class :character   1st Qu.:   25.0   Class :character   Class :character  
##  Mode  :character   Median :   53.0   Mode  :character   Mode  :character  
##                     Mean   :  232.7                                        
##                     3rd Qu.:  132.0                                        
##                     Max.   :66803.0                                        
##                     NA's   :12

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:

Dimension of the data

dim(inc)
## [1] 5001    8

Analysis of companies per state

inc %>% count(State,sort =TRUE,name='NoOfCompanies' )
##    State NoOfCompanies
## 1     CA           701
## 2     TX           387
## 3     NY           311
## 4     VA           283
## 5     FL           282
## 6     IL           273
## 7     GA           212
## 8     OH           186
## 9     MA           182
## 10    PA           164
## 11    NJ           158
## 12    NC           137
## 13    CO           134
## 14    MD           131
## 15    WA           130
## 16    MI           126
## 17    AZ           100
## 18    UT            95
## 19    MN            88
## 20    TN            82
## 21    WI            79
## 22    IN            69
## 23    MO            59
## 24    AL            51
## 25    CT            50
## 26    OR            49
## 27    SC            48
## 28    OK            46
## 29    DC            43
## 30    KY            40
## 31    KS            38
## 32    LA            37
## 33    IA            28
## 34    NE            27
## 35    NV            26
## 36    NH            24
## 37    ID            17
## 38    DE            16
## 39    RI            16
## 40    ME            13
## 41    MS            12
## 42    ND            10
## 43    AR             9
## 44    HI             7
## 45    VT             6
## 46    NM             5
## 47    MT             4
## 48    SD             3
## 49    AK             2
## 50    WV             2
## 51    WY             2
## 52    PR             1

We could see that California state has more companies , followed by Texas and New York

Analyze the number of companies per industry

inc %>% count(Industry,sort =TRUE,name='CompaniesPerIndustry' )
##                        Industry CompaniesPerIndustry
## 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
## 11              Human Resources                  196
## 12                 Construction                  187
## 13   Logistics & Transportation                  155
## 14              Food & Beverage                  131
## 15           Telecommunications                  129
## 16                       Energy                  109
## 17                  Real Estate                   96
## 18                    Education                   83
## 19                  Engineering                   74
## 20                     Security                   73
## 21         Travel & Hospitality                   62
## 22                        Media                   54
## 23       Environmental Services                   51
## 24                    Insurance                   50
## 25            Computer Hardware                   44

We could see more industries are on IT Services

Top 5 Companies with Highest Growth Rate

inc %>% arrange(desc(Growth_Rate)) %>% head(n = 5)%>%select(Rank,Name,Growth_Rate,Industry)
##   Rank                  Name Growth_Rate                     Industry
## 1    1                  Fuhu      421.48 Consumer Products & Services
## 2    2 FederalConference.com      248.31          Government Services
## 3    3         The HCI Group      245.45                       Health
## 4    4               Bridger      233.08                       Energy
## 5    5                DataXu      213.37      Advertising & Marketing

Top 5 Companies with Lowest Growth Rate

inc %>% arrange(desc(Growth_Rate)) %>% tail(n = 5)%>%select(Rank,Name,Growth_Rate,Industry)
##      Rank               Name Growth_Rate               Industry
## 4997 4997          Dot Foods        0.34        Food & Beverage
## 4998 4998 Lethal Performance        0.34                 Retail
## 4999 4999   ArcaTech Systems        0.34     Financial Services
## 5000 5000                INE        0.34            IT Services
## 5001 5000               ALL4        0.34 Environmental Services

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


inc %>% count(State,sort=TRUE) %>%   ggplot(aes(x = n,y= reorder(State,n), fill = n)) +  geom_col(width = .9)+
    labs(x= "Number of companies", y = "States")

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

#clean up the data using complete.cases
#then find and filter the Rank 3 state

inc_rank3 <-inc %>%filter(complete.cases(.)) %>%  add_count(State,name='TotalCompanies')%>%mutate(StateRank=dense_rank(desc(TotalCompanies)))%>% filter(StateRank == 3)

#use box plot ,color the outliers 
ggplot(inc_rank3, aes(x=Employees, y=reorder(Industry, Employees, median, order = TRUE)))+
  geom_boxplot(fill="yellow", alpha=0.2,outlier.color = "red", outlier.fill = "red", outlier.size = 2)+
  scale_x_log10()+
  labs(title = "Employees by Industry", x= "Employees", y = "Industries")    

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

# Which Industries generate the most revenue per employee? 

  #clean up the data
  #find revenue per employee and then plot the data 
  inc %>%
  filter(complete.cases(.))%>%
  group_by(Industry) %>%
  summarise(TotEmployees = sum(Employees), TotRevenue = sum(Revenue)) %>%
  mutate( revPerEmp = TotRevenue / TotEmployees) %>%
  ggplot(aes(x = reorder(Industry, revPerEmp), y = revPerEmp)) +
  geom_bar(stat = "identity", fill = "steelblue") +
  coord_flip() +
  labs(title = "Revenue per Employee by Industry", x= "Industry", y = "Revenue Per Employee") 
## `summarise()` ungrouping output (override with `.groups` argument)

From the chart we could see that “Computer Hardware” generate more revenue per employee