【问题标题】:ggplot heatmap where axis scaling is proportional to other variablesggplot 热图,其中轴缩放与其他变量成比例
【发布时间】:2020-09-01 22:02:45
【问题描述】:

我找到了这个图:Heatmap with variable box sizes 在这篇文章中:https://journals.sagepub.com/doi/10.1177/2378023118805646

有谁知道如何制作盒子大小不同的热图?

这个

# Library
library(ggplot2)

# Dummy data
x <- LETTERS[1:20]
y <- paste0("var", seq(1,20))
data <- expand.grid(X=x, Y=y)
data$Z <- runif(400, 0, 5)

# Heatmap 
ggplot(data, aes(X, Y, fill= Z)) + 
  geom_tile()

给了我一个热图,但不知道如何修复坐标轴比例,以便它们遵循不同的变量。

谢谢 哈桑

【问题讨论】:

    标签: r ggplot2 heatmap


    【解决方案1】:

    可以通过geom_rect 获得具有不同框大小的热图。根据您的数据,这需要或多或少的手动工作来设置或计算框的边界,即用于xminxmaxyminymax 美学的值。试试这个:

    # Library
    library(ggplot2)
    
    set.seed(42)
    
    # Dummy data
    x <- LETTERS[1:5]
    y <- paste0("var", seq(1,5))
    # Lower and upper bounds of rectangles
    xx <- setNames(cumsum(runif(5)), x)
    xx1 <- setNames(c(0, xx[1:4]), x)
    yy <- setNames(cumsum(runif(5)), y)
    yy1 <- setNames(c(0, yy[1:4]), y)
    data <- expand.grid(X=x, Y=y)
    data$Z <- runif(25, 0, 5)
    data$xmax <- xx[data$X]
    data$ymax <- yy[data$Y]
    data$xmin <- xx1[data$X]
    data$ymin <- yy1[data$Y]
    
    # Heatmap 
    ggplot(data, aes(fill= Z)) + 
      geom_rect(aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax), color = "white") +
      scale_x_continuous(breaks = (xx1 + xx) / 2, labels = x) +
      scale_y_continuous(breaks = (yy1 + yy) / 2, labels = y) +
      scale_fill_viridis_b()
    

    reprex package (v0.3.0) 于 2020-05-16 创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-31
      • 1970-01-01
      • 2012-09-24
      • 2012-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-29
      相关资源
      最近更新 更多