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  117900000
## 2    2        FederalConference.com         248   49600000
## 3    3                The HCI Group         245   25500000
## 4    4                      Bridger         233 1900000000
## 5    5                       DataXu         213   87000000
## 6    6 MileStone Community Builders         179   45700000
##                       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  
##  1st Qu.:1252   @Properties           :   1   1st Qu.:  1  
##  Median :2502   1-Stop Translation USA:   1   Median :  1  
##  Mean   :2502   110 Consulting        :   1   Mean   :  5  
##  3rd Qu.:3751   11thStreetCoffee.com  :   1   3rd Qu.:  3  
##  Max.   :5000   123 Exteriors         :   1   Max.   :421  
##                 (Other)               :4995                
##     Revenue                                    Industry      Employees    
##  Min.   :    2000000   IT Services                 : 733   Min.   :    1  
##  1st Qu.:    5100000   Business Products & Services: 482   1st Qu.:   25  
##  Median :   10900000   Advertising & Marketing     : 471   Median :   53  
##  Mean   :   48222535   Health                      : 355   Mean   :  233  
##  3rd Qu.:   28600000   Software                    : 342   3rd Qu.:  132  
##  Max.   :10100000000   Financial Services          : 260   Max.   :66803  
##                        (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:

library(psych)
describe(inc$Growth_Rate)
##    vars    n mean   sd median trimmed  mad  min max range skew kurtosis
## X1    1 5001 4.61 14.1   1.42    2.14 1.22 0.34 421   421 12.6      242
##     se
## X1 0.2
describe(inc$Revenue)
##    vars    n     mean        sd   median  trimmed      mad     min
## X1    1 5001 48222535 240542281 10900000 17334966 10674720 2000000
##            max       range skew kurtosis      se
## X1 10100000000 10098000000 22.2      723 3401441
prop.table(table(inc$Industry))
## 
##      Advertising & Marketing Business Products & Services 
##                       0.0942                       0.0964 
##            Computer Hardware                 Construction 
##                       0.0088                       0.0374 
## Consumer Products & Services                    Education 
##                       0.0406                       0.0166 
##                       Energy                  Engineering 
##                       0.0218                       0.0148 
##       Environmental Services           Financial Services 
##                       0.0102                       0.0520 
##              Food & Beverage          Government Services 
##                       0.0262                       0.0404 
##                       Health              Human Resources 
##                       0.0710                       0.0392 
##                    Insurance                  IT Services 
##                       0.0100                       0.1466 
##   Logistics & Transportation                Manufacturing 
##                       0.0310                       0.0512 
##                        Media                  Real Estate 
##                       0.0108                       0.0192 
##                       Retail                     Security 
##                       0.0406                       0.0146 
##                     Software           Telecommunications 
##                       0.0684                       0.0258 
##         Travel & Hospitality 
##                       0.0124
describe(inc$Employees)
##    vars    n mean   sd median trimmed  mad min   max range skew kurtosis
## X1    1 4989  233 1353     53    81.8 53.4   1 66803 66802 29.8     1269
##      se
## X1 19.2
prop.table(table(inc$State))
## 
##     AK     AL     AR     AZ     CA     CO     CT     DC     DE     FL 
## 0.0004 0.0102 0.0018 0.0200 0.1402 0.0268 0.0100 0.0086 0.0032 0.0564 
##     GA     HI     IA     ID     IL     IN     KS     KY     LA     MA 
## 0.0424 0.0014 0.0056 0.0034 0.0546 0.0138 0.0076 0.0080 0.0074 0.0364 
##     MD     ME     MI     MN     MO     MS     MT     NC     ND     NE 
## 0.0262 0.0026 0.0252 0.0176 0.0118 0.0024 0.0008 0.0274 0.0020 0.0054 
##     NH     NJ     NM     NV     NY     OH     OK     OR     PA     PR 
## 0.0048 0.0316 0.0010 0.0052 0.0622 0.0372 0.0092 0.0098 0.0328 0.0002 
##     RI     SC     SD     TN     TX     UT     VA     VT     WA     WI 
## 0.0032 0.0096 0.0006 0.0164 0.0774 0.0190 0.0566 0.0012 0.0260 0.0158 
##     WV     WY 
## 0.0004 0.0004

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.

# option 2 using forcats library
library(forcats)
ggplot(inc, aes(x=fct_infreq(State))) + 
    geom_bar(fill = "#58BFFF", stat="count") +
    coord_flip() +
    geom_text(aes(label=..count..), stat="count", size=3, 
              hjust=-0.2, color="darkgray") +
    xlab("State Abbreviation") +
    ylab("Number of Companies in State") +
    ggtitle("5,000 Fastest Growing Companies in US") + 
    theme(panel.background = element_blank())

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.

NY <- subset(inc, State=="NY")
NY <- NY[complete.cases(NY), ]

ggplot(NY, aes(x=Industry, y=Employees)) + 
    #geom_violin(adjust=.5, coef = 0) +
    geom_boxplot(width=.5, fill="#58BFFF", outlier.colour=NA) +
    stat_summary(aes(colour = "mean"), fun.y = mean, geom="point", fill="red", 
                 colour="red", shape=21, size=2, show.legend=TRUE) +
    stat_summary(aes(colour = "median"), fun.y = median, geom="point", fill="blue", 
                 colour="blue", shape=21, size=2, show.legend=TRUE) +
    # I can't for the life of me figure out how to get a legend to show what the colored points represent  :-(
    coord_flip(ylim = c(0, 1500), expand = TRUE) +   
    scale_y_continuous(labels = scales::comma,
                       breaks = seq(0, 1500, by = 150)) +
    xlab("Industry") +
    ylab("") +
    ggtitle("Mean and Median Employment by Industry for 311 Fastest Growing Companies in NY") + 
    theme(panel.background = element_blank(), legend.position = "top")

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.

library(dplyr)
revenue <-inc[complete.cases(inc),] %>%
                      group_by(Industry) %>%
                      summarise(sumR=sum(Revenue),sumE=sum(Employees)) %>%
                      mutate(rev_per_emp = sumR/sumE) 

ggplot(revenue, aes(x=reorder(Industry, -rev_per_emp),y=rev_per_emp)) + 
    geom_bar(fill = "#58BFFF", stat="identity") +
    coord_flip() + 
    xlab("Industry") +
    ylab("Revenue Per Employee") +
    ggtitle("Revenue Per Employee") +
    theme(panel.background = element_blank(), legend.position = "top")