【发布时间】:2014-11-04 20:54:39
【问题描述】:
我想使用 ggplot2 创建一个对称的热图。 x 轴应显示与 y 轴完全相同的标签。不幸的是 ddply() 方法会影响订单。
input.csv 如下所示:
Names,Peter,Tom,Marc
Peter,1,6,1
Tom,2,4,12
Marc,3,0,21
到目前为止,我使用的是以下代码:
library(ggplot2)
library(plyr)
library(reshape2)
library (scales)
dat <- read.csv("input.csv")# read input
dat.m <- melt(dat)# to "melt" the dataset
dat.s <- ddply(dat.m, .(variable), transform, rescale = scale(value)) #pairwise format
file <- ggplot(dat.s, aes(Names,variable)) + geom_tile(aes(fill = value),colour = "white") + theme(axis.text.x = element_text(angle = 90, hjust = 1),legend.position="top")
pdf(file=paste("output",".pdf",sep="")) # write to file
plot(file)
# make plot
dev.off()
这会导致 Y 轴(从上到下)有标签 Marc-Tom-Peter,但 X 轴有标签(从左到右)Marc-Peter-Tom .
有谁知道,我如何实现一个绘图,两个轴的标签具有相同的(原始)顺序? (Peter,Tom,Marc),请注意,这只是一个玩具示例 - 真实数据有 100 多个标签,因此手动定义对无济于事。
提前致谢
【问题讨论】:
-
欢迎来到 SO。作为您提出的第一个问题,您已经很好地完成了许多事情。改善这一点的一种方法是通过添加数据使其可重现?这是在 SO 上提出问题的预期部分:stackoverflow.com/help/mcve