Warning Midterm Exam (Part 2 of 2) on Jan 9, 2025, 3:30–5:30 PM. at RB3208 Total Score: 15% (5% Multiple Choice, 10% Writing R Code). Topics: - Data Structure: Data Frame - Data Wrangling - Data Structure: XTS Project 1: 10% Create your CV using Quarto and upload the HTML and related files to CMU’s personal web hosting. 1. Submit your HTML file to your CMU personal web hosting. 2. Zip your project folder. The zip file should include the .qmd file, any image files, and CSS files (if used). I will render your files to compare them with your URL. Send your URL and the zip file to my email: somsak.c@icdi.cmu.ac.th before midnight on January 5, 2025. Content Requirements: Your CV must include: - Your picture - Your birthday - Your education - Your hobbies - Links to at least 2 social media platforms - Your future plans Criteria: 1. The HTML file and .qmd file must match. 2. Use a theme. 3. Use CSS. 4. Include a table. 5. Include a tabset 6. Include a video. 5. Use icons from Font Awesome (e.g., Facebook, YouTube, LINE, WeChat, etc.). 8. Modify the layout creatively.
Additional Note: Some of your score will be determined by votes from your peers.
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")