we will have the second exam on August 27, 2025, from 12:00 PM to 2:00 PM at ICDI building
For the exam, students must use a tablet with a keyboard or a notebook computer.
Answers must be written by hand using a pencil or blue pen.
You may write or type anything on both sides of one A4 sheet to bring into the exam.
The use of AI tools, internet searches, or any form of social media during the exam is strictly prohibited.
Bar Plot
Dataset
Create a sample data frame, employee_data, representing employee information in a company:
Tip Dataset for create a bar plot.
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
Note 1. Bar Plot of Employee Count per Department:
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")
Note 2. Bar Plot of Employee Count per City:
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")
Note 3. Stacked Bar Plot of Gender Distribution per Department:
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)
Note 4. Bar Plot with Custom Colors:
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)))
Note 5. Bar Plot of Average Salary per Department:
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")
Note 6. Bar Plot of Total Salary per City:
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")
Note 7. Horizontal Bar Plot of Employee Count by Gender:
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)
Note 8. Grouped Bar Plot of Gender by City:
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)
Note 9. Bar Plot with Custom Axis Labels:
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"))
Note 10. Bar Plot with Sorted Values:
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")