Homework One

Joshua Hummell

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         Revenue         
##  Min.   :   1   Length:5001        Min.   :  0.340   Min.   :2.000e+06  
##  1st Qu.:1252   Class :character   1st Qu.:  0.770   1st Qu.:5.100e+06  
##  Median :2502   Mode  :character   Median :  1.420   Median :1.090e+07  
##  Mean   :2502                      Mean   :  4.612   Mean   :4.822e+07  
##  3rd Qu.:3751                      3rd Qu.:  3.290   3rd Qu.:2.860e+07  
##  Max.   :5000                      Max.   :421.480   Max.   :1.010e+10  
##                                                                         
##    Industry           Employees           City              State          
##  Length:5001        Min.   :    1.0   Length:5001        Length:5001       
##  Class :character   1st Qu.:   25.0   Class :character   Class :character  
##  Mode  :character   Median :   53.0   Mode  :character   Mode  :character  
##                     Mean   :  232.7                                        
##                     3rd Qu.:  132.0                                        
##                     Max.   :66803.0                                        
##                     NA's   :12
colnames(inc)
## [1] "Rank"        "Name"        "Growth_Rate" "Revenue"     "Industry"   
## [6] "Employees"   "City"        "State"

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:

Let’s get the datatype for each column

# Insert your code here, create more chunks as necessary
sapply(inc, class)
##        Rank        Name Growth_Rate     Revenue    Industry   Employees 
##   "integer" "character"   "numeric"   "numeric" "character"   "integer" 
##        City       State 
## "character" "character"

Let’s get the count of how many items there are (1-5000, there should be 5001)

nrow(inc)
## [1] 5001
unique(inc$Industry)
##  [1] "Consumer Products & Services" "Government Services"         
##  [3] "Health"                       "Energy"                      
##  [5] "Advertising & Marketing"      "Real Estate"                 
##  [7] "Financial Services"           "Retail"                      
##  [9] "Software"                     "Computer Hardware"           
## [11] "Logistics & Transportation"   "Food & Beverage"             
## [13] "IT Services"                  "Business Products & Services"
## [15] "Education"                    "Construction"                
## [17] "Manufacturing"                "Telecommunications"          
## [19] "Security"                     "Human Resources"             
## [21] "Travel & Hospitality"         "Media"                       
## [23] "Environmental Services"       "Engineering"                 
## [25] "Insurance"

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

library(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
library(ggplot2)


inc_company <-  inc %>% select(State) %>% group_by(State) %>% summarise(n = n()) %>% arrange(desc(n))



p <- ggplot(inc_company,aes(x = reorder(State, -n), y = n))+geom_bar(stat="identity")+coord_flip()
p

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.

First, let’s find out which state is third

# Answer Question 2 here
inc_company[3,]
## # A tibble: 1 x 2
##   State     n
##   <chr> <int>
## 1 NY      311

Now let’s take a closure look at it

NY <- inc %>% select_all() %>% filter(State == "NY")
hist(NY$Employees,breaks = 100)

It looks like there are a few that really stand out, let’s see what they are

NY %>% select_all() %>% filter(Employees > 5000)
##   Rank                       Name Growth_Rate   Revenue
## 1 4577 Sutherland Global Services        0.48 5.976e+08
## 2 4936                       Coty        0.36 4.600e+09
##                       Industry Employees      City State
## 1 Business Products & Services     32000 Pittsford    NY
## 2 Consumer Products & Services     10000  New York    NY

It looks like they may have typed in an extra 0, but since I don’t know them well enough, I’ll just remove them for now

NY <- NY %>% select_all() %>% filter(Employees < 5000)
hist(NY$Employees,breaks = 100)

STill there are a few outliers but overall more believable

Let’s get a look at the industries these employees fall in

ny_plot <-  NY %>% select(Industry, Employees, Name) %>% group_by(Industry, Name) %>% summarise(n = sum(Employees)) %>% arrange(desc(n))
## `summarise()` has grouped output by 'Industry'. You can override using the `.groups` argument.
ny_plot <- ny_plot %>% filter(complete.cases(Industry,n))


ny_plot <- ny_plot%>%
  group_by(Industry) %>%
  summarise_at(vars(n),funs(mean, sd, n()))
## Warning: `funs()` was deprecated in dplyr 0.8.0.
## Please use a list of either functions or lambdas: 
## 
##   # Simple named list: 
##   list(mean = mean, median = median)
## 
##   # Auto named with `tibble::lst()`: 
##   tibble::lst(mean, median)
## 
##   # Using lambdas
##   list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
p <- ggplot(ny_plot,aes(x = reorder(Industry, -mean), y = mean)) + 
  geom_point()+
  geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=.2,
                position=position_dodge(0.05)) +
  coord_flip()
p

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.

library("scales")
## Warning: package 'scales' was built under R version 4.1.2
inc_plot <- inc %>% filter(complete.cases(.))


inc_plot$Revenue <- format(inc_plot$Revenue, scientific = FALSE)

inc_plot <-  NY %>% select(Industry, Revenue, Name) %>% group_by(Industry, Name) %>%  arrange(desc(Revenue))

inc_dis <- inc_plot%>%
  group_by(Industry) %>%
  summarise_at(vars(Revenue),funs(mean, sd, n()))



p <- ggplot(inc_dis,aes(x = reorder(Industry, -mean), y = mean)) + 
  geom_point()+
  geom_errorbar(aes(ymin=0, ymax=mean+sd), width=.2,
                position=position_dodge(0.05)) +
  coord_flip()
p

Looks like IT services has the best bet at making the most money, but the large Standard Deviation makes it look more risky, a more secure bet would be Travel and hospitality, which has a lower mean revenue but also less of a standard deviation.