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)
Let’s load the required libraries
library(psych)
library(ggplot2)
##
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
library(knitr)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
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:
describe(inc) %>% kable()
vars | n | mean | sd | median | trimmed | mad | min | max | range | skew | kurtosis | se | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Rank | 1 | 5001 | 2.501641e+03 | 1.443506e+03 | 2.502e+03 | 2.501731e+03 | 1.853250e+03 | 1.0e+00 | 5.0000e+03 | 4.9990e+03 | -0.0004896 | -1.200432 | 2.041222e+01 |
Name* | 2 | 5001 | 2.501000e+03 | 1.443809e+03 | 2.501e+03 | 2.501000e+03 | 1.853250e+03 | 1.0e+00 | 5.0010e+03 | 5.0000e+03 | 0.0000000 | -1.200720 | 2.041650e+01 |
Growth_Rate | 3 | 5001 | 4.611826e+00 | 1.412369e+01 | 1.420e+00 | 2.136756e+00 | 1.215732e+00 | 3.4e-01 | 4.2148e+02 | 4.2114e+02 | 12.5495059 | 242.336616 | 1.997192e-01 |
Revenue | 4 | 5001 | 4.822254e+07 | 2.405423e+08 | 1.090e+07 | 1.733497e+07 | 1.067472e+07 | 2.0e+06 | 1.0100e+10 | 1.0098e+10 | 22.1744453 | 722.656318 | 3.401441e+06 |
Industry* | 5 | 5001 | 1.210038e+01 | 7.328351e+00 | 1.300e+01 | 1.205499e+01 | 8.895600e+00 | 1.0e+00 | 2.5000e+01 | 2.4000e+01 | -0.1012333 | -1.184515 | 1.036282e-01 |
Employees | 6 | 4989 | 2.327180e+02 | 1.353128e+03 | 5.300e+01 | 8.177511e+01 | 5.337360e+01 | 1.0e+00 | 6.6803e+04 | 6.6802e+04 | 29.8104167 | 1268.671130 | 1.915720e+01 |
City* | 7 | 5001 | 7.320010e+02 | 4.411164e+02 | 7.610e+02 | 7.317383e+02 | 6.049008e+02 | 1.0e+00 | 1.5190e+03 | 1.5180e+03 | -0.0420954 | -1.264827 | 6.237704e+00 |
State* | 8 | 5001 | 2.480324e+01 | 1.563706e+01 | 2.300e+01 | 2.444364e+01 | 1.927380e+01 | 1.0e+00 | 5.2000e+01 | 5.1000e+01 | 0.1190507 | -1.461030 | 2.211193e-01 |
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.
# creating dataframe that holds frequencies of companies in each state through grouping by States
state <- inc %>%
group_by(State) %>%
summarise(companies_freq = n())
# Now let's create visualization to see the results
ggplot(state, aes(x=reorder(State, companies_freq), y=companies_freq))+ geom_bar(stat= "identity", fill="#03DAC5")+labs(title="Number of companies in each state", x="States", y="Number of companies")+coord_flip()+geom_text(aes(label=companies_freq), vjust=0.6, hjust=1.2, size=3, color="black")
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.
# Create dataframe to filter Industries in New York
newyork <- inc %>%
filter(State== "NY") %>%
filter(complete.cases(.)) %>%
group_by(State, Industry) %>%
summarise(avg= mean(Employees))
# Now let's dig into the New York what it has for us !
library(forcats)
ggplot(newyork, aes(x=fct_reorder(Industry, avg), y=avg))+geom_bar(stat="identity")+scale_y_continuous(trans="log2")+labs(title="Average Number of Employees in different industries in NY", y="Average Number of Employees by different industries")+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.
# Filter by Most Revenue per employee by industry
revenue.industry <- inc %>%
filter(State== "NY") %>%
group_by(Industry) %>%
summarise(avg_revenue = mean(Revenue))
# NOw since we have the filtered data, let's put it into graph to see visualization
ggplot(revenue.industry, aes(x=reorder(Industry, avg_revenue), y=avg_revenue))+geom_bar(stat="identity", fill="#03DAC5")+coord_flip()+labs(title="Average Revenue per Employee by industry in NY", x="Industry", y="Average Revenue per Employee")
According to visualizations, California has the most number of companies in United States. Since as per the requirement of question 2, we chose New York because it has the 3rd most companies in US. Business & consumer products and services are the leading industries in NY with huge number of employees while real estate and government services has the least number of employees. Finally to check the average revenue per employee by different industry in NY, we again created bar chart and found that consumer products & services has the highest average revenue per employee while IT services and business products & services are the next highest. Eduction and government services has the least average revenue per employee in NY.