简体   繁体   English

在geom_tile图中的y轴上添加行名

[英]Adding row names on y-axis in geom_tile plot

I am plotting a data frame as a heatmap (using geom_tile) and I want the row names from my input matrix to be along the y-axis. 我正在将数据框绘制为热图(使用geom_tile),并且我希望输入矩阵中的行名称沿y轴。 Here is my code: 这是我的代码:

head(data)
                 0h_rep2   0h_rep1   1h_rep2   1h_rep1   4h_rep2   4h_rep1   9h_rep2  9h_rep1    15h_rep2  15h_rep1  18h_rep2  18h_rep1  21h_rep2  21h_rep1
hsa-let-7d-5p  11.485307 11.660549 11.383846 11.623624 11.395039 11.499499 11.660549 11.515537   11.697380 11.794755 11.803184 11.899935 12.225001 12.006919
hsa-let-7e-5p   10.660692 10.732285 10.869931 10.966696 10.984550 10.942785 10.966696 10.853484  10.963071 11.072640 11.315740 11.290014 11.643941 11.360874

pd <- as.data.frame(scale(t(data)))
pd$Time <- sub("", "", rownames(pd))
pd.m <- melt(pd)

head(pd.m)
 Time      variable      value
1 0h_rep2       11     -0.8495269
2 0h_rep1       11     -0.1199689
3 1h_rep2       11     -1.2719237

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_rep2", "0h_rep1","1h_rep2","1h_rep1","4h_rep2","4h_rep1","9h_rep2","9h_rep1", "15h_rep2", "15h_rep1","18h_rep2","18h_rep1","21h_rep2","21h_rep1")) + 
 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="")

Although we don't have your full data, if you have a data.frame to begin with and you simply remove the line 尽管我们没有完整的数据,但是如果您有一个data.frame开头,只需删除该行

pd.m$variable <- as.numeric(factor(pd.m$variable, levels = rev(as.character(unique(pd.m$variable))), ordered=F))

your code would seem to automatically label the y-axis with the row names - which become (factor) values of variable after melting. 您的代码似乎会自动用行名标记y轴-行名在融化后成为variable (因子)值。

在此处输入图片说明

library(reshape)
library(ggplot2)
library(scales)

pd <- as.data.frame(scale(t(data)))
pd$Time <- sub("", "", rownames(pd))
pd.m <- melt(pd)

> head(pd.m)
     Time      variable      value
1 0h_rep2 hsa-let-7d-5p -0.8495260
2 0h_rep1 hsa-let-7d-5p -0.1199692
3 1h_rep2 hsa-let-7d-5p -1.2719223

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_rep2","0h_rep1","1h_rep2","1h_rep1", "4h_rep2",
#                            "4h_rep1","9h_rep2","9h_rep1", "15h_rep2", "15h_rep1",
#                            "18h_rep2","18h_rep1","21h_rep2","21h_rep1")) +
  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="") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)

NOTE: I added the last line to rotate the x-axis labels, and also commented out your manual x scale because it seems to result in mismatched Time labels - but maybe that was intentional? 注意:我添加了最后一行来旋转x轴标签,并且还注释掉了您的手动x缩放比例,因为它似乎会导致Time标签不匹配-但这也许是故意的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM