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

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
library(psych)
#to see the growth rate
describe(inc$Growth_Rate)
##    vars    n mean    sd median trimmed  mad  min    max  range  skew
## X1    1 5001 4.61 14.12   1.42    2.14 1.22 0.34 421.48 421.14 12.55
##    kurtosis  se
## X1   242.34 0.2
#to see the revenue
describe(inc$Revenue)
##    vars    n     mean        sd   median  trimmed      mad   min      max
## X1    1 5001 48222535 240542281 10900000 17334966 10674720 2e+06 1.01e+10
##         range  skew kurtosis      se
## X1 1.0098e+10 22.17   722.66 3401441
describe(inc$Employees)
##    vars    n   mean      sd median trimmed   mad min   max range  skew
## X1    1 4989 232.72 1353.13     53   81.78 53.37   1 66803 66802 29.81
##    kurtosis    se
## X1  1268.67 19.16

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.

ggplot(df2,aes(x=factor(name),y=depth)) + geom_bar(stat=‘identity’,data=subset(df2,df2\(Mut==2),fill='red') + geom_bar(stat='identity',data=subset(df2,df2\)Mut==1),fill=‘blue’) + coord_flip() + labs(y=‘depth’,x=‘species’)

# Answer Question 1 here

library(ggplot2)
## 
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
## 
##     %+%, alpha
library(forcats)
library(ggthemes)
ggplot(inc, aes(x=fct_infreq(State))) +
    geom_bar(fill="grey",stat="count") +
    coord_flip() +
    geom_text(aes(label=..count..), stat="count", size=2.5,hjust=-0.4,color="brown") +
    labs(title = "5,000 fastest growing companies in the US", x = "State", y = "No. of Companies in State") +
    theme_tufte()

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

NY <- subset(inc, State=="NY")
NY <- NY[complete.cases(NY),]

outliers <- NY[order(-NY$Employees),]
head(outliers)
##      Rank                       Name Growth_Rate   Revenue
## 4577 4577 Sutherland Global Services        0.48 5.976e+08
## 4936 4936                       Coty        0.36 4.600e+09
## 4716 4716              Westcon Group        0.44 3.800e+09
## 3899 3899  Denihan Hospitality Group        0.71 2.808e+08
## 4363 4363               TransPerfect        0.55 3.413e+08
## 1498 1499       Sterling Infosystems        2.66 2.149e+08
##                          Industry Employees      City State
## 4577 Business Products & Services     32000 Pittsford    NY
## 4936 Consumer Products & Services     10000  New York    NY
## 4716                  IT Services      3000 Tarrytown    NY
## 3899         Travel & Hospitality      2280  New York    NY
## 4363 Business Products & Services      2218  New York    NY
## 1498              Human Resources      2081  New York    NY

Ommitted data

There are two outliers on this data. I have omitted both of them. The most people employed number are in Business Products & Services which is 32000 and in Consumer Products & Services which is 10,000 followed by IT Services and the least people emplyed are in Government Services. If I include these outliers then I cannot get anything from the graph so I have not included those industry in this graph. It will only distort the graph.

#ggplot for the 
NY <- ggplot(NY, aes(x=Industry, y=Employees)) + 
      geom_boxplot(width=0.7, fill="light grey", outlier.size = 7,outlier.colour="red")+
      coord_flip(ylim = c(0,3200)) +
      stat_summary(aes(shape = "mean"), fun.y = mean, geom="point", fill="blue",colour="blue", size=3) +
      stat_summary(aes(shape = "median"), fun.y =median, geom="point",fill="darkgreen",colour="darkgreen", size=3)+
      scale_colour_manual(values=c("stat_summary", "outlier"))+
      ggtitle('Mean and Median employment by Industry for fastest growing companies in NY') + 
      theme(legend.position = "right",plot.title = element_text(size=14, face="bold"))
NY

## Not able to change the color of the legend, the red circle is the outlier.

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
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
revenue_data <- inc[complete.cases(inc),]

revenue_data <- revenue_data%>% group_by(Industry)%>%
                summarise(Revenue=sum(Revenue),Employees=sum(Employees))%>%
                mutate(revenue_per_employee = Revenue / Employees)
head(revenue_data)
## # A tibble: 6 x 4
##   Industry                         Revenue Employees revenue_per_employee
##   <fct>                              <dbl>     <int>                <dbl>
## 1 Advertising & Marketing       7785000000     39731              195943.
## 2 Business Products & Services 26345900000    117357              224494.
## 3 Computer Hardware            11885700000      9714             1223564.
## 4 Construction                 13174300000     29099              452741.
## 5 Consumer Products & Services 14956400000     45464              328972.
## 6 Education                     1139300000      7685              148250.
#let's plot the data into the graph

employee_revenue <- ggplot(revenue_data, aes(x=reorder(Industry, revenue_per_employee),y=revenue_per_employee))+ 
    geom_bar(fill = "grey", stat="identity") +
    coord_flip() + 
    xlab("Industry") +
    ylab("Revenue Per Employee") +
    ggtitle("Revenue Per Employee for Industry") +
    theme_tufte()

employee_revenue

# highest revenue per employee
max_revenue_per_employee = max(revenue_data$revenue_per_employee)
max_revenue_per_employee
## [1] 1223564
##The most revenue generated is in the computer hardware industry.

# lowest revenue per employee
min_revenue_per_employee = min(revenue_data$revenue_per_employee)
min_revenue_per_employee
## [1] 40735.31