Home > Mobile >  My data groups are not staying in order on my bar chart in R
My data groups are not staying in order on my bar chart in R

Time:02-13

I am making a bar chart with sd error bars on R but my Months (9, 10, 11, 12) won't stay in the correct order when placed on the graph. Does anybody know how to make it so they don't swap order?enter image description here

library(tidyverse)
library(RColorBrewer)
d <- structure(list(Month = c("9", "9", "9", "10", "10", "10", "11", "11", "11", "12", "12", "12"), genus = c("Crustacean", "Mollusc", "Seaweed", "Crustacean", "Mollusc", "Seaweed","Crustacean", "Mollusc", "Seaweed","Crustacean", "Mollusc", "Seaweed"), 
                    shannon = c(0,
                                1.1594324,
                                1.37245692,
                                0,
                                0.84868133,
                                1.54327422,
                                0.06432965,
                                1.1748941,
                                1.54612048,
                                0,
                                1.1045688,
                                1.7212854),
                    sd = c(0,
                           0.3288658,
                           0.7824814,
                           0,
                           0.8148536,
                           0.5015486,
                           0.1286593,
                           0.446148,
                           0.800392,
                           0,
                           0.1804363,
                           0.4421767)), 
               row.names = c(NA, -12L), class = "data.frame")

d %>%
  ggplot(aes(x = genus, y = shannon, fill = Month))  
  geom_col(width = 0.5,
           position = position_dodge(0.5))   
  geom_errorbar(aes(ymin = shannon - sd, ymax = shannon   sd),
                width = 0.2,
                position = position_dodge(0.5))  
  scale_fill_brewer(palette = "Blues")  
  theme_minimal()

CodePudding user response:

Since month is of type character, the automatic order is alphabetical. You can change this using reorder():

d %>%
  ggplot(aes(x = genus, y = shannon, fill = reorder(Month, as.numeric(Month))))  
  geom_col(width = 0.5,
           position = position_dodge(0.5))   
  geom_errorbar(aes(ymin = shannon - sd, ymax = shannon   sd),
                width = 0.2,
                position = position_dodge(0.5))  
  scale_fill_brewer(palette = "Blues", name = 'Month')  
  theme_minimal()

enter image description here

CodePudding user response:

Here comes a base R solution. Beforehand you should look where your months get coded as character, for this example we may convert it to numeric.

d$Month <- as.numeric(d$Month)

b <- barplot(shannon ~ Month   genus, d, beside=T, ylim=c(0, max(rowSums(d[3:4]))),
             col=hcl.colors(4, 'Blues', rev=T), legend.text=unique(d$Month), 
             args.legend=list(x='topleft', title='Month', bty='n'))
with(d[order(d$genus), ], arrows(b, shannon - sd, b, shannon   sd, code=3,
                                 angle=90, length=.05, xpd=TRUE)))

enter image description here

R will warn you that in this example some arrows have length zero and therefore aren't plotted.


Data:

d <- structure(list(Month = c("9", "9", "9", "10", "10", "10", "11", 
"11", "11", "12", "12", "12"), genus = c("Crustacean", "Mollusc", 
"Seaweed", "Crustacean", "Mollusc", "Seaweed", "Crustacean", 
"Mollusc", "Seaweed", "Crustacean", "Mollusc", "Seaweed"), shannon = c(0, 
1.1594324, 1.37245692, 0, 0.84868133, 1.54327422, 0.06432965, 
1.1748941, 1.54612048, 0, 1.1045688, 1.7212854), sd = c(0, 0.3288658, 
0.7824814, 0, 0.8148536, 0.5015486, 0.1286593, 0.446148, 0.800392, 
0, 0.1804363, 0.4421767)), row.names = c(NA, -12L), class = "data.frame")
  •  Tags:  
  • r
  • Related