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:
library(ggplot2)
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.5.3
## -- Attaching packages ---------------
## v tibble 3.0.1 v dplyr 0.8.5
## v tidyr 1.0.2 v stringr 1.4.0
## v readr 1.3.1 v forcats 0.4.0
## v purrr 0.3.4
## Warning: package 'tibble' was built under R version 3.5.3
## Warning: package 'tidyr' was built under R version 3.5.3
## Warning: package 'readr' was built under R version 3.5.2
## Warning: package 'purrr' was built under R version 3.5.3
## Warning: package 'dplyr' was built under R version 3.5.3
## Warning: package 'stringr' was built under R version 3.5.3
## Warning: package 'forcats' was built under R version 3.5.3
## -- Conflicts ------------------------
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
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
## 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
inc %>% group_by(State) %>% summarise(Count= n(),mean_rev = mean(Revenue), sd_rev = sd(Revenue), R_SD_ALL= sd(Revenue)/sd(inc$Revenue)) %>% arrange(.,desc(R_SD_ALL))
## Warning: `...` is not empty.
##
## We detected these problematic arguments:
## * `needs_dots`
##
## These dots only exist to allow future extensions and should be empty.
## Did you misspecify an argument?
## # A tibble: 52 x 5
## State Count mean_rev sd_rev R_SD_ALL
## <fct> <int> <dbl> <dbl> <dbl>
## 1 IL 273 121773993. 702149071. 2.92
## 2 ID 17 231523529. 686093884. 2.85
## 3 WI 79 92362025. 529764518. 2.20
## 4 IA 28 123142857. 525683101. 2.19
## 5 NY 311 58715113. 341922076. 1.42
## 6 NC 137 67580292. 312739773. 1.30
## 7 DC 43 76344186. 273303810. 1.14
## 8 OH 186 68745161. 261035147. 1.09
## 9 AK 2 171500000 235890822. 0.981
## 10 MN 88 57256818. 226032974. 0.940
## # ... with 42 more rows
In my data I could see that there are Eight sates that did better than the natioanl std deviation of the Revenue.It seem like IL is having more potential with less number of company they were able to produce such a Revenue. ## 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
inc %>%
group_by(State) %>%
summarise(Count= n()) %>%
ggplot(mapping = aes(x= State, y=Count)) +
geom_col()+
theme(axis.text.x = element_text(angle = 60, colour="gray",hjust = 1,size=rel(0.86)))
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
# Third most company in the dataset
inc %>%
group_by(State) %>%
summarise(Count= n()) %>%
arrange(.,desc(Count)) %>% .[3,]
## Warning: `...` is not empty.
##
## We detected these problematic arguments:
## * `needs_dots`
##
## These dots only exist to allow future extensions and should be empty.
## Did you misspecify an argument?
## # A tibble: 1 x 2
## State Count
## <fct> <int>
## 1 NY 311
# how many people are employed by companies in different industries.
inc %>% filter(.,State== "NY") %>%
plyr::ddply(.,'Industry',summarise,E_Count = sum(Employees))
## Industry E_Count
## 1 Advertising & Marketing 3331
## 2 Business Products & Services 38804
## 3 Computer Hardware 44
## 4 Construction 366
## 5 Consumer Products & Services 10647
## 6 Education 838
## 7 Energy 646
## 8 Engineering 214
## 9 Environmental Services 310
## 10 Financial Services 1876
## 11 Food & Beverage 688
## 12 Government Services 17
## 13 Health 1064
## 14 Human Resources 4813
## 15 Insurance 65
## 16 IT Services 8776
## 17 Logistics & Transportation 118
## 18 Manufacturing 953
## 19 Media 1188
## 20 Real Estate 73
## 21 Retail 347
## 22 Security 540
## 23 Software 3197
## 24 Telecommunications 1621
## 25 Travel & Hospitality 3834
# Create a plot that shows the average and/or median employment by industry for companies in this state
inc %>% filter(.,State== "NY") %>%
plyr::ddply(.,'Industry',summarise,E_Count = sum(Employees),Mean_Emp = mean(Employees), Med_Emp=median(Employees)) %>% gather(key="Type" , value = "Count_EMP","Mean_Emp","Med_Emp","E_Count") %>% .[which(.$Type %in% c("Mean_Emp","Med_Emp")),] %>%
ggplot(mapping = aes(y= Industry, x=Count_EMP,fill = Type)) +
geom_col()
#Taking care of outliers
inc %>% filter(.,State== "NY") %>%
plyr::ddply(.,'Industry',summarise,E_Count = sum(Employees),Mean_Emp = mean(Employees), Med_Emp=median(Employees)) %>% gather(key="Type" , value = "Count_EMP","Mean_Emp","Med_Emp","E_Count") %>% .[which(.$Type %in% c("Mean_Emp","Med_Emp")),] %>% .[-which(.$Count_EMP > 1000),] %>%
ggplot(mapping = aes(y= Industry, x=Count_EMP,fill = Type)) +
geom_col() +
labs(title = "Mean/Median of Employee Count in NY",
y = "Sector") +
facet_wrap(~Type)
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
SUM_NY_TOTL_REV <- inc %>% filter(.,State== "NY") %>% plyr::ddply(.,'Industry',summarise,
Rev_Per_Emp = sum(Revenue)/sum(Employees)) %>% .[,"Rev_Per_Emp"] %>% sum(.)
inc %>% filter(.,State== "NY") %>% plyr::ddply(.,'Industry',summarise,
Rev_Per_Emp = sum(Revenue)/sum(Employees),
p=( Rev_Per_Emp*100)/SUM_NY_TOTL_REV)%>%
ggplot(mapping = aes(y= Industry, x=(p))) +
geom_bar(stat = "identity") +
labs(title = "Revenue per Employee in NY (%) ", y = "Sector", x = "% of Revenue") + theme_minimal()
Here I have taken the sum of per employee revenue in NY state, then got the % of the same agaist total per person Employee Revenue. The above chart suggest that Enery and IT services have the most Revenue per employee.