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:
Below, we can see that NY has the highest number of Employees, followed by DE, FL, MD so on and so forth:
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.2.1 ✔ purrr 0.3.3
## ✔ tibble 2.1.3 ✔ dplyr 0.8.3
## ✔ tidyr 1.0.0 ✔ stringr 1.4.0
## ✔ readr 1.3.1 ✔ forcats 0.4.0
## ── Conflicts ────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
employ_state <- inc %>% group_by(State) %>% summarise(total_emply = sum(Employees)) %>% arrange(desc(total_emply))
head(employ_state)
## # A tibble: 6 x 2
## State total_emply
## <fct> <int>
## 1 NY 84370
## 2 DE 68544
## 3 FL 61221
## 4 MD 40439
## 5 OH 38002
## 6 MI 36905
And the Industry with the highest revenue comes from “Business Products & Services” followed by “IT Services”, “Health” and “Consumer Products & Services”:
revenue_indust <- inc %>% group_by(Industry) %>% summarise(total_rev = sum(Revenue)) %>% arrange(desc(total_rev))
head(revenue_indust)
## # A tibble: 6 x 2
## Industry total_rev
## <fct> <dbl>
## 1 Business Products & Services 26367900000
## 2 IT Services 20681300000
## 3 Health 17863400000
## 4 Consumer Products & Services 14956400000
## 5 Logistics & Transportation 14840500000
## 6 Energy 13771600000
Lastly, we can observe that the industry with the highest average growth rate is “Energy”, followed by “Consumer Products & Services”, “Real Estate” and “Government Services” to name a few:
growth_indust <- inc %>% group_by(Industry) %>% summarise(avg_growth = mean(Growth_Rate)) %>% arrange(desc(avg_growth))
head(growth_indust)
## # A tibble: 6 x 2
## Industry avg_growth
## <fct> <dbl>
## 1 Energy 9.60
## 2 Consumer Products & Services 8.78
## 3 Real Estate 7.75
## 4 Government Services 7.24
## 5 Advertising & Marketing 6.23
## 6 Retail 6.18
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.
comp_state <- inc %>% group_by(State) %>% summarise(Count = n()) %>% arrange(desc(Count))
ggplot(comp_state, aes(x=reorder(State, -Count), Count)) + geom_bar(stat="identity", width = 0.5, fill = "tomato2") + labs(x = "State", y = "Number of Companies", title = "Number of Fast-Growing Companies per State\n") + theme(axis.text.x = element_text(hjust = 1, size=10)) + theme(axis.text.y = element_text(hjust = 1, size=5)) + geom_label(aes(label=comp_state$Count), position = position_dodge(width = 0.1), size = 1.8, label.padding = unit(0.1, "lines"), label.size = 0.07, inherit.aes = TRUE) + coord_flip()
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.
third_most_comp <- inc %>% filter(complete.cases(.)) %>% group_by(State) %>% mutate(companies = n()) %>% arrange(desc(companies)) %>% ungroup %>% mutate(y = dense_rank(desc(companies))) %>% filter(y == 3) %>% group_by(Industry)
head(third_most_comp)
## # A tibble: 6 x 10
## # Groups: Industry [3]
## Rank Name Growth_Rate Revenue Industry Employees City State companies y
## <int> <fct> <dbl> <dbl> <fct> <int> <fct> <fct> <int> <int>
## 1 26 Been… 84.4 1.37e7 Consume… 17 New … NY 311 3
## 2 30 Sail… 73.2 8.10e6 Adverti… 79 New … NY 311 3
## 3 37 Yell… 67.4 1.80e7 Adverti… 27 New … NY 311 3
## 4 38 Cond… 67.0 7.10e6 Adverti… 89 New … NY 311 3
## 5 48 Cini… 53.6 5.90e6 Financi… 32 Rock… NY 311 3
## 6 70 33Ac… 45.0 2.79e7 Adverti… 75 New … NY 311 3
ggplot(third_most_comp, aes(x=Industry, y=Employees)) + geom_boxplot(outlier.shape = NA, fill="tomato2") + scale_y_continuous(limits = quantile(third_most_comp$Employees, c(0.1,0.5))) + coord_flip()
## Warning: Removed 184 rows containing non-finite values (stat_boxplot).
This second way of looking at the requested data does not deal with outliers and may misrepresent the information we’re looking for:
third_avg_employ <- third_most_comp %>% group_by(Industry) %>% summarise(avg_empl = round(mean(Employees))) %>% arrange(desc(avg_empl))
head(third_avg_employ)
## # A tibble: 6 x 2
## Industry avg_empl
## <fct> <dbl>
## 1 Business Products & Services 1492
## 2 Consumer Products & Services 626
## 3 Travel & Hospitality 548
## 4 Human Resources 438
## 5 Software 246
## 6 IT Services 204
ggplot(third_avg_employ, aes(x=reorder(Industry, -avg_empl), avg_empl)) + geom_bar(stat="identity", width = 0.5, fill = "tomato2") + labs(x = "Companies", y = "Average Employees", title = "Average Employees by Company in NY\n") + theme(axis.text.x = element_text(hjust = 1, size=10)) + theme(axis.text.y = element_text(hjust = 1, size=5)) + geom_label(aes(label=third_avg_employ$avg_empl), position = position_dodge(width = 0.1), size = 1.8, label.padding = unit(0.1, "lines"), label.size = 0.07, inherit.aes = TRUE) + coord_flip()
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.
rev_employ_indust <- inc %>% filter(complete.cases(.)) %>% mutate(rev_empl = Revenue/Employees) %>% group_by(Industry) %>% summarise(med_rev_empl = round(median(rev_empl))) %>% arrange(desc(med_rev_empl))
head(rev_employ_indust)
## # A tibble: 6 x 2
## Industry med_rev_empl
## <fct> <dbl>
## 1 Computer Hardware 516477
## 2 Logistics & Transportation 425024
## 3 Consumer Products & Services 313043
## 4 Retail 312755
## 5 Telecommunications 284000
## 6 Energy 283212
ggplot(rev_employ_indust, aes(x=reorder(Industry, med_rev_empl), med_rev_empl)) + geom_bar(stat="identity", width = 0.5, fill = "tomato2") + labs(x = "Industry", y = "Median Revenue", title = "Median Revenue Per Employee by Industry\n") + theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 8)) + theme(axis.text.y = element_text(hjust = 1, size=7)) + geom_label(aes(label=rev_employ_indust$med_rev_empl), position = position_dodge(width = 0.1), size = 1.8, label.padding = unit(0.1, "lines"), label.size = 0.07, inherit.aes = TRUE) + coord_flip()