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:
# number of rows and columns
dim(inc)
## [1] 5001 8
# The length suggests duplicate rank. Companies tied to same rank
duplicated.rows <- inc[ which(inc$Rank %in% x[duplicated(x)]),]
head(duplicated.rows)
## Rank Name Growth_Rate Revenue
## 3423 3424 Stemp Systems Group 19.37 6800000
## 3424 3424 Total Beverage Solution 0.90 41500000
## 5000 5000 INE 0.34 6800000
## 5001 5000 ALL4 0.34 4700000
## Industry Employees City State
## 3423 IT Services 39 Long Island City NY
## 3424 Food & Beverage 35 Mt. Pleasant SC
## 5000 IT Services 35 Bellevue WA
## 5001 Environmental Services 34 Kimberton PA
# Average growth per industry
aggregate(Growth_Rate ~ Industry, inc, mean)
## Industry Growth_Rate
## 1 Advertising & Marketing 6.225478
## 2 Business Products & Services 3.518485
## 3 Computer Hardware 4.089773
## 4 Construction 3.366684
## 5 Consumer Products & Services 8.776108
## 6 Education 3.642651
## 7 Energy 9.603303
## 8 Engineering 1.984324
## 9 Environmental Services 2.068039
## 10 Financial Services 5.435308
## 11 Food & Beverage 3.636565
## 12 Government Services 7.238168
## 13 Health 4.856394
## 14 Human Resources 3.300459
## 15 Insurance 2.008400
## 16 IT Services 3.331814
## 17 Logistics & Transportation 4.339226
## 18 Manufacturing 2.295391
## 19 Media 4.374074
## 20 Real Estate 7.746667
## 21 Retail 6.184729
## 22 Security 3.388904
## 23 Software 5.020643
## 24 Telecommunications 2.883721
## 25 Travel & Hospitality 2.353065
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.
data1 <- inc%>%
group_by(State)%>%
summarise(CompCount = n())%>%
arrange(desc(CompCount))
ggplot(data = data1,aes(x=reorder(State, CompCount),y=CompCount)) +
geom_bar(stat="identity") +
coord_flip() +
labs(title="Distribution of Companies by State", x="State", y="Companies Count")
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.
# base dataframe for the 3rd company
State3 <- inc%>%
filter(complete.cases(inc))%>%
filter(State == as.character(data1$State[3]))%>%
select(Name, Industry, Employees)
# base data by industry
State3_industry <- State3%>%
group_by(Industry)%>%
summarise(min_value = min(Employees) # for range, lowest value
,max_value = max(Employees) # for range, highest value
,ave_value = mean(Employees)
,sd_value = if_else(is.na(sd(Employees)),0,sd(Employees)))%>%
mutate(lowerbound = ave_value - (2*sd_value) # 2 sd left of mean
,upperbound = ave_value + (2*sd_value) # 2 sd right of mean
,Industry_w_range = paste(Industry, "(", min_value, "-",max_value,")",sep=" "))
# base data by employers
State3_employers <- State3%>%
inner_join(State3_industry, by = "Industry")%>%
mutate(Include = Employees >= lowerbound & Employees <= upperbound)%>%
filter(Include == "TRUE")%>%
group_by(Industry_w_range)%>%
summarise(EmpAve = mean(Employees))%>%
arrange(desc(EmpAve))
ggplot(data = State3_employers,aes(x=reorder(Industry_w_range, EmpAve),y=EmpAve)) +
geom_bar(stat="identity") +
coord_flip() +
labs(title="Average Employment By Industry In New York", x="Industry and Range of Number of Employees", y="Average Employees")
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.
state3_revenue <- inc%>%
filter(complete.cases(inc))%>%
filter(State == as.character(data1$State[3]))%>%
group_by(Industry)%>%
summarise(RevPerEmp = sum(Revenue) / sum(Employees))%>%
arrange(desc(RevPerEmp))
# Third company
ggplot(data = state3_revenue,aes(x=reorder(Industry, RevPerEmp),y=RevPerEmp)) +
geom_bar(stat="identity") +
coord_flip() +
labs(title="Revenue Per Employees in New York", x="Industry", y="Revenue Per Employees")
# OVerall
data3_revenue <- inc%>%
filter(complete.cases(inc))%>%
group_by(Industry)%>%
summarise(RevPerEmp = sum(Revenue) / sum(Employees))%>%
arrange(desc(RevPerEmp))
ggplot(data = data3_revenue,aes(x=reorder(Industry, RevPerEmp),y=RevPerEmp)) +
geom_bar(stat="identity") +
coord_flip() +
labs(title="Revenue Per Employees Overall", x="Industry", y="Revenue Per Employees")