使用ggplot在相关热图上排序x轴 [英] Ordering x-axis on correlation heat map using ggplot

查看:151
本文介绍了使用ggplot在相关热图上排序x轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用ggplot创建相关热图,但似乎无法控制x或y轴上变量的顺序。具体来说,ggplot似乎试图按顺序排列变量,但只能在第一个数字上进行操作。这是一个简单的问题例子。

  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)

×3 <-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)

×5 <-c(1,2,3,3,5,4,6,7,4, 4,6,3,4,5,6,10,8,9)

×10 <-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

新$ x3 = x3

新$ x4 = x4

新$ x5 = x5

新$ x10 = x10

keep = melt(cor(new))

ggplot(aes(Var1,Var2),xlab = NULL,ylab = NULL)+ geom_tile(aes(fill = value ),color =white)+ scale_fill_gradient(low =white,high =steelblue)+ theme(axis.title.x = element_blank(),axis.title.y = element_blank())

如果您运行此代码,您将看到x轴已订购

  x1,x10,x2,x3,x4,x5 

而我想要它阅读

  x1,x2,x3,x4,x5,x10 
pre>

有没有一种方法可以指定这个顺序?

解决方案

  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),color =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)


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?

解决方案

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)

这篇关于使用ggplot在相关热图上排序x轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆