I am currently working on an R Markdown document for our school, which should make documenting student performance easier for the teachers.
I would like to include a bar chart using ggplot2, which
- orders students from best to worst based on their GPA, and
- colors the 3 highest bars gold, silver and bronze respectively, and all the other bars blue.
Note that the code needs to work with an arbitrary number of students. What I tried is:
subjects_long %>%
group_by(Name) %>%
summarize(gpa = mean(grade)) %>%
ggplot(aes(x = reorder(Name, GPA), y = GPA, fill = Name)) +
geom_col() +
coord_flip() +
scale_y_continuous(breaks = seq(0, 5, by = 0.1)) +
scale_fill_manual(values = c("#d6af36","#d7d7d7","#a77044",
rep("blue", length(subjects$Name)-3)))
This ensures that the code runs, and there is an appropriate number of columns every time, regardless of which dataset (class data) I run it on, but the bars getting colored gold/silver/bronze are not the ones with the highest value, but the ones with the highest (= alphabetically) names, regardless of how high their GPA is. Apparently, this is because scale_fill_manual
orders by levels of the factor, not by Y-axis values.
Any help would be greatly appreciated!