R Markdown

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:

libr <- c("stringr","knitr","dplyr","ggplot2","reshape","DT","tidyr")
lapply (libr,require,character.only=TRUE)
## Loading required package: stringr
## Loading required package: knitr
## 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
## Loading required package: ggplot2
## Loading required package: reshape
## 
## Attaching package: 'reshape'
## The following object is masked from 'package:dplyr':
## 
##     rename
## Loading required package: DT
## Loading required package: tidyr
## 
## Attaching package: 'tidyr'
## The following objects are masked from 'package:reshape':
## 
##     expand, smiths
## [[1]]
## [1] TRUE
## 
## [[2]]
## [1] TRUE
## 
## [[3]]
## [1] TRUE
## 
## [[4]]
## [1] TRUE
## 
## [[5]]
## [1] TRUE
## 
## [[6]]
## [1] TRUE
## 
## [[7]]
## [1] TRUE
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:

This dataset has Rank, Name of company, Growth_Rate, Revenue , Industry type, count of Employees, City and State. There are 5000 records in the data set with 25 Industry type and 52 states(DC, PR are Some new State code.)

# Insert your code here, create more chunks as necessary
# Statewise count of Industries
df1 <- data.frame(inc)
df1["Count"] <-1
df2 <- aggregate(df1[c("Count")], by=list(Industry=df1$Industry,State=df1$State ), FUN=sum, na.rm=TRUE,order(1))
df2 <- df2[order(df2[1],decreasing=FALSE),]

# Statewise count of Industries
datatable(df2, options = list( pageLength = 5, lengthMenu = c(5, 10, 40),   initComplete = JS(
    "function(settings, json) {",
    "$(this.api().table().header()).css({'background-color': '#01975b', 'color': '#fff'});",
    "}")), rownames=TRUE)
# Industry wise count in each State
df3 <- data.frame(inc)
df3["Count"] <-1
df4 <- aggregate(df3[c("Count")], by=list(State=df3$State, Industry=df3$Industry), FUN=sum, na.rm=TRUE,ordered=TRUE)
df4 <- df4[order(df4[1],decreasing=FALSE),]

# Industry wise count in each State
datatable(df4, options = list( pageLength = 5, lengthMenu = c(5, 10, 40),   initComplete = JS(
    "function(settings, json) {",
    "$(this.api().table().header()).css({'background-color': '#01975b', 'color': '#fff'});",
    "}")), rownames=TRUE)

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
# Statewise count of Industries

ggplot(df2, aes(x = State, y = Count, color = Industry)) +
  geom_point() +
  coord_flip() + ggtitle("State wise Count of Industries")
## Warning in plyr::split_indices(scale_id, n): '.Random.seed' is not an
## integer vector but of type 'NULL', so ignored

# Industry wise count in each State
ggplot(df4, aes(x = Industry, y = Count, color = State)) +
  geom_point() +
  coord_flip() + ggtitle("Industry wise Count in Each State")

Quesiton 2

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

# State with most companies
dfsc <- data.frame(inc)
dfsc["Count"] <-1
dfsc1 <- aggregate(dfsc[c("Count")], by=list(State=dfsc$State ), FUN=sum, na.rm=TRUE)
dfsc1 <- dfsc1[order(dfsc1[2],decreasing=TRUE),]

# State count
head(dfsc1 ,5)
##    State Count
## 5     CA   701
## 45    TX   387
## 35    NY   311
## 47    VA   283
## 10    FL   282
NYd<-data.frame(subset(dfsc, State =="NY"))
NYd <- NYd[complete.cases(NYd), ] 

NYd1 <- NYd %>%
  group_by(State,Industry) %>%
  summarise_each( funs( mean, median, sd) , Employees)
## `summarise_each()` is deprecated.
## Use `summarise_all()`, `summarise_at()` or `summarise_if()` instead.
## To map `funs` over a selection of variables, use `summarise_at()`
datatable(NYd1, options = list( pageLength = 5, lengthMenu = c(5, 10, 40),   initComplete = JS(
    "function(settings, json) {",
    "$(this.api().table().header()).css({'background-color': '#01975b', 'color': '#fff'});",
    "}")), rownames=TRUE)
# Plot Mean, Median for Industries in NY.

ggplot(NYd, aes(x = Industry, y = Employees)) + geom_boxplot() + coord_flip() 

New york is the state with 3rd most companies. "Business Produts & Services", "Human Resources" and "Travel & Hospitality" Industry offers the high Employment. There are outliers too.

Question 3

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

Invd<-data.frame(subset(inc, State =="NY"))

ggplot(Invd, aes(x = Industry, y = Revenue/Employees)) +   geom_point()  + coord_flip() + ggtitle("Distribution per Industry")

Telecom, Retail, Human Resources, Consumer Products & Services, Business Products & services and Advertising & Marketing industries generate most revenue per employee.