【问题标题】:symmetric (same axis) Heatmap with ggplot2具有ggplot2的对称(同轴)热图
【发布时间】: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

标签: r plot ggplot2


【解决方案1】:

首先创建一个按您喜欢的方式排列的名称向量:

lvls <- as.character(dat$Names)

下一个订单variable 所以它匹配Names:

dat.s$variable <- factor(dat.s$variable, levels = lvls)

现在尝试绘图。

【讨论】:

    【解决方案2】:

    您也可以将limits 添加到您的体重秤中。请注意,默认是从下到上,所以如果我理解正确,您还必须使用rev 来颠倒顺序。这是一个可能的解决方案:

    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") +
      scale_x_discrete(limits = dat$Names) +
      scale_y_discrete(limits = rev(dat$Names)) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-14
      • 2017-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 2019-11-06
      • 1970-01-01
      相关资源
      最近更新 更多