0
votes

I'm using ggplot to make bar graphs of data. I have everything ordered as I want it in my data frame but when I plot it, it is rearranged in some random order. How do I tell it to leave the bars in the order of the data frame? Thank you

iris Example below. In iris data frame species are ordered setosa, versicolor, virginica. They are ordered in reverse on the bar plot. It appears to be descending. This is not the case with my plots. Mine are in no obvious order.

ggplot(iris[iris$Sepal.Width ==3.0,]) + aes(x=Species, y=Sepal.Length) + geom_col() + coord_flip()
1
Welcome to SO! Please see how to make a great R reproducible example and edit your question so the community can best help you. To start, consider using dput() to share some of your data.OTStats
How did you arrange your dataframe ? did you convert it in a factor format ? Using the iris dataset, I"m not able to reproduce your error. Please provide more information about your dataset and your code.dc37
Not sure what factor form means. Sorry, I'm still new to R. My data frame is ordered with species in the first column followed by the combined column (site data) and then abundance column (the numerical data). The data frame is in long format with species repeated in each of the sites. So the data are ordered by site.bob ox
Please share an example of your data. It will make easier to assist you. Seee @OTStats's comment for how to provide a reproducible exampledc37
Does this answer your question? Order Bars in ggplot2 bar graphstefan

1 Answers

0
votes

If you want to reorder the columns, try this

ggplot(iris[iris$Sepal.Width ==3.0,]) + aes(x= factor(Species, levels = c("versicolor", "setosa", "virginica")), y=Sepal.Length) + geom_col() + coord_flip() + xlab("Species")