Reputation: 3
I am trying to create a correlation heat map using ggplot, but I cannot seem to control the order of my variables on the x or y axis. Specifally, ggplot seems to try to order the variables sequentially, but only operating on the first digit. Here's a simple example of the problem
x1<-c(1,2,3,4,5,6,6,7,4,5,6,3,4,5,6,7,8,9)
x2<-c(1,2,3,3,5,4,6,7,4,4,6,3,4,5,6,10,8,9)
x3<-c(2,2,3,5,5,4,6,4,4,4,6,3,4,5,6,10,8,9)
x4<-c(1,2,3,5,5,4,6,4,4,4,4,4,4,5,6,10,8,9)
x5<-c(1,2,3,3,5,4,6,7,4,4,6,3,4,5,6,10,8,9)
x10<-c(1,1,1,1,1,1,6,1,1,1,1,1,4,5,1,1,1,1)
new=data.frame(x1)
new$x2=x2
new$x3=x3
new$x4=x4
new$x5=x5
new$x10=x10
keep=melt(cor(new))
ggplot(keep,aes(Var1,Var2),xlab=NULL,ylab=NULL) + geom_tile(aes(fill = value),colour = "white") + scale_fill_gradient(low = "white",high = "steelblue")+theme(axis.title.x=element_blank(),axis.title.y=element_blank())
If you run this code you'll see that the x-axis is ordered
x1, x10, x2, x3, x4, x5
whereas I want it to read
x1, x2, x3, x4, x5, x10
Is there a way to specify this order?
Upvotes: 0
Views: 3187
Reputation: 459
Based on @Troy's answer I wrote a small function:
reorder_version_sorting <- function(col,prefix){
unique(col[order(as.numeric(gsub(prefix,"",col)))])
}
Usage:
ggplot +...+
scale_x_discrete(limits=reorder_version_sorting(dd2$Rep,"rep"))
Before:
"rep0" "rep10" "rep11" "rep12" "rep13" "rep14" "rep15" "rep16" "rep17" "rep18" "rep19" "rep1" "rep2" "rep3" "rep4" "rep5" "rep6" "rep7" "rep8" "rep9"
After:
"rep0" "rep1" "rep2" "rep3" "rep4" "rep5" "rep6" "rep7" "rep8" "rep9" "rep10" "rep11" "rep12" "rep13" "rep14" "rep15" "rep16" "rep17" "rep18" "rep19"
Upvotes: 0
Reputation: 42283
This works for me:
keep$Var1 <- factor(keep$Var1, levels = unique(keep$Var1), ordered = TRUE)
keep$Var2 <- factor(keep$Var2, levels = unique(keep$Var2), ordered = TRUE)
ggplot(keep,aes(Var1,Var2),xlab=NULL,ylab=NULL) + geom_tile(aes(fill = value),colour = "white") + scale_fill_gradient(low = "white",high = "steelblue")+theme(axis.title.x=element_blank(),axis.title.y=element_blank())
Upvotes: 1
Reputation: 8691
Try this if the padded zero solution doesn't fit for your purpose:
cust_breaks<-unique(keep$Var1[order(as.numeric(gsub("x","",keep$Var1)))])
ggplot(keep,aes(Var1,Var2),xlab=NULL,ylab=NULL) +
geom_tile(aes(fill = value),colour = "white") +
scale_fill_gradient(low = "white",high = "steelblue") +
theme(axis.title.x=element_blank(),axis.title.y=element_blank()) +
scale_x_discrete(limits=cust_breaks) +
scale_y_discrete(limits=cust_breaks)
Upvotes: 0
Reputation: 12664
For this particular example, one option would be to name your variables x01
, x02
, etc and then they will order correctly.
Upvotes: 0