0

As far as I can see my code is correct so why is the reorder function not working? Ideally I do not want to have to order the data itself prior to plotting.

DOC_output_clean2 <- data.frame("Trap_type" = c("DOC 150", "DOC 150", "DOC 150", 
                                                "DOC 200", "DOC 200", "DOC 200", 
                                                "DOC 250", "DOC 250", "DOC 250", 
                                                "DOC 250", "DOC 250", "Fenn", "Fenn", 
                                                "Fenn", "Fenn", "Fenn", "Fenn"), 
                                "Hedgcapt" = c("0.02", "0.0", "0.04", "0.001", "0.02", 
                                               "0.0", "0.002", "0.0", "0.2", "0.03", 
                                               "0.004", "0.0", "0.35", "0.07", "0.5", 
                                               "0.015", "0.7"))

DOC_output_clean$Trap_type <- as.factor(DOC_output_clean$Trap_type)
DOC_output_clean$Hedgcapt <- as.numeric(DOC_output_clean$Hedgcapt)

DOC_output_clean %>%  
  ggplot(aes(x = reorder(Trap_type, -Hedgcapt), y= Hedgcapt)) +
  geom_point(aes(x = Trap_type), 
             position = position_jitterdodge(jitter.width = 4, jitter.height=0, 
                                             dodge.width = 0.9), 
             alpha = 0.2) +
  geom_bar(position="dodge", stat="summary", fun.y = 'mean', alpha = 0.7)+
  aes(color = Trap_type, fill = Trap_type) +
  geom_errorbar(stat='summary', position=position_dodge(0.9), width=0.4) +
  coord_cartesian(ylim = c(0,1)) +
  theme_classic() +
  theme(axis.text.x = element_text(angle = 90)) +
  xlab("\nTrap type") + ylab("Mean corrected trap catch\n") + 
  theme(legend.position = "none")

This is the result I get currently, and I want it to be re-ordered: result

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
patch105
  • 29
  • 6
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. But it does seem troublesome to have two different `aes(x=)` values but only one of them is reordered – MrFlick Aug 25 '20 at 03:55
  • That's it. I've added reorder to the second aes(x=). I'll edit my question to include your suggestions, thanks. – patch105 Aug 25 '20 at 04:00

1 Answers1

1

Here is the solution as pointed out by MrFlick (an extra reorder function in the geompoint aesthetic)

DOC_output_clean %>%  
  ggplot(aes(x = reorder(Trap_type, -Hedgcapt), y= Hedgcapt)) +
  geom_point(aes(x = reorder(Trap_type, -Hedgcapt), position = position_jitterdodge(jitter.width = 4, jitter.height=0, dodge.width = 0.9), alpha = 0.2) +
  geom_bar(position="dodge", stat="summary", fun.y = 'mean', alpha = 0.7)+
  aes(color = Trap_type, fill = Trap_type) +
  geom_errorbar(stat='summary', position=position_dodge(0.9), width=0.4) +
  coord_cartesian(ylim = c(0,1)) +
  theme_classic() +
  theme(axis.text.x = element_text(angle = 90)) +
  xlab("\nTrap type") + ylab("Mean corrected trap catch\n") + 
  theme(legend.position = "none")

and the output is as I wanted

patch105
  • 29
  • 6