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:
inc <- as.data.table(inc)
# list rows of data that have missing values
inc[!complete.cases(inc),]
## Rank Name Growth_Rate Revenue
## 1: 183 First Flight Solutions 22.32 2700000
## 2: 1064 Popchips 3.98 93300000
## 3: 1124 Vocalocity 3.72 42900000
## 4: 1653 Higher Logic 2.36 6000000
## 5: 1686 Global Communications Group 2.30 3600000
## 6: 2197 JeffreyM Consulting 1.68 12100000
## 7: 2743 Excalibur Exhibits 1.27 9900000
## 8: 3001 Heartland Business Systems 1.12 156300000
## 9: 3978 SSEC 0.68 80400000
## 10: 4112 Carolinas Home Medical Equipment 0.64 3300000
## 11: 4566 Oakbrook 0.48 8900000
## 12: 4968 Popcorn Palace 0.35 5500000
## Industry Employees City State
## 1: Logistics & Transportation NA Emerald Isle NC
## 2: Food & Beverage NA San Francisco CA
## 3: Telecommunications NA Atlanta GA
## 4: Software NA Washington DC
## 5: Telecommunications NA Englewood CO
## 6: Business Products & Services NA Bellevue WA
## 7: Business Products & Services NA houston TX
## 8: IT Services NA Little Chute WI
## 9: Manufacturing NA Horsham PA
## 10: Health NA Matthews NC
## 11: Real Estate NA Madison WI
## 12: Food & Beverage NA Schiller Park IL
new.inc <- na.omit(inc)
# growth rate summary by indsutry
head(arrange(new.inc[, .(mean_growth_rate = mean(Growth_Rate),
median_growth_rate = median(Growth_Rate),
min_growth_rate = min(Growth_Rate),
max_growth_rate = max(Growth_Rate)), by = .(Industry)], desc(mean_growth_rate)),10)
## Industry mean_growth_rate median_growth_rate
## 1 Energy 9.603303 2.080
## 2 Consumer Products & Services 8.776108 1.820
## 3 Real Estate 7.823158 2.080
## 4 Government Services 7.238168 2.110
## 5 Advertising & Marketing 6.225478 1.610
## 6 Retail 6.184729 1.760
## 7 Financial Services 5.435308 1.485
## 8 Software 5.028446 1.700
## 9 Health 4.868305 1.570
## 10 Media 4.374074 1.940
## min_growth_rate max_growth_rate
## 1 0.35 233.08
## 2 0.35 421.48
## 3 0.35 179.38
## 4 0.35 248.31
## 5 0.35 213.37
## 6 0.34 166.89
## 7 0.34 174.04
## 8 0.35 128.63
## 9 0.35 245.45
## 10 0.41 23.01
# revenue summary by industry
head(arrange(new.inc[, .(mean_rev = mean(Revenue),
median_rev = median(Revenue),
min_rev = min(Revenue),
max_rev = max(Revenue)), by = .(Industry)], desc(mean_rev)),10)
## Industry mean_rev median_rev min_rev max_rev
## 1 Computer Hardware 270129545 22350000 3800000 1.010e+10
## 2 Energy 126344954 29400000 2100000 1.900e+09
## 3 Food & Beverage 99321705 18600000 2000000 4.500e+09
## 4 Logistics & Transportation 96349351 20800000 2300000 1.900e+09
## 5 Consumer Products & Services 73676847 9400000 2000000 4.600e+09
## 6 Construction 70450802 14000000 2100000 4.700e+09
## 7 Telecommunications 57385039 16600000 2100000 8.462e+08
## 8 Business Products & Services 54887292 9750000 2000000 2.400e+09
## 9 Security 52230137 12300000 2000000 7.181e+08
## 10 Environmental Services 51741176 12500000 2100000 1.400e+09
# number of employees by industry
head(arrange(new.inc[, .(sum_employee = sum(Employees),
mean_employee = mean(Employees),
min_employee = min(Employees),
max_employee = max(Employees)), by =.(Industry)], desc(sum_employee)),10)
## Industry sum_employee mean_employee min_employee
## 1 Human Resources 226980 1158.0612 4
## 2 Business Products & Services 117357 244.4938 4
## 3 IT Services 102788 140.4208 2
## 4 Health 82430 232.8531 2
## 5 Food & Beverage 65911 510.9380 3
## 6 Software 51262 150.3284 1
## 7 Financial Services 47693 183.4346 5
## 8 Consumer Products & Services 45464 223.9606 1
## 9 Manufacturing 43942 172.3216 1
## 10 Security 41059 562.4521 7
## max_employee
## 1 66803
## 2 32000
## 3 7000
## 4 4390
## 5 7681
## 6 3000
## 7 1829
## 8 13200
## 9 8500
## 10 20000
# number of cities by industry
head(arrange(new.inc[, .(count = length(unique(City))), by =.(Industry)], desc(count)),10)
## Industry count
## 1 IT Services 388
## 2 Business Products & Services 292
## 3 Health 257
## 4 Advertising & Marketing 252
## 5 Manufacturing 223
## 6 Software 202
## 7 Financial Services 191
## 8 Retail 164
## 9 Construction 157
## 10 Consumer Products & Services 152
# number of state by industry
head(arrange(new.inc[, .(count = length(unique(State))), by =.(Industry)], desc(count)),10)
## Industry count
## 1 Health 44
## 2 Software 44
## 3 IT Services 44
## 4 Advertising & Marketing 43
## 5 Business Products & Services 42
## 6 Financial Services 39
## 7 Manufacturing 38
## 8 Retail 37
## 9 Construction 36
## 10 Consumer Products & Services 34
# number of cities by state
head(arrange(new.inc[, .(count = length(unique(City))), by =.(State)], desc(count)),10)
## State count
## 1 CA 204
## 2 IL 104
## 3 FL 102
## 4 NJ 97
## 5 NY 90
## 6 PA 80
## 7 OH 79
## 8 MA 72
## 9 TX 66
## 10 VA 56
# number of companies by industry
head(arrange(new.inc[, .(count = length(unique(Name))), by = .(Industry)], desc(count)),10)
## Industry count
## 1 IT Services 732
## 2 Business Products & Services 480
## 3 Advertising & Marketing 471
## 4 Health 354
## 5 Software 341
## 6 Financial Services 260
## 7 Manufacturing 255
## 8 Consumer Products & Services 203
## 9 Retail 203
## 10 Government Services 202
# number of companies by cities
head(arrange(new.inc[, .(count = length(unique(Name))), by = .(City)], desc(count)),10)
## City count
## 1 New York 160
## 2 Chicago 90
## 3 Austin 88
## 4 Houston 76
## 5 San Francisco 74
## 6 Atlanta 73
## 7 San Diego 67
## 8 Seattle 52
## 9 Boston 43
## 10 Denver 42
# number of companies by state
head(arrange(new.inc[, .(count = length(unique(Name))), by = .(State)], desc(count)),10)
## State count
## 1 CA 700
## 2 TX 386
## 3 NY 311
## 4 VA 283
## 5 FL 282
## 6 IL 272
## 7 GA 211
## 8 OH 186
## 9 MA 182
## 10 PA 163
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.
data.1 <- arrange(inc[, .(num_of_companies = length(Name)), by =.(State)], desc(num_of_companies))
plot1 <- ggplot(data.1,
aes(reorder(State, num_of_companies), num_of_companies)) +
geom_point(size=0.5) +
geom_segment(aes(x=State, xend=State, y=0, yend=num_of_companies)) +
geom_text(aes(label = paste0(num_of_companies, ",", State)),
color = "red", size = 2, hjust = -0.1)+
scale_y_continuous(breaks = seq(0,800,100),labels = comma) +
labs(title = "Number Of Companies By State",
x = "State",
y = "Number Of Companies") +
coord_flip() +
theme_bw()
plot1
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.
#head(arrange(new.inc[, .(count = length(unique(Name))), by = .(State)], desc(count)),3)
data.2 <- inc[complete.cases(inc),][State == 'NY']
upper <- max(data.2$Employees)
lower <- min(data.2$Employees)
median.label <- paste0("Median Number of Employees(NY): ", median(data.2$Employees))
plot2 <- ggplot(data.2,aes(reorder(Industry, Employees, FUN=median), Employees)) +
geom_boxplot(outlier.shape = NA) +
geom_hline(yintercept = median(data.2$Employees),
color="red",
linetype="dashed") +
scale_y_continuous(trans = log2_trans(), limits = c(lower, upper)) +
labs(title = "Number of Employees by Industry in the state of NY",
x = "Industry",
y = "Number of Employees, Log2 transform") +
geom_text(aes(x=1.5, label=median.label, y = 300),
size = 3,
colour="red") +
theme_bw() +
coord_flip()
plot2
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.
data.3 <- arrange(inc[complete.cases(inc),][, `:=`(RPE = Revenue/Employees)], desc(RPE))
upper <- max(data.3$RPE)
lower <- min(data.3$RPE)
median.label <- paste0("Median Revenue Per Employees: ", round(median(data.3$RPE),2))
plot3 <- ggplot(data.3, aes(reorder(Industry, RPE, FUN=median), RPE)) +
geom_boxplot() +
geom_hline(yintercept = median(data.3$RPE),
color="red",
linetype="dashed") +
scale_y_continuous(trans = log2_trans(), limits = c(lower, upper)) +
labs(title = "Revenue Per Employees Distribution Per Industry",
x = "Industry",
y = "Revenue Per Employees, Log2 transform") +
geom_text(aes(x=1.5, label=median.label, y = 4000000),
size = 3,
colour="red") +
theme_bw() +
coord_flip()
plot3