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)

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✓ ggplot2 3.3.6     ✓ purrr   0.3.4
## ✓ tibble  3.1.6     ✓ dplyr   1.0.8
## ✓ tidyr   1.2.0     ✓ stringr 1.4.0
## ✓ readr   2.1.2     ✓ forcats 0.5.1
## Warning: package 'tidyr' was built under R version 4.0.5
## Warning: package 'readr' was built under R version 4.0.5
## Warning: package 'dplyr' was built under R version 4.0.5
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()

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:

inc_state <- inc %>%
  group_by(State) %>% arrange(State) %>% summarize(mean_revenue = mean(Revenue), sd_revenue = sd(Revenue), median_revenue = median(Revenue), iqr_revenue = IQR(Revenue), companies = sum(State == State))


inc_state <- inc_state[order(-inc_state$mean_revenue),]

head(inc_state)
## # A tibble: 6 × 6
##   State mean_revenue sd_revenue median_revenue iqr_revenue companies
##   <chr>        <dbl>      <dbl>          <dbl>       <dbl>     <int>
## 1 ID      231523529. 686093884.        8800000    11600000        17
## 2 AK      171500000  235890822.      171500000   166800000         2
## 3 IA      123142857. 525683101.       14650000    18000000        28
## 4 IL      121773993. 702149071.       14700000    29800000       273
## 5 HI       99485714. 205452390.       15200000    38600000         7
## 6 WI       92362025. 529764518.       10900000    20650000        79

Comments

It is worth noting that median and interquartile range may be better representations of this data, because outliers do not impact the median, while they significantly impact the mean. In Idaho, for example, the mean revenue is the highest of all of the states, but the median revenue ranks \(40^{th}\) among all of the states.

When ordered by median revenue, lower populated states like Alaska, Wyoming, and Vermont ranked the highest.

inc_state_median <- inc_state[order(-inc_state$median_revenue),]

head(inc_state_median)
## # A tibble: 6 × 6
##   State mean_revenue sd_revenue median_revenue iqr_revenue companies
##   <chr>        <dbl>      <dbl>          <dbl>       <dbl>     <int>
## 1 AK      171500000  235890822.      171500000   166800000         2
## 2 WY       34750000   37830213.       34750000    26750000         2
## 3 VT       46200000   68100720.       23100000    26550000         6
## 4 MS       43766667.  63785198.       19400000    38375000        12
## 5 LA       56648649. 118270698.       16800000    35200000        37
## 6 MI       61950794. 216337592.       15900000    29900000       126

I will subset the data to look for the highest median revenue in states with more than 50 companies.

inc_state_sub_50 <- subset(inc_state_median, companies >= 50)

head(inc_state_sub_50)
## # A tibble: 6 × 6
##   State mean_revenue sd_revenue median_revenue iqr_revenue companies
##   <chr>        <dbl>      <dbl>          <dbl>       <dbl>     <int>
## 1 MI       61950794. 216337592.       15900000    29900000       126
## 2 MO       45164407.  87377234.       15000000    42800000        59
## 3 IL      121773993. 702149071.       14700000    29800000       273
## 4 UT       36038947.  60810771.       12700000    35350000        95
## 5 TX       57271835. 171003314.       12500000    29150000       387
## 6 TN       35870732.  56662556.       12350000    35250000        82
inc_state_median[which(inc_state_median$companies == max(inc_state_median$companies)), 'State']
## # A tibble: 1 × 1
##   State
##   <chr>
## 1 CA

California has the most companies on the list of the 5000 fastest growing companies.

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.

ggplot(inc, aes(y = State))+
  geom_bar(stat = "count",aes(fill = State))+
  ggtitle("Distribution of Fastest Growing Companies by State")+
  xlab("Count")+
  ylab("State")+
  theme(plot.title = element_text(hjust = 0.5))+
  scale_y_discrete(guide = guide_axis(n.dodge = 2))+
  theme(legend.position = "none")

ggplot(inc_state_median, aes(x = companies, y = reorder(State,companies)))+
  geom_bar(stat = "identity",aes(fill = State))+
  ggtitle("Distribution of Fastest Growing Companies by State")+
  xlab("Count")+
  ylab("State")+
  theme(plot.title = element_text(hjust = 0.5))+
  scale_y_discrete(guide = guide_axis(n.dodge = 2))+
  theme(legend.position = "none")

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.

Comment:

I checked the employee totals, and there were a few outliers above 500 employees that were excessive and would skew the axis, so I limited the y-axis to 500 employees.

inc_state <- inc_state[order(-inc_state$companies),]

inc_state[3,"State"]
## # A tibble: 1 × 1
##   State
##   <chr>
## 1 NY
ny <- subset(inc,State == "NY")

ny <- ny[complete.cases(ny), ]

ny_ind <- ny %>%
  group_by(Industry) %>% summarize(median_revenue = median(Revenue), median_employees = median(Employees), iqr_employees = IQR(Employees), mean_growth = mean(Growth_Rate))

ggplot(ny, aes(x = Industry, y = Employees))+
  geom_boxplot(outlier.color = "red")+
  ylim(0,500)+
  ggtitle("New York Median Employees by Industry")+
  xlab("Industry")+
  ylab("Employees")+
  theme(plot.title = element_text(hjust = 0.5))+
  scale_x_discrete(guide = guide_axis(n.dodge = 4))+
  theme(axis.text.x = element_text(size = 6))
## Warning: Removed 17 rows containing non-finite values (stat_boxplot).

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.

I will assume that the investor wants to look nationwide, and not just in NY.

inc$rev_emp <- inc$Revenue / inc$Employees

ggplot(inc, aes(x = Industry, y = rev_emp))+
  geom_boxplot(outlier.color = "red")+
  ylim(0,1000000)+
  ggtitle("Median Revenue Per Employee by Industry, Nationwide")+
  xlab("Industry")+
  ylab("Revenue Per Employee")+
  theme(plot.title = element_text(hjust = 0.5))+
  #scale_x_discrete(guide = guide_axis(n.dodge = 4))+
  theme(axis.text.x = element_text(size = 6, angle = 90))
## Warning: Removed 357 rows containing non-finite values (stat_boxplot).