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 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:
# remove NA values
inc <- na.omit(inc)
# explore the data
inc %>%
dplyr::select(-Name, -Industry, -City, -State, -Rank) %>% #remove categorical and Rank variable
gather(key,value) %>%
ggplot(aes(value)) + geom_boxplot() + facet_wrap(~key, scales = "free")
# every numerical variable has many outliers, can we see why?
psych::describe(inc)
## vars n mean sd median trimmed
## Rank 1 4989 2501.39 1443.42 2.502e+03 2501.47
## Name* 2 4989 2495.00 1440.34 2.495e+03 2495.00
## Growth_Rate 3 4989 4.61 14.14 1.420e+00 2.14
## Revenue 4 4989 48253357.39 240819468.86 1.090e+07 17328099.17
## Industry* 5 4989 12.09 7.33 1.300e+01 12.05
## Employees 6 4989 232.72 1353.13 5.300e+01 81.78
## City* 7 4989 730.98 440.33 7.600e+02 730.70
## State* 8 4989 24.80 15.63 2.300e+01 24.44
## mad min max range skew kurtosis se
## Rank 1851.77 1.0e+00 5.0000e+03 4.9990e+03 0.00 -1.20 20.44
## Name* 1848.80 1.0e+00 4.9890e+03 4.9880e+03 0.00 -1.20 20.39
## Growth_Rate 1.22 3.4e-01 4.2148e+02 4.2114e+02 12.54 241.94 0.20
## Revenue 10674720.00 2.0e+06 1.0100e+10 1.0098e+10 22.15 721.05 3409454.05
## Industry* 8.90 1.0e+00 2.5000e+01 2.4000e+01 -0.10 -1.18 0.10
## Employees 53.37 1.0e+00 6.6803e+04 6.6802e+04 29.81 1268.67 19.16
## City* 603.42 1.0e+00 1.5170e+03 1.5160e+03 -0.04 -1.26 6.23
## State* 19.27 1.0e+00 5.2000e+01 5.1000e+01 0.12 -1.46 0.22
# there are considerable skews in the three numerical variables,
# as well as high kurtosis (i.e. number of outliers) in each variable
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.
# Answer Question 1 here
inc_State <- data.frame(inc) %>%
group_by(State) %>%
summarize(
state_count = n()
) %>%
arrange(desc(state_count))
ggplot(data=inc_State, aes(x=reorder(State, -state_count), y=state_count)) +
geom_col(fill="darkgrey") + coord_flip() +
labs(x="State", y="Count", title="Companies by State")
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.
# Answer Question 2 here
head(inc_State) # The state with the 3rd most companies is NY with 311
## # A tibble: 6 x 2
## State state_count
## <chr> <int>
## 1 CA 700
## 2 TX 386
## 3 NY 311
## 4 VA 283
## 5 FL 282
## 6 IL 272
# gather the data for NY only from the original data
inc_NY_detail <- data.frame(inc) %>%
filter(State=="NY", complete.cases(.))
inc_NY <- data.frame(inc) %>%
filter(State=="NY", complete.cases(.)) %>%
group_by(Industry) %>%
summarize(
tot_companies = n(),
tot_emp = sum(Employees),
avg_emp = mean(Employees),
median_emp = median(Employees),
min_emp = min(Employees),
max_emp = max(Employees)
) %>%
arrange(desc(tot_emp)) # sort in descending order by total workforce count in the
# industry (i.e. industries with largest total workforce on top)
inc_NY
## # A tibble: 25 x 7
## Industry tot_companies tot_emp avg_emp median_emp min_emp max_emp
## <chr> <int> <int> <dbl> <dbl> <int> <int>
## 1 Business Products &~ 26 38804 1492. 70.5 4 32000
## 2 Consumer Products &~ 17 10647 626. 25 5 10000
## 3 IT Services 43 8776 204. 54 8 3000
## 4 Human Resources 11 4813 438. 56 7 2081
## 5 Travel & Hospitality 7 3834 548. 61 6 2280
## 6 Advertising & Marke~ 57 3331 58.4 38 2 270
## 7 Software 13 3197 246. 80 15 1271
## 8 Financial Services 13 1876 144. 81 14 483
## 9 Telecommunications 17 1621 95.4 31 6 316
## 10 Media 11 1188 108 45 4 602
## # ... with 15 more rows
NY_plot <- ggplot(data=inc_NY_detail, aes(x=reorder(Industry,Employees,median), y=Employees)) +
geom_boxplot() + coord_flip() +
labs(x = "Industry", y = "Median # Employees", title="Median # Employees per Industry in NY State")
NY_plot + scale_y_log10() # scale the y axis (x flipped axis) for better presentation
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.
# Answer Question 3 here
inc_invest <- data.frame(inc) %>%
filter(complete.cases(.)) %>%
group_by(Industry) %>%
summarize(
tot_revenue = sum(Revenue),
tot_emp = sum(Employees)
) %>%
mutate(rev_per_emp = tot_revenue/tot_emp) %>%
arrange(desc(rev_per_emp))
inc_invest
## # A tibble: 25 x 4
## Industry tot_revenue tot_emp rev_per_emp
## <chr> <dbl> <int> <dbl>
## 1 Computer Hardware 11885700000 9714 1223564.
## 2 Energy 13771600000 26437 520921.
## 3 Construction 13174300000 29099 452741.
## 4 Logistics & Transportation 14837800000 39994 371001.
## 5 Consumer Products & Services 14956400000 45464 328972.
## 6 Insurance 2337900000 7339 318558.
## 7 Manufacturing 12603600000 43942 286824.
## 8 Retail 10257400000 37068 276718.
## 9 Financial Services 13150900000 47693 275741.
## 10 Environmental Services 2638800000 10155 259852.
## # ... with 15 more rows
Investor_plot <- ggplot(data=inc_invest, aes(x=reorder(Industry,rev_per_emp), y=rev_per_emp)) +
geom_bar(stat="identity", fill="darkgreen") + coord_flip() +
labs(x = "Industry", y = "Revenue per Employee",
title="Revenue per Employee by Industry") +
scale_y_continuous(limits=c(0,1450000)) +
geom_text(aes(label = round(rev_per_emp,0)), size=3, hjust=-0.25)
Investor_plot