I have read many posts on here about controlling the order of axis's when using GGPLOT2 in R and overriding the default 1.2.3... and alphabetical order of the points. At least based on my current understanding of R these examples are not meeting my requirements.
I want to display the days of the week in a particular order picking which day of the week I want to be the first day of the week, sometimes Sunday and other times Monday for example. The same could be applied to multiple other examples.
All of the examples I have found on here fall into one of two categories, neither of which meet my needs.
Using the order in the file (such as if B is the first value it will display before A then C) OR
List change the sort to ascending or descending based on the order.
Some of the others I specifically referenced (which maybe I missed something?).
- How do you specifically order ggplot2 x axis instead of alphabetical order?
- controlling order of points in ggplot2 in R?
- Ordering bar plots with ggplot2 according to their size, i.e. numerical value
- Specifying plot order with ggsubplot
- Order of legend entries in ggplot2 barplots with coord_flip()
- Control ggplot2 legend look without affecting the plot
I also was reading on this cheat sheet under "Create a tiled correlation plot (geom_tile())" which looks like they say they are ordering for the final output but their example graphic does not display them in this order but in the default alphabetical.
#careful, I'm sorting the field names so that the ordering in the final plot is correct
thecor<-round(cor(nmmaps[,sort(c("death", "temp", "dewpoint", "pm10", "o3"))], method="pearson", use="pairwise.complete.obs"),2)
As far as reproducabile, this is technically a follow-up to this question, which after completing the describe step their my data looks like the following:
DofM DofW newvol
1 1 Fri 7412236
2 1 Mon 6713342
3 1 Sat 7138702
4 1 Sun 6246188
5 1 Thu 6756753
6 1 Tue 6817368
7 1 Wed 6098822
8 2 Fri 7688617
9 2 Mon 7073268
10 2 Sat 6777736
My code to produce the graphic looks like this:
>p<-ggplot(AC4,aes(DofM,DofW))
>p + geom_tile(aes(fill=newvol)) + scale_fill_gradient2(low="white", mid="orange", high="darkred",na.value = "grey50", midpoint=6000000, breaks=c(4000000,4500000,5000000,5500000,6000000,6500000,7000000,7500000,8000000)) + xlab("Day of Month") + ylab("Day of Week") + scale_x_continuous(expand = c(0,0), breaks = 0:31)
Which produces the following graphic but my days of the week are out of order.
I need them to be in a specific order of the Y-axis to "Mon","Tue","Wed","Thu","Fri","Sat","Sun", not "Wed", "Tue", "Thu", "Sun", "Sat", "Mon", "Fri". The X axis auto-order is just fine. How do I do this?