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/jzuniga123/SPS/master/DATA%20608/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:

sapply(inc, class)
##        Rank        Name Growth_Rate     Revenue    Industry   Employees 
##   "integer"    "factor"   "numeric"   "numeric"    "factor"   "integer" 
##        City       State 
##    "factor"    "factor"
cor(sapply(inc, as.integer), use = "complete.obs")
##                    Rank         Name Growth_Rate       Revenue
## Rank         1.00000000 -0.027666350 -0.39879532  0.0798308497
## Name        -0.02766635  1.000000000  0.01633000 -0.0211752932
## Growth_Rate -0.39879532  0.016330000  1.00000000  0.0316198749
## Revenue      0.07983085 -0.021175293  0.03161987  1.0000000000
## Industry    -0.02076674 -0.008452281 -0.02974976  0.0069751810
## Employees    0.04620016  0.004948993 -0.01642358  0.3007133385
## City         0.00883184  0.053550228 -0.01276701  0.0016361719
## State        0.04553538  0.003877628 -0.01130775  0.0006872054
##                 Industry    Employees         City         State
## Rank        -0.020766742  0.046200165  0.008831840  0.0455353819
## Name        -0.008452281  0.004948993  0.053550228  0.0038776285
## Growth_Rate -0.029749760 -0.016423583 -0.012767014 -0.0113077540
## Revenue      0.006975181  0.300713339  0.001636172  0.0006872054
## Industry     1.000000000  0.021539833 -0.024099990  0.0148533537
## Employees    0.021539833  1.000000000  0.018803258 -0.0259397634
## City        -0.024099990  0.018803258  1.000000000 -0.0563198608
## State        0.014853354 -0.025939763 -0.056319861  1.0000000000

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)
library(dplyr)
library(tidyr)

State_Counts <- inc %>%
  count(State)

ggplot(data=State_Counts, aes(x=reorder(State, n), y=n)) +
  labs(x="State", y="Number of Companies") +  
  geom_bar(stat="identity", fill="steelblue") +
  coord_flip()

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.

library(ggplot2)
library(dplyr)
library(tidyr)

Employees <- inc %>%
  filter(complete.cases(.)) %>%
  group_by(State) %>%
  mutate(NumCos = n()) %>%
  arrange(desc(NumCos)) %>%
  ungroup() %>%
  mutate(NumCo_Rank = dense_rank(desc(NumCos))) %>%
  filter(NumCo_Rank == 3) %>%
  group_by(Industry)

ggplot(Employees, aes(x=Industry, y=Employees)) +
  geom_boxplot(outlier.shape = NA, fill="steelblue") + 
  scale_y_continuous(limits = quantile(Employees$Employees, c(0.1, 0.9))) +
  coord_flip()

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(ggplot2)
library(dplyr)
library(tidyr)

Industries <- inc %>%
  group_by(Industry) %>%
  filter(complete.cases(Revenue, Employees)) %>%
  summarise(SumRev = sum(Revenue)/10^6, SumEmp=sum(Employees)) %>% 
  mutate(Prod = SumRev / SumEmp)

ggplot(data=Industries, aes(x=reorder(Industry, SumRev), y=SumRev, fill=Prod)) +
  scale_fill_gradient(low = 'grey', high = 'steelblue', name = "Productivity") + 
  labs(x="Industry", y="Revenue (Millions)") +  
  geom_bar(stat="identity") +
  coord_flip()

References

https://rpubs.com/josezuniga/212486

https://rpubs.com/josezuniga/228184

https://stackoverflow.com/questions/34967837/rank-variable-by-group-dplyr

https://stackoverflow.com/questions/5677885/ignore-outliers-in-ggplot2-boxplot

https://rstudio-pubs-static.s3.amazonaws.com/3364_d1a578f521174152b46b19d0c83cbe7e.html