我正在将数据框绘制为热图(使用geom_tile),我希望输入矩阵中的行名称沿y轴。这是我的代码:
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="")
答案 0 :(得分:0)
虽然我们没有您的完整数据,但如果您开始使用data.frame
,则只需删除该行
pd.m$variable <- as.numeric(factor(pd.m$variable, levels = rev(as.character(unique(pd.m$variable))), ordered=F))
您的代码似乎会自动将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)
注意:我添加了最后一行来旋转x轴标签,并且还注释了你的手动x刻度,因为它似乎导致了不匹配的Time
标签 - 但这可能是故意的?