Bar Plot

Dataset

Create a sample data frame, employee_data, representing employee information in a company:

This dataset includes:

  • Department: Department of each employee (categorical).

  • Gender: Gender of each employee (categorical).

  • Salary: Salary of each employee (numeric).

  • Experience: Years of experience (numeric).

  • City: City where each employee is based (categorical).

Exercises

Create a bar plot showing the number of employees in each department.

Hint: Use tapply() function.

Solution:

total_salary <- tapply(employee_data$Salary, employee_data$City, sum)
barplot(total_salary, 
        main = "Total Salary per City", 
        xlab = "City", 
        ylab = "Total Salary")

Create a bar plot displaying the count of employees in each city.

Solution:

city_counts <- table(employee_data$City)
barplot(city_counts, 
        main = "Employee Count per City", 
        xlab = "City", 
        ylab = "Count")

Create a stacked bar plot showing the number of male and female employees in each department.

Solution:

gender_dept_counts <- table(employee_data$Department, employee_data$Gender)

barplot(gender_dept_counts, 
        main = "Gender Distribution per Department", 
        xlab = "Department", 
        ylab = "Count", 
        col = c("lightblue", "pink"), 
        legend = TRUE)

Create a bar plot of employee counts by department, and use a custom color palette for each bar (col = rainbow(length(dept_counts))).

Solution:

dept_counts <- table(employee_data$Department)

barplot(dept_counts, 
        main = "Employee Count per Department", 
        xlab = "Department", 
        ylab = "Count", 
         col = rainbow(length(dept_counts)))

Calculate the average salary for each department and create a bar plot to show these averages.

Hint: Use tapply() function.

Solution:

avg_salary <- tapply(employee_data$Salary, employee_data$Department, mean)

barplot(avg_salary, 
        main = "Average Salary per Department", 
        xlab = "Department", 
        ylab = "Average Salary")

Calculate the total salary of employees in each city and create a bar plot of the results.

Solution:

total_salary <- tapply(employee_data$Salary, employee_data$City, sum)

barplot(total_salary, 
        main = "Total Salary per City", 
        xlab = "City", 
        ylab = "Total Salary")

Create a horizontal bar plot to show the count of male and female employees.

Solution:

gender_counts <- table(employee_data$Gender)

barplot(gender_counts, 
        main = "Employee Count by Gender", 
        xlab = "Count", 
        ylab = "Gender", 
       horiz = TRUE)

Create a grouped bar plot to display the number of male and female employees in each city. (col = c("lightgreen", "lightcoral"))

Solution:

gender_city_counts <- table(employee_data$City, employee_data$Gender)

barplot(gender_city_counts, 
        beside = TRUE, 
          main = "Gender Distribution by City", 
          xlab = "City", 
          ylab = "Count", 
          col = c("lightgreen", "lightcoral"), 
          legend = TRUE)

Create a bar plot of employee counts by department, with custom labels for the x-axis and a title. (names.arg = c("Sales", "Mktg", "IT", "HR", "Fin"))

Solution:

dept_counts <- table(employee_data$Department)
barplot(dept_counts, 
        main = "Employee Distribution by Department", 
        xlab = "Department (Custom Labels)", 
        ylab = "Number of Employees", 
        names.arg = c("Sales", "Mktg", "IT", "HR", "Fin"))

Calculate the total years of experience per department, sort the values in descending order, and create a bar plot based on this sorted data.

Hint: Use tapply() and sort() function.

Solution:

total_experience <- tapply(employee_data$Experience, employee_data$Department, sum)
sorted_experience <- sort(total_experience, decreasing = TRUE)

barplot(sorted_experience, 
        main = "Total Years of Experience per Department (Sorted)", 
        xlab = "Department", 
        ylab = "Total Experience")