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)
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:
# Insert your code here, create more chunks as necessary
numColumns <- inc %>%
select_if(is.numeric) %>%
colnames()
skews <- numColumns %>%
map(function(col) {
df <- inc[col] %>%
summarise(skew = case_when(
mean(inc[[col]], na.rm = T) > median(inc[[col]], na.rm = T) ~ 'right',
mean(inc[[col]], na.rm = T) < median(inc[[col]], na.rm = T) ~ 'left',
mean(inc[[col]], na.rm = T) == median(inc[[col]], na.rm = T) ~ 'none'
))
return(df)
})
names(skews) <- numColumns
print(skews)
## $Rank
## skew
## 1 left
##
## $Growth_Rate
## skew
## 1 right
##
## $Revenue
## skew
## 1 right
##
## $Employees
## skew
## 1 right
inc %>%
group_by(State) %>%
summarise(count = n()) %>%
arrange(desc(count))
Analyzing the positions of the median vs mean, we can determine that Rank is Skewed Left, while the other numeric columns (Growth_Rate, Revenue, and Employees) are skewed right. For the skewed right columns, it means that there is data present (high values/outliers) pulling the mean further positive away from zero. The opposite is true for Rank.
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.
# Answer Question 1 here
ggplot(inc, aes(x=State)) +
geom_bar() +
ggtitle("State Count Distribution") +
theme(axis.text.x = element_text(angle = 90))
California has the most representation in the dataset
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.
# Answer Question 2 here
inc %>%
filter(State == "NY") %>%
filter(complete.cases(.)) %>%
group_by(Industry) %>%
summarise(median = median(Employees),
mean = mean(Employees)) %>%
ggplot(aes(x = Industry, y = mean)) +
geom_point() +
geom_hline(yintercept = mean(inc[inc$State == "NY", "Employees"], na.rm = T)) +
coord_flip()
There are 4 industries where their average employment is above the average employee count in NY: Travel & Hospitality, Human Resources, Consumer Products & Services, and Business Products & Services
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.
# Answer Question 3 here
inc %>%
group_by(Industry) %>%
summarise(Revenue = sum(Revenue),
Employees = sum(Employees)) %>%
mutate(rev_per_employee = Revenue/Employees) %>%
arrange(desc(rev_per_employee))
Computer Hardware will make the most revenue per employee followed by Energy, Construction, Consumer Products & Services, and Insurance. As an investor, it would be most prodent to invest in Computer Hardware.