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
## 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
require(dplyr)
## Loading required package: 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
require(ggplot2)
## Loading required package: ggplot2
require(plyr)
## Loading required package: plyr
## -------------------------------------------------------------------------
## You have loaded plyr after dplyr - this is likely to cause problems.
## If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
## library(plyr); library(dplyr)
## -------------------------------------------------------------------------
##
## Attaching package: 'plyr'
## The following objects are masked from 'package:dplyr':
##
## arrange, count, desc, failwith, id, mutate, rename, summarise,
## summarize
require(grid)
## Loading required package: grid
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: #using glimpse of dplyr package to overview the data
glimpse(inc)
## Observations: 5,001
## Variables: 8
## $ Rank <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,...
## $ Name <fct> Fuhu, FederalConference.com, The HCI Group, Bridge...
## $ Growth_Rate <dbl> 421.48, 248.31, 245.45, 233.08, 213.37, 179.38, 17...
## $ Revenue <dbl> 1.179e+08, 4.960e+07, 2.550e+07, 1.900e+09, 8.700e...
## $ Industry <fct> Consumer Products & Services, Government Services,...
## $ Employees <int> 104, 51, 132, 50, 220, 63, 27, 75, 97, 15, 149, 16...
## $ City <fct> El Segundo, Dumfries, Jacksonville, Addison, Bosto...
## $ State <fct> CA, VA, FL, TX, MA, TX, TN, CA, UT, RI, VA, CA, FL...
dim(inc)
## [1] 5001 8
Num_of_State <- inc$State %>% unique() %>% length()
Num_of_State
## [1] 52
inc_by_state <- inc %>% group_by(State) %>%
summarise(byMean = mean(Revenue))
max(inc_by_state$byMean)
## [1] 48222535
So there are 52 states in total. maxium average revenue by states is 48222535
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.
inc_by_state <- ggplot(inc, aes(factor(State))) + geom_bar(fill="blue")
inc_by_state <- inc_by_state + coord_flip()
inc_by_state <- inc_by_state + theme(text = element_text(size=10), axis.title=element_text(size=12))
inc_by_state <- inc_by_state + labs(title = "Number of Companies by State", x= "State", y= "Count")
inc_by_state <- inc_by_state + theme(plot.title = element_text(size=14))
inc_by_state
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.
counts <- as.data.frame(table(inc$State))
colnames(counts) <- c("State", "Count")
third <- sort(counts$Count, TRUE)[3]
filter(counts, Count == third)
## State Count
## 1 NY 311
ny_inc <- filter(inc, State == "NY")
ny_inc <- ny_inc[complete.cases(ny_inc),]
p <- ggplot(ny_inc) + geom_bar(aes(Industry, Employees, fill = Industry), position = "dodge", stat = "summary", fun.y = "mean", fill="blue")
p <- p + coord_flip()
p <- p + theme(legend.position="none")
p <- p + theme(text = element_text(size=10), axis.title=element_text(size=12,face="bold"))
p <- p + labs(title = "Average Number of Employees by Industry in NY", x= "Industry", y= "Average Number of Employees")
p <- p + theme(plot.title = element_text(size=14))
p
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.
inc_rev <- inc[complete.cases(inc),]
inc_rev <- inc_rev %>% mutate(rev_per_emp = Revenue / Employees)
p1 <- ggplot(inc_rev) + geom_bar(aes(Industry, rev_per_emp, fill = Industry), position = "dodge", stat = "summary", fun.y = "mean", fill="blue")
p1 <- p1 + coord_flip()
p1 <- p1 + theme(legend.position="none")
p1 <- p1 + theme(text = element_text(size=10), axis.title=element_text(size=12,face="bold"))
p1 <- p1 + labs(title = "Average Revenue per Employees by Industry", x= "Industry", y= "Average Revenue per Employees")
p1 <- p1 + theme(plot.title = element_text(size=14))
p1