1

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.

  1. Using the order in the file (such as if B is the first value it will display before A then C) OR

  2. List change the sort to ascending or descending based on the order.

Some of the others I specifically referenced (which maybe I missed something?).

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?

enter image description here

Community
  • 1
  • 1
CRSouser
  • 658
  • 9
  • 25
  • 1
    You just need your data column to be a `factor` that has the levels in the correct order. `AC4$DofW = factor(AC4$DofW, levels = c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")`. (As with numbers, the default is to start from the first on at the origin and go up, reverse the order if you prefer to start at the top and go down.) – Gregor Thomas Apr 01 '15 at 23:25
  • And, there's an example of this in the first answer to the first question you link (suggested duplicate), there the variable is treatment and the desired order is Y, X, Z, and the code is `data$Treatment <- factor(data$Treatment, levels=c("Y", "X", "Z"))` – Gregor Thomas Apr 01 '15 at 23:27
  • But you're right that the blog entry claims to demo this, but doesn't do it correctly or produce a correct plot. – Gregor Thomas Apr 01 '15 at 23:29
  • @Gregor I saw that but how it was written it wasn't clear on how to apply it (still a newbie).. I played with it a bit based on your first comment and it is now more clear. I can delete it if you believe appropriate as it is now coming out correctly. – CRSouser Apr 01 '15 at 23:34

0 Answers0