2013-07-19 74 views
0

我使用R: How do I display clustered matrix heatmap (similar color patterns are grouped)中提供的代碼成功提供,但是我無法用文本標籤代替Y軸,這可能嗎?R:ggplot2,將Y軸標籤替換爲geom_tile

library(reshape2) 
library(ggplot2) 

# Create dummy data 
set.seed(123) 
df <- data.frame(
    a = sample(1:5, 25, replace=TRUE), 
    b = sample(1:5, 25, replace=TRUE), 
    c = sample(1:5, 25, replace=TRUE) 
) 

# Perform clustering 
k <- kmeans(df, 3) 

# Append id and cluster 
dfc <- cbind(df, id=seq(nrow(df)), cluster=k$cluster) 

# Add idsort, the id number ordered by cluster 
dfc$idsort <- dfc$id[order(dfc$cluster)] 
dfc$idsort <- order(dfc$idsort) 

# use reshape2::melt to create data.frame in long format 
dfm <- melt(dfc, id.vars=c("id", "idsort")) 

ggplot(dfm, aes(x=variable, y=idsort)) + geom_tile(aes(fill=value)) 

回答

2

您可以使用scale_y_continuous()設置breaks=,然後提供labels=(例如只使用字母)。使用scale_...內部的參數expand=c(0,0)可以刪除繪圖中的灰色區域。

ggplot(dfm, aes(x=variable, y=idsort)) + geom_tile(aes(fill=value))+ 
    scale_x_discrete(expand=c(0,0))+ 
    scale_y_continuous(expand=c(0,0),breaks=1:25,labels=letters[1:25])