0
votes

I've created two grouped barplots from the same dataframe, and I'm having the same problem with both. In the first plot, I've grouped data by category (fields in this case) on the x axis, and filled each bar by year, which I've also categorized withfill=as.factor(Year). On the second graph I've switched the variables that are visualized on the x axis and the color of the bars (fill). In other words, the data is grouped by year on the x axis, and filled according to field (fill=as.factor(fnum)). It appears ggplot is ordering the bars based on the y values, but I'm trying to order the data consistenty in categorical, logical way (by year or field). Is there any way to specify order, either in the code for the plots or in the way I structure my dataframe? Thanks.

Code 1:

ggplot(data=OM, aes(factor(fnum), y=Value, fill=as.factor(Year))) + 
  geom_bar(stat="identity", position = "dodge")+
  labs(x='Field', y='Soil Organic Matter %', fill='Year',
    title = 'Organic Matter Plotted by Field and Year')+
  theme(axis.text.x = element_text(angle=65, vjust=0.6))

First plot

Code 2:

ggplot(data=OM, aes(factor(Year), y=Value, fill=as.factor(fnum))) + 
  geom_bar(stat="identity", position = "dodge",color='black')+
  labs(x='Year', y='Soil Organic Matter %', fill='Field',
       title = 'Organic Matter Plotted by Field and Year')+
  theme(axis.text.x = element_text(angle=65, vjust=0.6))

Second plot

2
Hmmm... Can you dput(OM) ? I am really puzzled how you get this.StupidWolf
Please read how to make a great reproducible example. Providing such an example makes it much more likely that you receive a useful answer.Axeman
Thank you @Axeman, I'll do that in the future.nbeck1999
@StupidWolf I see that dput() "Writes an ASCII text representation of an R object to a file or connection, or uses one to recreate the object." but I'm wondering if you could give some clarification of how that is helpful/useful - I'm new to stackoverflow so not quite sure how that function's used in this context.nbeck1999
Glad that my answer help you to figure it out the problem in your data. As @Axeman suggested it, I think you factored y values was a big issue for the plotting. As you are a new contributor, you should check the Help Center: stackoverflow.com/help/asking and also stackoverflow.com/help/someone-answersdc37

2 Answers

1
votes

Your data are not organized by y values, if you look at the order of Field, on both plot, they are following the same order. r will automatically order factor vector in an ascending way (1 -> 9, A -> Z), so that's why your PRF values seems mis-ordered.

If you want to have a customed order, you could set it before using ggplot like this:

OM$fnum <- factor(OM$fnum, levels = c("PRF1","PRF1-2","PRF2","PRF3","PRF4","PRF5-1","PRF5-3","PRF6","PRF7","PRF8","PRF9","PRF10","PRF12"

Then, it should plot everything consistently using the order you specified.

Your column Years is already order in the ascending order as 2015 is before 2017 before 2019. So no need to change it.

Does it answer your question ? If not, you should consider to provide a reproducible example of your dataset (see here: How to make a great R reproducible example)


PS: BTW, it seems that your column Value used as y in your plots is in a factor format. Is it intentional ?

0
votes

Try something like this before your code:

OM$fnum <- factor(OM$fnum, levels = c("PRF1", "PRF12", "PRF2", "PRF3", 
                                    "PRF5-1", "PRF5-3", "PRF6", "PRF7", "PRF8", "PRF9", "PRF10", "PRF12"))