0

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!

stefan
  • 90,330
  • 6
  • 25
  • 51
  • 1
    Welcome to SO! To help us to help you would you mind sharing [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. To share your data, you could type `dput(NAME_OF_DATASET)` into the console and copy & paste the output starting with `structure(....` into your post. If your dataset has a lot of observations you could do e.g. `dput(head(NAME_OF_DATASET, 10))` for the first ten rows of data. – stefan Feb 19 '22 at 15:29
  • 1
    Try with `aes(..., fill = reorder(Name, gpa))`. This way the `fill` colors are assigned based on the reordered `Name` too. – stefan Feb 19 '22 at 16:04
  • That does not work, unfortunately (the lowest 3 bars end up colored), but I kind of managed to find a workaround: If I use fill = as.factor(GPA) and order the colors appropriately, I end with the plot I'd like to see. – Bálint L. Tóth Feb 19 '22 at 16:37
  • One of the reasons why I personally prefer to use named vectors to assign colors to categories. But as you said that the bottom bars get colored it would have been enough to simply reverse the order using `scale_fill_manual(values = rev(....))` to to get the colors in the right order. – stefan Feb 19 '22 at 17:40
  • Yes, I have realized my mistake in the meantime, your solution works perfectly too, thank you! – Bálint L. Tóth Feb 19 '22 at 18:57
  • Check out the {gghighlight} package. It makes this pretty easy. – Dan Adams Feb 20 '22 at 02:12

0 Answers0