Libraries Used
require(dplyr)
require(tidyr)
require(knitr)
require(kableExtra)
require(kable)
require(ggplot2)
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:
Average Revenue By Industry
If we were to break down the amount of revenue per industry, we can see the following:
inc %>%
group_by(Industry) %>%
summarise(Mean_Revenue = mean(Revenue), Number_of_Companies = n()) %>%
arrange(desc(Mean_Revenue)) %>%
kable('html') %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"))
| Industry | Mean_Revenue | Number_of_Companies |
|---|---|---|
| Computer Hardware | 270129545 | 44 |
| Energy | 126344954 | 109 |
| Food & Beverage | 98559542 | 131 |
| Logistics & Transportation | 95745161 | 155 |
| Consumer Products & Services | 73676847 | 203 |
| Construction | 70450802 | 187 |
| Telecommunications | 56855814 | 129 |
| Business Products & Services | 54705187 | 482 |
| Security | 52230137 | 73 |
| Environmental Services | 51741176 | 51 |
| Financial Services | 50580385 | 260 |
| Retail | 50529064 | 203 |
| Health | 50319437 | 355 |
| Manufacturing | 49546875 | 256 |
| Travel & Hospitality | 47283871 | 62 |
| Human Resources | 47173980 | 196 |
| Insurance | 46758000 | 50 |
| Engineering | 34222973 | 74 |
| Media | 32266667 | 54 |
| Real Estate | 30892708 | 96 |
| Government Services | 29748020 | 202 |
| IT Services | 28214598 | 733 |
| Software | 23802924 | 342 |
| Advertising & Marketing | 16528662 | 471 |
| Education | 13726506 | 83 |
As we can see, the Computer Hardware industry generates the most revenue out of the 25 industries in this dataset.
Top 5 Companies with Highest/Lowest Growth Rate
We can also take a look at the companies that had the highest and lowest growth rates. Below, are the top 5 companies with the highest growth rates:
inc %>%
arrange(desc(Growth_Rate)) %>%
head(n = 5L) %>%
kable('html') %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"))
| Rank | Name | Growth_Rate | Revenue | Industry | Employees | City | State |
|---|---|---|---|---|---|---|---|
| 1 | Fuhu | 421.48 | 1.179e+08 | Consumer Products & Services | 104 | El Segundo | CA |
| 2 | FederalConference.com | 248.31 | 4.960e+07 | Government Services | 51 | Dumfries | VA |
| 3 | The HCI Group | 245.45 | 2.550e+07 | Health | 132 | Jacksonville | FL |
| 4 | Bridger | 233.08 | 1.900e+09 | Energy | 50 | Addison | TX |
| 5 | DataXu | 213.37 | 8.700e+07 | Advertising & Marketing | 220 | Boston | MA |
As we can see, it looks like Fuhu, based in El Segundo, CA had an extremely high growth rate (almost double the next highest company on the list).
And here are the 5 companies with the lowest growth rates:
inc %>%
arrange(desc(Growth_Rate)) %>%
tail(n = 5L) %>%
kable('html') %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"))
| Rank | Name | Growth_Rate | Revenue | Industry | Employees | City | State | |
|---|---|---|---|---|---|---|---|---|
| 4997 | 4997 | Dot Foods | 0.34 | 4.50e+09 | Food & Beverage | 3919 | Mt. Sterling | IL |
| 4998 | 4998 | Lethal Performance | 0.34 | 6.80e+06 | Retail | 8 | Wellington | FL |
| 4999 | 4999 | ArcaTech Systems | 0.34 | 3.26e+07 | Financial Services | 63 | Mebane | NC |
| 5000 | 5000 | INE | 0.34 | 6.80e+06 | IT Services | 35 | Bellevue | WA |
| 5001 | 5000 | ALL4 | 0.34 | 4.70e+06 | Environmental Services | 34 | Kimberton | PA |
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.
f1 <- inc%>%
group_by(State)%>%
count(State)%>%
arrange(desc(n))%>%
as_tibble(f1)
ggplot(f1, aes(x=reorder(State,n), y=n))+
geom_bar(stat="identity", width=.8)+
theme(axis.title=element_blank())+
geom_hline(yintercept=seq(1,800,100), col="white", lwd=1)+
theme(panel.grid.major.y = element_blank())+
theme_classic()+
coord_flip()+
xlab("State")+
ylab("Number of Fastest Growing Companies")
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.
First, we’ll have to isolate the state with the 3rd most companies in the dataset. This appears to be the state of New York. In order to do this, we can use the filter function in R:
#let's find the state
f1[3,"State"]
## # A tibble: 1 x 1
## State
## <chr>
## 1 NY
ny_st<- filter(inc, State == "NY")
f2 <- ny_st %>%
filter(complete.cases(.))%>%
select(Industry, Employees)
ggplot(f2, aes(x=Employees, y=reorder(Industry, Employees, median, order = TRUE)))+
geom_boxplot(fill="slateblue", alpha=0.2)+
scale_x_log10()+
theme_classic()+
ylab("Industry (by median)")+
ggtitle("NY Median by Industry")
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.
Similar to our work in the exploratory analysis section above, I’ll use the group_by and summarise functions again to group the data based on Industry, and calculate the mean revenue per employee for each industry in the dataset:
f3 <- ny_st%>%
filter(complete.cases(.))%>%
group_by(Industry)%>%
summarise(Revenue_total = sum(Revenue), Employees_Total= sum(Employees))%>%
mutate(Revenue_per_employee = Revenue_total/Employees_Total)
ggplot(f3, aes(x=reorder(Industry, Revenue_per_employee), y=Revenue_per_employee))+
geom_bar(stat = "identity")+
geom_hline(yintercept=seq(1,700000,100000), col="white", lwd=1)+
theme_classic()+
coord_flip()+
xlab("Industry")+
ggtitle("NY Industry Revenue per Employee")