How do I reverse the order in which error bars appear using geom_errorbar() in R?

Viewed 1181

I have the following example plot:

test <- data.frame("Factor" = as.factor(c("O", "C", "A")), 
                       b = c(0.18, .34, .65, -.13, .38, .26), 
                       lower95 = c(-.1, .09, .34, -.52, .10, -.02), 
                       upper95 = c(.48, .58, .98, .26, .67, .56), 
                       group = factor(c("Experiment 1","Experiment 2")))

test$Factor <- factor(test$Factor, as.character(test$Factor))
test$group <- factor(test$group, as.character(test$group))

ggplot(test, aes(Factor, b, colour = group)) +
  geom_errorbar(aes(ymin = lower95, ymax = upper95), 
            size = 1,
            width = .5, 
            position = 'dodge') +
  geom_hline(yintercept = 0) +
  ylim(-1.25, 1.25) +
  coord_flip() +
  theme_bw() + 
  ggtitle("Title") +
  theme(
axis.text=element_text(size = 20), 
axis.title=element_text(size = 18),
plot.title = element_text(size = 20, face = "bold"),
axis.text.y=element_text(size = 12)
  )

As you'll see, the error bars appear in the reverse order (from top to bottom) as they do in the legend. I would like Experiment 1 error bars to appear above Experiment 2 error bars.

I have tried

ggplot(test, aes(Factor, b, colour = forcats::fct_rev(groups)

But this reverses the order of the group labels in the legend, not the order of the colours in the legend–which is what would work. I have also tried reversing the order in which I enter them in the data frame and this does not solve the problem.

I would appreciate some help!

2 Answers

Re-factoring will change the order of the plot, but, as you saw, also changes the order of the legend. In addition to reversing the levels of group, you can reverse the order the legend is displayed with the reverse argument in guide_legend.

ggplot(test, aes(Factor, b, colour = forcats::fct_rev(group))) +
     geom_errorbar(aes(ymin = lower95, ymax = upper95), 
                   size = 1,
                   width = .5, 
                   position = 'dodge') +
     geom_hline(yintercept = 0) +
     ylim(-1.25, 1.25) +
     coord_flip() +
     theme_bw() + 
     ggtitle("Title") +
     theme(
          axis.text=element_text(size = 20), 
          axis.title=element_text(size = 18),
          plot.title = element_text(size = 20, face = "bold"),
          axis.text.y=element_text(size = 12)
     ) +
     guides(color = guide_legend(reverse = TRUE) )

enter image description here

If you are using scale_color_discrete or scale_color_manual to control other scale elements like the legend name, you can use guide_legend there instead of via guides.

+
scale_color_discrete(name = "Experiment", guide = guide_legend(reverse = TRUE) )

Do you mean something like this?

test$Factor <- factor(test$Factor, levels = rev(levels(test$Factor)));
test$group <- factor(test$group, levels = rev(levels(test$group)));

ggplot(test, aes(Factor, b, colour = group)) +
  geom_errorbar(aes(ymin = lower95, ymax = upper95),
            size = 1,
            width = .5,
            position = 'dodge') +
  geom_hline(yintercept = 0) +
  ylim(-1.25, 1.25) +
  coord_flip() +
  theme_bw() +
  ggtitle("Title") +
  theme(
axis.text=element_text(size = 20),
axis.title=element_text(size = 18),
plot.title = element_text(size = 20, face = "bold"),
axis.text.y=element_text(size = 12)
  )

enter image description here

I'm not entirely clear on whether you want to reverse the ordering of test$Factor as well; just (un)comment the corresponding line depending on what you're after.

Related