library(readr)      
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
hr <- read_csv('https://raw.githubusercontent.com/aiplanethub/Datasets/refs/heads/master/HR_comma_sep.csv')
## Rows: 14999 Columns: 10
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Department, 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.
head(hr)
## # A tibble: 6 × 10
##   satisfaction_level last_evaluation number_project average_montly_hours
##                <dbl>           <dbl>          <dbl>                <dbl>
## 1               0.38            0.53              2                  157
## 2               0.8             0.86              5                  262
## 3               0.11            0.88              7                  272
## 4               0.72            0.87              5                  223
## 5               0.37            0.52              2                  159
## 6               0.41            0.5               2                  153
## # ℹ 6 more variables: time_spend_company <dbl>, Work_accident <dbl>,
## #   left <dbl>, promotion_last_5years <dbl>, Department <chr>, salary <chr>
# Histogram (Employee Satisfaction)
ggplot(hr, aes(x = satisfaction_level)) +
  geom_histogram(binwidth = 0.05, fill = "skyblue", color = "black") +
  labs(title = "Most Employees Have Moderate Satisfaction Levels",
       x = "Satisfaction Level",
       y = "Count") +
  theme_minimal()

 #Analysis: Most employees have moderate satisfaction levels, with fewer employees at extreme satisfaction levels.
#Box plot (Last Evaluation Scores)
ggplot(hr, aes(y = last_evaluation)) +
  geom_boxplot(fill = "orange", color = "black") +
  labs(title = "Evaluation Scores Show Some Outliers",
       y = "Last Evaluation Score") +
  theme_minimal()

#Analysis: Some employees have exceptionally high or low evaluation scores.
# Box plot (Average monthly hours by department)
ggplot(hr, aes(x = Department, y = average_montly_hours, fill = Department)) +
  geom_boxplot() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  labs(title = "R&D and IT Departments Have Higher Monthly Hours",
       x = "Department",
       y = "Average Monthly Hours") +
  theme_minimal()

#Analysis: Some departments, like R&D and IT, tend to have higher working hours.
# Pie Chart (Salary Level)
attrition_salary <- hr %>%
  group_by(salary) %>%
  summarise(attrition_count = sum(left))
plot_ly(attrition_salary, labels = ~salary, values = ~attrition_count, type = "pie") %>%
  layout(title = list(text = "Higher Attrition Among Low-Salary Employees"))
#Analysis: Employees with low salaries are more likely to leave the company.
# Bar plot (Average satisfaction level by department)
hr %>%
  group_by(Department) %>%
  summarise(avg_satisfaction = mean(satisfaction_level)) %>%
  ggplot(aes(x = Department, y = avg_satisfaction, fill = Department)) +
  geom_bar(stat = "identity") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  labs(title = "HR and Sales Have the Lowest Satisfaction Levels",
       x = "Department",
       y = "Average Satisfaction Level") +
  theme_minimal()

#Analysis: Employees in HR and Sales have the lowest satisfaction levels.