-1

I'm trying to reorder the bars in my chart, some of which have duplicated category names, but some of them won't reorder. What am I doing wrong?

Sample data:
dyndom1 <- data.frame(Structure=c("T1R1vftd.web8", "6N4X.pdb", "T1R1vftd.web8", "T1R1vftd.web11", "6N52.pdb"),
                      Rotation_angle = c(31.4, 29, 4.9, 16.1, 28.1),
                      #Translation = c(0.5, 0.4, -0.3, 1.2),
                      Closure = c(99.9, 99.4, 98.9, 98.5, 96.7))
dyndom1

dyndom1$Structure <- fct_reorder(dyndom1$Structure, dyndom1$Closure)

ggplot(dyndom1, aes(fill=Structure, y=Closure, x=Structure)) + 
  geom_bar(position="dodge", stat="identity") +
  coord_flip()

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
glitterbox
  • 31
  • 5
  • Are there more than one observation per `Structure`? `fct_reorder` uses the median by default, you might want to use sum instead. – Jon Spring Mar 12 '21 at 21:08
  • It sounds like the unusual thing you might be asking about is that you have multiple items in `Structure` which have the same name, but which represent distinct items and which you want to display as separate bars. e.g. you might have item/value pairs of A1, B2, C3, A4, and you want the y axis to show A B C A with those respective 1 2 3 4 values in the x axis. Is that right? – Jon Spring Mar 12 '21 at 23:29
  • Yes, each structure item has a % closure, so it doesn't make sense to average or sum the closures. – glitterbox Mar 13 '21 at 15:33

1 Answers1

0

EDIT: modified to answer clarified question

I think the hard part of the question here is how to show duplicated categories on the axis. I don't think there's a way to do this in ggplot2 without a hack. Here I've borrowed one from tidytext, which has a function called reorder_within which helps for sorting categories in different orders per facet. The hack is to make new categories with unique identifiers after a "___". By making the categories into an ordered factor and then removing the identifiers from the labels, I think we can get what you want.

library(dplyr)
dyndom1 %>%
  arrange(Closure) %>%
  mutate(Structure_unique = paste0(Structure, "___", row_number()) %>%
           forcats::fct_inorder()) %>%
  ggplot(aes(Closure, Structure_unique)) +
  geom_col() +
  tidytext::scale_y_reordered()

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
  • Thank you for your answer, but it doesn't make sense to solve it like that for this data. I need to think more about it – glitterbox Mar 12 '21 at 21:30
  • So when you use `dyndom$Structure <- fct_reorder(dyndom$Structure, dyndom$Closure, .fun = sum)` that doesn't fix it? – Jon Spring Mar 12 '21 at 21:31
  • 1
    (It's also best practice on this site to include sample data in your question so that we can verify that it works instead of guessing about your data's characteristics.) – Jon Spring Mar 12 '21 at 21:34
  • Ah ok, I'm not sure how to add data... It doesn't fix it, but I realised that that approach wouldn't make sense with the data anyway – glitterbox Mar 12 '21 at 21:37
  • 1
    The standard guide for including data in your R question: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Jon Spring Mar 12 '21 at 21:39
  • I've written a new answer that's hopefully more useful. – Jon Spring Mar 13 '21 at 18:43