Change legend order in grouped barplot when levels are variables

I have an issue with my grouped barplot.

I have the following data:

  accuracy sensitivity specificity      auc rating
1 77.22019    89.22559    76.02808 82.62684      1
2 72.20195    89.14729    71.50997 80.32863      2
3 68.36983    95.48387    67.02841 81.25614      3
4 61.40511    73.25000    59.76454 66.50727      4
5 68.36983    95.48387    67.02841 81.25614      5

I use this code to create a grouped barplot:

library(ggplot2)
library(tidyr)

eval.df %>%
  gather("Measures", "Percent",-rating) %>%
  ggplot(aes(rating, Percent, fill = Measures)) +
  geom_bar(position = "dodge", stat = "identity")

I get this barplot:

enter image description here

But I want to change the order of the bars as well as the legend so that after "accuracy" comes "sensitivity", then "specificity" and then "auc".

I know that this is done when the different bars would represent different values of one variable (e.g., just defining the levels). But as in my case they are variables, I can't find a solution.

(Additionally, I would like to change the Names of the legend manually, if someone knows how to do this as well)

Thank you very much in advance!

eval.df %>%
  gather("Measures", "Percent",-rating) %>%
  mutate(Measures = factor(Measures,
                           levels=c("accuracy","sensitivity","specificity","auc"),
                           labels = letters[1:4])) %>%
  ggplot(aes(rating, Percent, fill = Measures)) +
  geom_bar(position = "dodge", stat = "identity")

in this example levels= sets the order of the measures as they would appear in the plot.
the labels lets you control how they then appear in the legend, if you omit label=, then it will be as the levels.

1 Like

Thank you so much for your answer. Sadly, your code doesn't seem to work for me as I now get the following "plot":

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:


Short Version

You can share your data in a forum friendly way by passing the data to share to the dput() function.
If your data is too large you can use standard methods to reduce it before sending to dput().
When you come to share the dput() text that represents your data, please be sure to format your post with triple backticks on the line before your code begins to format it appropriately.

```
( example_df <- structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4, 4.6, 
5, 4.4, 4.9), Sepal.Width = c(3.5, 3, 3.2, 3.1, 3.6, 3.9, 3.4, 
3.4, 2.9, 3.1), Petal.Length = c(1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 
1.4, 1.5, 1.4, 1.5), Petal.Width = c(0.2, 0.2, 0.2, 0.2, 0.2, 
0.4, 0.3, 0.2, 0.2, 0.1), Species = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L), .Label = c("setosa", "versicolor", "virginica"
), class = "factor")), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame")))
```

Sorry, as my knowledge in R is not very good, I was not able to take the exact code but this code worked for me to create an example dataset:

example_df <- as.data.frame(structure(list(accuracy= c(77,72,68,61,68), 
                             sensitivity = c(89,89,95,73,95), 
                             specificity = c(76,71,67,59,67), 
                             auc = c(82,80,81,66,81), 
                             rating = c(1,2,3,4,5))))

I tried your example on my code, and it worked as I described.
If you continue to have an issue of a blank plot. Restart your R session, and try again.
If you run your original code, is the plot blank or not ? but then add the factor line and it correctly changes the labels but drops the content ?
very strange....

Hey, thank you so much! I figured it out. I was just having some issues with the labels and bar colors that are in my original code which is why I messed up a part.
Thank you so much for your patience and help! This is exactly what I was looking for :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.