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(tidyverse)
## -- Attaching packages ------------------------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 3.1.0     v purrr   0.2.5
## v tibble  1.4.2     v dplyr   0.7.6
## v tidyr   0.8.1     v stringr 1.3.1
## v readr   1.1.1     v forcats 0.3.0
## -- Conflicts ---------------------------------------------------------------- tidyverse_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)
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
names(inc)
## [1] "Rank"        "Name"        "Growth_Rate" "Revenue"     "Industry"   
## [6] "Employees"   "City"        "State"
#removing Na from the dataset
inc_na<-na.omit(inc)
head(inc_na)

Aggregate to get the frequency of employee by industry

#indeed_skillaggr<-aggregate(read_indeed_url$Count,by=list(Category=read_indeed_url$Skills), FUN=sum)
#indeed_skillaggr

inc_na_aggr<-aggregate(inc_na$Employees, by=list(Categgory=inc_na$Industry), FUN=sum)
inc_na_aggr

Revenue by state

#skills_count<-read_indeed_url %>%
#  group_by(Skills) %>%
#  summarise(Total=sum(Count)) %>%
#  arrange(desc(Total))

#skills_count

revenue_by_state<-inc_na %>%
  group_by(State) %>%
  summarise(Total=sum(Revenue)) %>%
  arrange(desc(Total))
  
revenue_by_state
#skills_city<-read_indeed_url %>%
 # group_by(Skills,City) %>%
#  summarise(Total=sum(Count)) %>%
#  arrange(desc(Total))

#skills_city

rev_by_ind_state<-inc_na %>%
  group_by(Industry, State) %>%
  summarise(Total=sum(Revenue)) %>%
  arrange(desc(Total))

rev_by_ind_state

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_state<-inc %>%
  group_by(State) %>%
  summarise(Total=n()) %>%
  arrange(desc(Total))

inc_state

Graph distribution of companies by state

#g<-ggplot(inc_state,aes(x=reorder(State,Total, height=1),y=Total))+ geom_bar(stat='identity')+
  #coord_flip()+
#  labs(title='Frequency of Companies by State') +
 # xlab('State')+
 # ylab('Total')
#g

ggplot(data=inc_state, aes(x=reorder(State, Total),y=Total, fill=State)) +
    geom_bar(stat= "identity") +
    #guides(fill=FALSE) +
    xlab("State") + ylab("Total companies") +
    ggtitle("Companies by State") +
    theme(axis.text.x = element_text(angle=65, vjust=0.6))

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
inc_select<-  inc%>%select(c(State, Industry, Employees))

inc_select<-inc_select[complete.cases(inc_select),]

inc_mean<-inc_select %>% 
  filter(State == 'NY') %>%
  group_by(Industry) %>%
  summarise(mean=mean(Employees), median=median(Employees))

ggplot(inc_mean, aes(x=Industry, y=mean)) +
  geom_bar(stat="identity", width = 0.5) +
  ggtitle("Average of employee by Industry")+
  xlab("Industry")+
  ylab("Mean Employees")+
    theme(axis.text.x = element_text(angle=65, vjust=0.6))

ggplot(inc_mean, aes(x=Industry, y=median)) +
  geom_bar(stat="identity", width = 0.5) +
  ggtitle(" Industry by median employee")+
  xlab("Industry")+
  ylab("Median Employees")+
    theme(axis.text.x = element_text(angle=65, vjust=0.6))

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
inc_investor<-inc %>%
  select(c(Industry,Revenue, Employees))

inc_investor<-inc_investor[complete.cases(inc_investor),]

inc_rev_total<-inc_investor %>% 
  group_by(Industry) %>%
  summarise(Revenue_Total=sum(Revenue),Employees_Total=sum(Employees))

inc_revenue_emp<-transform(inc_rev_total, rev_per_emp= Revenue_Total / Employees_Total)%>%
  arrange(desc(rev_per_emp))

ggplot(inc_revenue_emp, aes(x=reorder(Industry,rev_per_emp), y=rev_per_emp))+
  geom_bar(stat="identity")+
  coord_flip()+
  ggtitle("Industry with most Revenue per Employee")+
  xlab("Industry")+
  ylab("Revenue Per Employees") +
  theme(axis.text.x = element_text(angle=65, vjust=0.6))