2014-08-27 43 views
10

请参阅我下面的情节: enter image description hereCluster数据ggplot

我的代码:

> head(data) 
       X0  X1  X2  X3  X4  X5  X6  X7  X8  X9 
NM_001001144 6.52334 9.75243 5.62914 6.833650 6.789850 7.421440 8.675330 12.117600 11.551500 7.676900 
NM_001001327 1.89826 3.74708 1.48213 0.590923 2.915120 4.052600 0.758997 3.653680 1.931400 2.487570 
NM_001002267 1.70346 2.72858 2.10879 1.898050 3.063480 4.435810 7.499640 5.038870 11.128700 22.016500 
NM_001003717 6.02279 7.46547 7.39593 7.344080 4.568470 3.347250 2.230450 3.598560 2.470390 4.184450 
NM_001003920 1.06842 1.11961 1.38981 1.054000 0.833823 0.866511 0.795384 0.980946 0.731532 0.949049 
NM_001003953 7.50832 7.13316 4.10741 5.327390 2.311230 1.023050 2.573220 1.883740 3.215150 2.483410 

pd <- as.data.frame(scale(t(data))) 
pd$Time <- sub("_.*", "", rownames(pd)) 
pd.m <- melt(pd) 
pd.m$variable <- as.numeric(factor(pd.m$variable, levels =  rev(as.character(unique(pd.m$variable))), ordered=F)) 
p <- ggplot(pd.m, aes(Time, variable)) 
p + geom_tile(aes(fill = value)) + scale_fill_gradient2(low=muted("blue"), high=muted("red")) + 
    scale_x_discrete(labels=c("0h", "0.25h", "0.5h","1h","2h","3h","6h","12h","24h","48h")) + 
    theme_bw(base_size=20) + theme(axis.text.x=element_text(angle=0, vjust=0.5, hjust=0, size=12), 
    axis.text.y=element_text(size=12), strip.text.y=element_text(angle=0, vjust=0.5, hjust=0.5, size=12), 
    strip.text.x=element_text(size=12)) + labs(y="Genes", x="Time (h)", fill="") 

有没有办法集群的情节,这样的情节显示在动态时间过程。我想用所散发出来的集群:

hc.cols <- hclust(dist(t(data))) 

enter image description here

+0

仅供参考,我认为这是在上所以要加“解决”你的问题的标题皱起了眉头。我删除它。 – 2014-08-27 15:29:42

+0

谢谢! did not know – user3741035 2014-08-27 15:57:24

回答

9

您可以通过您已经应用hclust到数据后,在树状图确定时间点的顺序实现这一点:

data <- scale(t(data)) 
ord <- hclust(dist(data, method = "euclidean"), method = "ward.D")$order 
ord 
[1] 2 3 1 4 8 5 6 10 7 9 

你必须做的那么唯一的事情就是改变你的时间列到factor其中因子水平由ord下令:

pd <- as.data.frame(data) 
pd$Time <- sub("_.*", "", rownames(pd)) 
pd.m <- melt(pd, id.vars = "Time", variable.name = "Gene") 

pd.m$Gene <- factor(pd.m$Gene, levels = colnames(data), labels = seq_along(colnames(data))) 
pd.m$Time <- factor(pd.m$Time, levels = rownames(data)[ord], labels = c("0h", "0.25h", "0.5h","1h","2h","3h","6h","12h","24h","48h")) 

其余股份由ggplot自动完成

ggplot(pd.m, aes(Time, Gene)) + 
    geom_tile(aes(fill = value)) + 
    scale_fill_gradient2(low=muted("blue"), high=muted("red")) 

enter image description here

+0

真棒,正是我所赢得的。在整个数据集上看起来不错。 – user3741035 2014-08-27 14:03:07

+0

对不起,在第一个版本中的数据聚集在一起的基因。这现在已经修复。但请注意,时间轴当然不符合要求。所以也许你想按基因聚类,而不是按时间聚类。除非你不期望随着时间的推移反复出现的效果,这对我来说也更有意义。 – Beasterfield 2014-08-27 14:05:27

+0

我其实想要按时间聚集的版本。 – user3741035 2014-08-27 14:11:17

3

我不认为ggplot支持这一开箱即用,但可以使用heatmap

heatmap(
    as.matrix(dat), Rowv=NA, 
    Colv=as.dendrogram(hclust(dist(t(as.matrix(dat))))) 
) 

enter image description here

注意这看起来不像你的,因为我只是使用你的head数据,而不是整个事情。

在这里,我们手动指定具有从hclustColv参数派生的树状图的聚类。如果默认使用的参数与您想要的不一致,您也可以通过参数Colv手动指定群集。

+0

如何像上面的树状图一样设置Rowv。 – user3741035 2014-08-27 13:32:43

+0

尝试:'heatmap(as.matrix(dat),Rowv = NA,Colv = as.dendrogram(hclust(dist(t(as.matrix(dat)))))' – BrodieG 2014-08-27 13:39:03

+0

@ user3741035, 'Colv',而不是'Rowv' – BrodieG 2014-08-27 13:39:37