Reputation: 2545
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. 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="")
Upvotes: 1
Views: 1779
Reputation: 785
Although we don't have your full data, if you have a data.frame
to begin with and you simply remove the line
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.
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?
Upvotes: 1