DATA608 Principles of Data Visualization and Introduction to ggplot2
Note: The dataset used for this homework is about the 5,000 fastest growing companies in the US (as compiled by Inc. magazine). Provided by: Charley Ferrari
Rank | Name | Growth_Rate | Revenue | Industry | Employees | City | State |
---|---|---|---|---|---|---|---|
1 | Fuhu | 421.48 | 1.179e+08 | Consumer Products & Services | 104 | El Segundo | CA |
2 | FederalConference.com | 248.31 | 4.960e+07 | Government Services | 51 | Dumfries | VA |
3 | The HCI Group | 245.45 | 2.550e+07 | Health | 132 | Jacksonville | FL |
4 | Bridger | 233.08 | 1.900e+09 | Energy | 50 | Addison | TX |
5 | DataXu | 213.37 | 8.700e+07 | Advertising & Marketing | 220 | Boston | MA |
6 | MileStone Community Builders | 179.38 | 4.570e+07 | Real Estate | 63 | Austin | TX |
7 | Value Payment Systems | 174.04 | 2.550e+07 | Financial Services | 27 | Nashville | TN |
8 | Emerge Digital Group | 170.64 | 2.390e+07 | Advertising & Marketing | 75 | San Francisco | CA |
And lets preview this data:
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:
misValues <- sum(is.na(inc))# Returning the column names with missing values
#column_na1 <- colnames(insuranceT_df1)[ apply(insuranceT_df1, 2, anyNA) ] # 2 is dimension(dim())
#misValues1 <- sum(is.na(prof.salary$yrs.since.phd))
#misValues1
cat("From Professor Salary dataset, the total of missing values is : ", misValues)
## From Professor Salary dataset, the total of missing values is : 12
Let’s visualize the missing values per variable.
x | |
---|---|
Rank | 0 |
Name | 0 |
Growth_Rate | 0 |
Revenue | 0 |
Industry | 0 |
Employees | 12 |
City | 0 |
State | 0 |
## Rank Name Growth_Rate Revenue Industry Employees
## 0 0 0 0 0 12
## City State
## 0 0
Variable, Employees has 12 missing values which represents 0.24% of the total record. We can delete this missing values as it has no significant impact on the entire data.
## Rank Name Growth_Rate Revenue Industry Employees
## 0 0 0 0 0 0
## City State
## 0 0
Let’s see stats
## Rank Name Growth_Rate Revenue
## Min. : 1 Length:4989 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 :2501 Mean : 4.615 Mean :4.825e+07
## 3rd Qu.:3750 3rd Qu.: 3.290 3rd Qu.:2.860e+07
## Max. :5000 Max. :421.480 Max. :1.010e+10
## Industry Employees City State
## Length:4989 Min. : 1.0 Length:4989 Length:4989
## 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
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.
#my-chunk1, R.options = list(width = 10)} or fig.dim = c(8,6)
library(ggplot2)
#checking variable distribution
#library(ggthemes)
#plot1 <-inc1$State %>%
# gather() %>%
# ggplot(aes(value)) +
# facet_wrap(~ key, scales = "free") +
# geom_bar() +
# theme_wsj()+ scale_colour_wsj("colors6")
# plot1
# Something wrong with the code below...i have used it in the pass with no issue
# ggplot(inc1, aes(x= (State))) +
# geom_bar(aes(y = ..prop.., fill = factor(..x..)), stat="count") +
# geom_text(aes( label = scales::percent(..prop..),
# y= ..prop.. ), stat= "count", vjust = -.5) +
# labs(y = "Percent") +
# scale_y_continuous(labels=percent)
#
# ggplot(inc1, aes(x=State)) + geom_bar(stat='identity', position='dodge')
#ggplot(inc1) + geom_bar(aes(x = State))
#
# ggplot(inc1) + geom_bar(aes(y = State))
#
#
#
# ggplot(data=inc1, aes(y=State)) +
# geom_bar(fill= rainbow(52)) +
# geom_text(stat='count', aes(label=..count..), vjust=-1)
# geom_bar(aes(x=variables, y=number.missing), stat = 'identity', col='blue') +
# labs(x='variables', y="number of missing values", title='Number of missing values') +
# theme(axis.text.x = element_text(angle = 100, hjust = 0.2))
ggplot(data=inc1, aes(x=State)) +
geom_bar(fill= rainbow(52)) +
geom_text(stat='count', size= 4, aes(label=..count..), vjust=-0.1)+
# geom_bar(aes(x=variables, y=number.missing), stat = 'identity', col='blue') +
labs(x='Number of Companies', y="States", title='Number of Companies by State') +
theme(axis.text.x = element_text(angle = 100, hjust = 0.1))
print("Display plot in verticale, We could add the industry with fill = ? , but something goes wrong when adding reorder() aes(x = reorder(?), ...")
## [1] "Display plot in verticale, We could add the industry with fill = ? , but something goes wrong when adding reorder() aes(x = reorder(?), ..."
ggplot(data=inc1, aes(y=State)) +
geom_bar(fill= rainbow(52)) +
geom_text(stat='count', size= 4, aes(label=..count..), vjust=-0)+
# geom_bar(aes(x=variables, y=number.missing), stat = 'identity', col='blue') +
labs(x='Number of Companies', y="States", title='Number of Companies by State') +
theme(axis.text.x = element_text(angle = 180, hjust = 0.2))
# library(scales)
# plot1b <- ggplot(data=inc1, aes(x=States)) +
# geom_bar(aes(y = (..count..)/sum(..count..)))
# plot1b + scale_y_continuous(labels = percent)
print("Another way of presenting the result and fixing the error with reorder()")
## [1] "Another way of presenting the result and fixing the error with reorder()"
count_companyState <- inc1 %>%
group_by(State) %>%
summarise(Number_of_Companies = n())
#count_companyState
ggplot(count_companyState, aes(x = Number_of_Companies, y = reorder(State, -Number_of_Companies))) + geom_bar(stat = "identity", col= "blue")
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.
# We need to filter the dataframe by third-state
count_companyState[
with(count_companyState, order(Number_of_Companies)),
]
## # A tibble: 52 x 2
## State Number_of_Companies
## <chr> <int>
## 1 PR 1
## 2 AK 2
## 3 WV 2
## 4 WY 2
## 5 SD 3
## 6 MT 4
## 7 NM 5
## 8 VT 6
## 9 HI 7
## 10 AR 9
## # ... with 42 more rows
inc2 <- inc1 %>%
filter(State == 'NY')%>%
filter(complete.cases(.))
#View(inc2)
inc2 <- inc2 %>%
group_by(Industry)%>%
summarise(mean_Employees = mean(Employees), median_Employees = median(Employees), Number_of_Companies = n())
# something wrong when adding arrange()
#%>%
#arrange(-mean_Employees)
#arrange(Industry, -mean_Employees, median_Employees, Number_of_Companies)
ggplot(inc2, aes(x = median_Employees, y = reorder(Industry, -median_Employees))) + geom_bar(stat = "identity", col = "green")
ggplot(data=inc2, aes(x= Number_of_Companies, Industry), y= reorder(Industry )) +
geom_bar(stat= 'identity', fill = 'orange') +
geom_text(size= 4, aes(label=round(mean_Employees, 0)), vjust=-1)+
# geom_bar(aes(x=variables, y=number.missing), stat = 'identity', col='blue') +
labs(x='Number of Companies', y="States", title='Average Employees per Industry in new York State') +
theme(axis.text.x = element_text(angle = 180, hjust = 0.2))
ggplot(inc2, aes(x=Number_of_Companies, y=Industry, fill=median_Employees)) +
geom_bar(stat="identity", position="stack") +
geom_text(aes(y=Industry, x=5, label=round(mean_Employees,0)),
size=4, inherit.aes=FALSE)
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.
inc3 <- inc1 %>%
filter(State == 'NY')%>%
filter(complete.cases(.))
# Creating new dataframe with total revenue by industry, total revenue by employee per New York State
inc3 <- inc3 %>%
group_by(Industry)%>%
summarise(revenueTotal = sum(Revenue), EmployeesTotal = sum(Employees)) %>%
mutate(Revenue_per_Employees = revenueTotal/ EmployeesTotal)
ggplot(inc3, aes(x = Revenue_per_Employees, y = reorder(Industry, -Revenue_per_Employees))) + geom_bar(stat = "identity", fill = "coral")+geom_text(size= 4, aes(label=round(Revenue_per_Employees, 0)), vjust=-0)+
# geom_bar(aes(x=variables, y=number.missing), stat = 'identity', col='blue') +
labs(x='Revenue_per_Employees', y="States", title='Industry Revenue per Employee in new York State') +
theme(axis.text.x = element_text(angle = 180, hjust = 0.2))