suppressPackageStartupMessages(library(dplyr))
## Warning: package 'dplyr' was built under R version 3.5.2
suppressPackageStartupMessages(library(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 117900000
## 2 2 FederalConference.com 248.31 49600000
## 3 3 The HCI Group 245.45 25500000
## 4 4 Bridger 233.08 1900000000
## 5 5 DataXu 213.37 87000000
## 6 6 MileStone Community Builders 179.38 45700000
## 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
## Min. : 2000000 IT Services : 733
## 1st Qu.: 5100000 Business Products & Services: 482
## Median : 10900000 Advertising & Marketing : 471
## Mean : 48222535 Health : 355
## 3rd Qu.: 28600000 Software : 342
## Max. :10100000000 Financial Services : 260
## (Other) :2358
## Employees City State
## Min. : 1.0 New York : 160 CA : 701
## 1st Qu.: 25.0 Chicago : 90 TX : 387
## Median : 53.0 Austin : 88 NY : 311
## Mean : 232.7 Houston : 76 VA : 283
## 3rd Qu.: 132.0 San Francisco: 75 FL : 282
## Max. :66803.0 Atlanta : 74 IL : 273
## NA's :12 (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
# Identify the top 10 Idustries with the most cases in the dataset
ind_by_count <- inc %>% count(Industry)
arrange(ind_by_count, desc(n)) %>% top_n(10)
## Selecting by n
## # A tibble: 10 x 2
## Industry n
## <fct> <int>
## 1 IT Services 733
## 2 Business Products & Services 482
## 3 Advertising & Marketing 471
## 4 Health 355
## 5 Software 342
## 6 Financial Services 260
## 7 Manufacturing 256
## 8 Consumer Products & Services 203
## 9 Retail 203
## 10 Government Services 202
# Identify the Idustries with the highest revenues
ind_by_rev <- inc %>% group_by(Industry) %>% summarise(mean_rev = mean(Revenue))
arrange(ind_by_rev, desc(mean_rev)) %>% top_n(10)
## Selecting by mean_rev
## # A tibble: 10 x 2
## Industry mean_rev
## <fct> <dbl>
## 1 Computer Hardware 270129545.
## 2 Energy 126344954.
## 3 Food & Beverage 98559542.
## 4 Logistics & Transportation 95745161.
## 5 Consumer Products & Services 73676847.
## 6 Construction 70450802.
## 7 Telecommunications 56855814.
## 8 Business Products & Services 54705187.
## 9 Security 52230137.
## 10 Environmental Services 51741176.
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
# Get a list of counts by state
count_by_state <- group_by(inc, State) %>%
summarize(Count=n())
# Plot results
ggplot(count_by_state, aes(x=reorder(State,Count),Count))+
geom_bar(stat="identity", fill="DarkRed")+
geom_text(aes(label=round(Count, digits=2)), vjust=0.2, size=2, position=position_dodge(width = 1), hjust=1)+
theme_minimal()+
theme(axis.text.x=element_text(size=6, vjust=0.5))+
theme(axis.text.y=element_text(size=6, vjust=0.5))+
labs( x="State", y="No of Companies")+
coord_flip()+
ggtitle("Companies by State")
NY is the state with the 3rd most companies in the data set
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
# NY Employee count by Industry
nyEmp_by_ind <- filter(inc, State=="NY") %>%
select(Industry, Name, Employees)
# Graph
nyEmp <- group_by(nyEmp_by_ind, Industry) %>% summarize(m = mean(Employees), max= max(Employees), min = min(Employees)) %>%
na.omit()
upper <- nyEmp$max
lower <- nyEmp$min
ggplot(nyEmp, aes(x = Industry, y =m, ymax=max, ymin = min, lower = lower, upper= upper)) + geom_boxplot(outlier.shape = NA) + coord_flip()+
labs(title="NY Employees By Industry", y = "Mean")
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
# Get revenue per Employee Data
rev_per_emp <- select(inc, Industry, Revenue, Employees) %>%
na.omit() %>% group_by(Industry) %>%
summarise(TotalRev = sum(Revenue), TotalEmp = sum(Employees)) %>%
mutate(RevEmployee = TotalRev / TotalEmp)
# Graph
ggplot(data = rev_per_emp, aes(x = reorder(Industry, RevEmployee), y = RevEmployee)) +
geom_bar(stat="identity", fill="#924444") +
geom_text(data = filter(rev_per_emp, RevEmployee>150000),
aes(x = Industry, y = RevEmployee, label=scales::dollar_format()(RevEmployee)),
hjust=1.1, vjust=0.4, color="#FFFFFF") +
geom_text(data = filter(rev_per_emp, RevEmployee<150000),
aes(x = Industry, y = RevEmployee, label=scales::dollar_format()(RevEmployee)),
hjust=-0.1, vjust=0.4, color="#cccccc") +
coord_flip() +
ggtitle("Revenue per Employee per Industry") + labs(x = "", y = "") +
theme(panel.background = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_text(size = 10, margin = margin(r=-20)))