The Final Exam 2/2 will be held on March 14, 2025, from 8:00 to 11:00 AM at the ICDI building. Total Score: 15% of 100% - 5%: Multiple-choice questions. - 10%: Answering questions, editing, or showing the code based on the given results. You are allowed to use your computer/iPad/Tablet to run the R program. However, you are not allowed to use the internet to search for solutions via search engines (e.g., Google) or generative AI (e.g., ChatGPT). You are not allowed to discuss anything in the testing room. You are allowed to bring and write notes on one A4 page (both sides).
Bar Plot
Dataset
Create a sample data frame, employee_data, representing employee information in a company:
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
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")
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")
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)
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)))
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")
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")
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)
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)
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"))
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")