-2

so I'm having issues with the order of my bars in my bar chart where I want all of the functional groups (fu5) to be aligned beside each other, I've looked into levels and all other forms but I honestly can't figure out why they don't align with there groups.

enter image description here

Here is the code I used so far (I'm using RStudio, and a csv file uploaded to it):

ggplot(Ot, aes(x = sp5, y = o5, fill = fu5)) + 
    geom_col() + 
    theme(axis.text.x = element_text(angle = 60, hjust = 1))

Any help would be great.

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
  • Welcome @Elanor . It's always good to provide sample data. Look at https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example for posting good questions – Andrew Lavers Mar 15 '18 at 23:07

1 Answers1

0

The reorder function can do this so long as fu5 is a factor. But note the as.integer to retrieve the numeric ordering of the factor.

library(ggplot2)
df = read.csv(text="
sp5,fu5,o5
spa,c,1
spd,c,2
spb,a,3
spc,b,4
", stringsAsFactors = TRUE)

ggplot(df, aes(x = reorder(sp5, as.integer(fu5)), y = o5, fill = fu5)) + 
  geom_col() + 
  theme(axis.text.x = element_text(angle = 60, hjust = 1))

enter image description here

Andrew Lavers
  • 4,328
  • 1
  • 12
  • 19