# Load ggplot2
library(ggplot2)
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(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(readr)
# Data
hr <- read_csv("~/Documents/UNH/Semester-2/Data_Visu_Comm/Assignments/Assignment1_4thFeb/HR_comma_sep.csv")
## Rows: 14999 Columns: 10
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): sales, salary
## dbl (8): satisfaction_level, last_evaluation, number_project, average_montly...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
hr_ql <- hr %>%
group_by(sales) %>%
tally() %>% # summarized data
ungroup() %>%
arrange(desc(sales)) %>%
mutate(prop = n / sum(n) *100) %>% # Created variables for ploting and label
mutate(ypos = cumsum(prop)- 0.5*prop ,
label = paste0(sales , ' Department', '\n' ,round(prop , digits = 1) , '%') ,
label_plotly = paste0('Department: ' , sales))
## 1. Using the HR_comma_sep dataset, create a pie chart for the variable sales.
ggplot(hr_ql, aes(x="", y=prop, fill=as.factor(sales))) +
geom_bar(stat="identity", width=1) +
coord_polar("y", start=0) + # Up to here: basic pie chart
theme_void() + # White background
theme(legend.position="none") + # position of labels
geom_text(aes(y = ypos, label = as.factor(label)),
color = "black",
size=3) + # adding the labels
scale_fill_brewer(palette="Set1") # color set
## Warning in RColorBrewer::brewer.pal(n, pal): n too large, allowed maximum for palette Set1 is 9
## Returning the palette you asked for with that many colors

## 2. Using the HR_comma_sep dataset, create a pie chart for the variable salary.
hr_q2 <- hr %>%
group_by(salary) %>%
tally() %>% # summarized data
ungroup() %>%
arrange(desc(salary)) %>%
mutate(prop = n / sum(n) *100) %>% # Created variables for ploting and label
mutate(ypos = cumsum(prop)- 0.5*prop ,
label = paste0(salary , ' salary', '\n' ,round(prop , digits = 1) , '%') ,
label_plotly = paste0('salary: ' , salary))
ggplot(hr_q2, aes(x="", y=prop, fill=as.factor(salary))) +
geom_bar(stat="identity", width=1) +
coord_polar("y", start=0) + # Up to here: basic pie chart
theme_void() + # White background
theme(legend.position="none") + # position of labels
geom_text(aes(y = ypos, label = as.factor(label)),
color = "white",
size=3) + # adding the labels
scale_fill_brewer(palette="Set1") # color set
