0

I have a very simple problem. I'm making a simple dodged bar chart in ggplot2 in R, and my measures are not in the same order in the plot and in the legend. As can be seen in the example plot, measure A in salmon is on the bottom in the chart, but on the top in the legend. I'd like them to be ordered the same in both places.

enter image description here Any advice? I've tried playing around with the ordering of the fill measure, but doesn't make a difference. Here's the R code:

ggplot(out2, aes(x = Variable, y=est,fill=Measure)) +
  geom_bar(position = "dodge", stat="identity",color="black")+
  geom_errorbar(aes(x=Variable,ymin=lo,ymax=hi),width=.2,position=position_dodge(.9))+
  theme_classic()+coord_flip()+ylab("Standardized Estimate")+xlab("Measure")

The data:

Variable    Measure est lo  hi 
Var1    A   -0.22046025 -0.242941124    -0.19797938
Var1    B   -0.4416939  -0.464179242    -0.41920856
Var1    C   -0.05870968 -0.079757151    -0.0376622
Var1    D   -0.09956618 -0.121966337    -0.07716603
Var1    E   -0.21761678 -0.237557365    -0.19767619
Var2    A   0.15310304  0.130995958 0.17521013
Var2    B   0.08317558  0.059667487 0.10668366
Var2    C   0.19167363  0.171662536 0.21168473
Var2    D   0.19607088  0.174750576 0.21739118
Var2    E   0.12307817  0.102282982 0.14387336
Var3    A   0.01874422  -0.007943041    0.04543148
Var3    B   0.09679525  0.068783593 0.12480691
Var3    C   0.17800168  0.153945969 0.20205739
Var3    D   -0.06815006 -0.092771073    -0.04352905
Var3    E   0.10935194  0.092778676 0.1259252
Var4    A   0.02119417  -0.005383474    0.04777182
Var4    B   0.20175 0.174170789 0.22932921
Var4    C   0.28234648  0.258946192 0.30574677
Var4    D   0.27383735  0.24550973  0.30216497
Var4    E   0.12060643  0.095333034 0.14587983
Var5    A   -0.40312318 -0.428041245    -0.37820512
Var5    B   -0.62223812 -0.646148035    -0.5983282
Var5    C   -0.24178985 -0.265545315    -0.21803439
Var5    D   -0.25663925 -0.281690564    -0.23158794
Var5    E   -0.2504907  -0.274898158    -0.22608324
Var6    A   0.08896879  0.059268463 0.11866912
Var6    B   0.12772646  0.099299213 0.15615371
Var6    C   0.10013601  0.075418108 0.12485392
Var6    D   0.02986901  0.006681256 0.05305677
Var6    E   0.12655356  0.107467398 0.14563971
Var7    A   0.36395987  0.340471818 0.38744791
Var7    B   0.59798475  0.575777017 0.62019248
Var7    C   0.30156148  0.279785839 0.32333712
Var7    D   0.3636511   0.338241065 0.38906113
Var7    E   0.29686526  0.273368812 0.3203617
Var8    A   0.05732388  0.032874273 0.08177348
Var8    B   0.16576558  0.140281883 0.19124928
Var8    C   0.05272789  0.030332088 0.07512368
Var8    D   0.10909782  0.077128721 0.14106691
Var8    E   0.12067134  0.096529477 0.14481321
  • 1
    I think it is because, on the Y axis.. higher Y values (Level E) goes to the top and lower Y value (Level A) goes to the bottom. The following might work. But it is hard to say without a reproducible example. ggplot(out2, aes(x = Variable, y=est ,fill=fact_rev(Measure))) – Seshadri Jan 28 '20 at 18:17
  • @Seshadri that just changes the order of the variables in the plot, but doesn't align the legend with the bars. I posted the data. – robin.datadrivers Jan 28 '20 at 18:28
  • 1
    By the way refer to this link for a better way to provide reproducible examples (dput etc.): https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Seshadri Jan 28 '20 at 19:40

2 Answers2

2

There are three main ways that I could find to do this:

  1. Change it in your ggplot2 code using guide = guide_legend(reverse=TRUE). Source here
  2. Change it in your ggplot2 code using aes(..., order=-Measure). The - in serves to reverse the variable, such as when you use order(-x) to sort a data table by x in reverse. Source here
  3. Reverse the order in your data management, e.g. out2$Measure <- factor(out2$Measure, levels = rev(levels(out2$Measure) but that is more complicated. Source here
1

Here you go. I used position_dodge2(reverse = TRUE), instead of position_dodge. That is the least intrusive way of doing this I guess.

library(tidyverse)
text1 <- "Variable    Measure est lo  hi 
Var1    A   -0.22046025 -0.242941124    -0.19797938
Var1    B   -0.4416939  -0.464179242    -0.41920856
Var1    C   -0.05870968 -0.079757151    -0.0376622
Var1    D   -0.09956618 -0.121966337    -0.07716603
Var1    E   -0.21761678 -0.237557365    -0.19767619
Var2    A   0.15310304  0.130995958 0.17521013
Var2    B   0.08317558  0.059667487 0.10668366
Var2    C   0.19167363  0.171662536 0.21168473
Var2    D   0.19607088  0.174750576 0.21739118
Var2    E   0.12307817  0.102282982 0.14387336
Var3    A   0.01874422  -0.007943041    0.04543148
Var3    B   0.09679525  0.068783593 0.12480691
Var3    C   0.17800168  0.153945969 0.20205739
Var3    D   -0.06815006 -0.092771073    -0.04352905
Var3    E   0.10935194  0.092778676 0.1259252
Var4    A   0.02119417  -0.005383474    0.04777182
Var4    B   0.20175 0.174170789 0.22932921
Var4    C   0.28234648  0.258946192 0.30574677
Var4    D   0.27383735  0.24550973  0.30216497
Var4    E   0.12060643  0.095333034 0.14587983
Var5    A   -0.40312318 -0.428041245    -0.37820512
Var5    B   -0.62223812 -0.646148035    -0.5983282
Var5    C   -0.24178985 -0.265545315    -0.21803439
Var5    D   -0.25663925 -0.281690564    -0.23158794
Var5    E   -0.2504907  -0.274898158    -0.22608324
Var6    A   0.08896879  0.059268463 0.11866912
Var6    B   0.12772646  0.099299213 0.15615371
Var6    C   0.10013601  0.075418108 0.12485392
Var6    D   0.02986901  0.006681256 0.05305677
Var6    E   0.12655356  0.107467398 0.14563971
Var7    A   0.36395987  0.340471818 0.38744791
Var7    B   0.59798475  0.575777017 0.62019248
Var7    C   0.30156148  0.279785839 0.32333712
Var7    D   0.3636511   0.338241065 0.38906113
Var7    E   0.29686526  0.273368812 0.3203617
Var8    A   0.05732388  0.032874273 0.08177348
Var8    B   0.16576558  0.140281883 0.19124928
Var8    C   0.05272789  0.030332088 0.07512368
Var8    D   0.10909782  0.077128721 0.14106691
Var8    E   0.12067134  0.096529477 0.14481321
"
out2  <-  read.table(text = text1, header = T)
ggplot(out2, aes(x = Variable, y=est, fill=Measure)) +
  geom_bar(position = position_dodge2(reverse = TRUE), stat="identity",color="black")+
  geom_errorbar(aes(x=Variable,ymin=lo,ymax=hi), position = position_dodge2(width=1, padding = 1, rev = TRUE))+
  theme_classic()+coord_flip()+ylab("Standardized Estimate")+xlab("Measure")

Which will give you:

enter image description here

Seshadri
  • 669
  • 3
  • 11