基于R语言ggplot2生成分年级单生标注的个性化成绩柱状图需求
Solution for Personalized Student Score Bar Charts (By Grade Level)
Got it, let's build exactly what you need—personalized bar charts for each student where only their name is visible, while still letting them compare their score to the rest of their grade (no other student's info exposed). Here's the adjusted R code with clear explanations:
Step 1: Setup & Sample Data
First, let's confirm your data setup (swap in your full 50-student dataset here):
# Load required libraries library(ggplot2) library(dplyr) library(purrr) # For seamless looping # Sample data (replace with your 50-student dataset) names <- c("Jackson", "Smith", "Johnson", "Richards", "Matthews", "Redmond", "Phillips") scores <- c(.99, .65, .73, .89, .88, .92, .87) level <- c(10,11,10,11,11,11,11) grades <- cbind.data.frame(names, scores, level) # Split data by grade level (your original step, kept here) grade_split <- split(grades, grades$level)
Step 2: Function to Generate Personalized Charts
We'll create a function that takes a grade-level dataset and builds a unique chart for every student in that group:
generate_personalized_plot <- function(grade_data) { # Grab the current grade for titles and file naming current_grade <- unique(grade_data$level) # Loop through each student in the grade group walk(grade_data$names, function(student_name) { # Create a copy of the data where only the current student's name is visible plot_data <- grade_data %>% mutate(display_name = ifelse(names == student_name, names, "")) # Build the personalized chart p <- ggplot(data = plot_data, aes(x = display_name, y = scores, fill = scores)) + geom_bar(stat = "identity", position = "dodge", alpha = ifelse(plot_data$names == student_name, 1, 0.6)) + # Make the student's bar stand out with full opacity scale_fill_viridis_c(option = "mako", guide = "none") + theme_minimal() + theme( axis.text.x = element_text(angle = 45, vjust = 0.5, size = 12), plot.title = element_text(hjust = 0.5, size = 14, face = "bold") ) + labs( title = paste0("Your Test Score | Grade ", current_grade, " - Feb 2018"), x = "", y = "Score (0-1)" ) # Save the chart with a unique, easy-to-identify filename ggsave( filename = paste0(student_name, "_Grade", current_grade, "_ScoreChart.png"), plot = p, width = 8, height = 6, dpi = 150 ) }) }
Step 3: Generate All Charts
Run this line to create personalized charts for every student across all grade levels:
# Apply the function to each grade-level dataset lapply(grade_split, generate_personalized_plot)
Key Features Breakdown
- Personalized Privacy: Only the target student's name appears on the x-axis; all others are hidden (replace
""with"Other Students"if you prefer a placeholder label). - Visual Focus: The student's bar is fully opaque, while peers' bars are faded to draw attention to their score.
- Grade-Level Segmentation: Students only compare against their own grade group, as we split the data first.
- Auto-Distribution Ready: Each chart saves with a unique filename (e.g.,
Jackson_Grade10_ScoreChart.png) for easy sharing.
Quick Customization Tips
- Adjust the
alphavalue to make peer bars more/less visible. - Swap
scale_fill_viridis_cfor a different color scale (e.g.,scale_fill_gradient(low = "#e0f3ff", high = "#2c5aa0")). - Add a class average line with
geom_hline(yintercept = mean(grade_data$scores), linetype = "dashed", color = "darkred").
内容的提问来源于stack exchange,提问作者regents
相关产品推荐
相关产品推荐

