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:
# load libraries
library(dplyr)
library(ggplot2)
library(scales)
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 Revenue
Min. : 1 (Add)ventures : 1 Min. : 0.340 Min. :2.000e+06
1st Qu.:1252 @Properties : 1 1st Qu.: 0.770 1st Qu.:5.100e+06
Median :2502 1-Stop Translation USA: 1 Median : 1.420 Median :1.090e+07
Mean :2502 110 Consulting : 1 Mean : 4.612 Mean :4.822e+07
3rd Qu.:3751 11thStreetCoffee.com : 1 3rd Qu.: 3.290 3rd Qu.:2.860e+07
Max. :5000 123 Exteriors : 1 Max. :421.480 Max. :1.010e+10
(Other) :4995
Industry Employees City State
IT Services : 733 Min. : 1.0 New York : 160 CA : 701
Business Products & Services: 482 1st Qu.: 25.0 Chicago : 90 TX : 387
Advertising & Marketing : 471 Median : 53.0 Austin : 88 NY : 311
Health : 355 Mean : 232.7 Houston : 76 VA : 283
Software : 342 3rd Qu.: 132.0 San Francisco: 75 FL : 282
Financial Services : 260 Max. :66803.0 Atlanta : 74 IL : 273
(Other) :2358 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
# look at the variable types
str(inc)
'data.frame': 5001 obs. of 8 variables:
$ Rank : int 1 2 3 4 5 6 7 8 9 10 ...
$ Name : Factor w/ 5001 levels "(Add)ventures",..: 1770 1633 4423 690 1198 2839 4733 1468 1869 4968 ...
$ Growth_Rate: num 421 248 245 233 213 ...
$ Revenue : num 1.18e+08 4.96e+07 2.55e+07 1.90e+09 8.70e+07 ...
$ Industry : Factor w/ 25 levels "Advertising & Marketing",..: 5 12 13 7 1 20 10 1 5 21 ...
$ Employees : int 104 51 132 50 220 63 27 75 97 15 ...
$ City : Factor w/ 1519 levels "Acton","Addison",..: 391 365 635 2 139 66 912 1179 131 1418 ...
$ State : Factor w/ 52 levels "AK","AL","AR",..: 5 47 10 45 20 45 44 5 46 41 ...
#look at the first few rows of data
head(inc)
# summarize raw data
state_summary <- inc %>%
group_by(State) %>%
tally()
state_summary$Corp_count <- state_summary$n
state_revenues <- inc %>% group_by(State) %>% dplyr::summarize(Total_Revenue = sum(Revenue,na.rm = TRUE))
state_employees <- inc %>% group_by(State) %>% dplyr::summarize(Total_Employees = sum(Employees,na.rm = TRUE))
state_summary <- merge(state_summary,state_revenues)
state_summary <- merge(state_summary,state_employees)
state_summary <- state_summary %>% mutate(Revenue_Per_Emp = Total_Revenue/Total_Employees)
state_summary$n <- NULL
state_summary <- state_summary %>%
arrange(desc(Corp_count))
print(state_summary)
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.
df <- inc %>%
group_by(State) %>%
summarise(bizCount = n()) %>% # States act as a proxy for unique business
arrange(desc(bizCount))
library(ggthemes)
ggplot(data = df, aes(x = reorder(State, bizCount), y = bizCount)) +
theme_tufte() +
geom_bar(fill="gray38", stat = "identity",width = 0.8,) + theme(axis.title=element_blank(),text = element_text(size=7)) +
geom_hline(yintercept=seq(1, 800, 100), col="white", lwd=1) +
coord_flip() +
labs(title = 'Number of Companies by State') +
xlab('State') +
ylab('Number of 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.
##Looking at the Distributions by Industry Using this we can see some different outliers that are out such in the Business and Product Services Industry there is an organization that has over 30000 employees
inc <- inc[complete.cases(inc),]
ny = inc %>%
filter(State == "NY")
g <- ggplot(ny, aes(reorder(Industry,Employees,mean), Employees))
g <- g + geom_boxplot() + coord_flip() + labs(x = "Industry", y = "Employees") + ggtitle("Employee Size Distribution of Different Industries")
g
g <- ny %>% filter(Employees < 5000) %>% ggplot(aes(reorder(Industry,Employees,mean), Employees))
g <- g + geom_boxplot() + coord_flip() + labs(x = "Industry", y = "Employees")+ggtitle("Employee Distribution of Companies under 5000 Employees")
g
Here I plot the means and the medians by each industry so we can get a better sense of how big the average company of a specific industry is.
df_ny <- inc %>%
filter(State == "NY") %>%
filter(complete.cases(.)) %>%
group_by(Industry) %>%
summarise(Mean = mean(Employees),
Median = median(Employees)) %>%
gather(Statistic, Amount, Mean, Median)
ggplot(data = df_ny, aes(x= reorder(Industry,-Amount),Amount)) +
geom_bar(stat = 'identity', aes(fill = Statistic), position = 'dodge') +
scale_fill_manual(values = c('grey80', 'grey33'))+
geom_hline(yintercept=seq(1, 1500, 100), col="white", lwd=0.5) +
theme_tufte() +
coord_flip()+ggtitle("State of New York Employee counts by Industry") +
xlab("Industry") + ylab("Employees Per Company")
NA
NA
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
Rev_By_Emp <- inc %>%
filter(State == "NY") %>%
filter(complete.cases(.)) %>%
mutate(RevPerEmp = (Revenue / Employees)/1000) %>%
group_by(Industry) %>%
summarise(Mean = mean(RevPerEmp))
ggplot(data = Rev_By_Emp, aes(x= reorder(Industry,-Mean),Mean)) +
geom_bar(stat = 'identity') +
theme_tufte()+
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
geom_hline(yintercept=seq(1, 9000, 1000), col="white", lwd=0.5) +
ylab('Revenue/Employee in thousands $')+ggtitle("State of New York Revenue Per Employee by Industry") +
xlab("Industry") + ylab("Revenue Per Employee")
Rev_By_Emp %>% filter(Industry != "Energy") %>% ggplot(aes(x= reorder(Industry,-Mean),Mean)) +
geom_bar(stat = 'identity') +
theme_tufte()+
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
geom_hline(yintercept=seq(1, 1000, 100), col="white", lwd=0.5) +
ylab('Revenue/Employee in thousands $')+ggtitle("State of New York Revenue Per Employee by Industry excluding Energy") +
xlab("Industry") + ylab("Revenue Per Employee")
NA
NA
NA