首页 文章

具有连续彩虹色的热图

提问于
浏览
1

首先,我必须说我在stackoverflow和其他地方读了很多关于heatmap和ggplot2的线程 . 但我的问题还没有解决 .

我有以下数据集:

Var1   Var2 value
1 -197.5 -197.5     0
2 -192.5 -197.5     0
3 -187.5 -197.5     0
4 -182.5 -197.5     0
5 -177.5 -197.5     0
6 -172.5 -197.5     0
.....

值应该是颜色,右侧的图例会很好 .

library(ggplot2)
ggheatmap <- ggplot(data = dat.plot, aes(x=Var1, y=Var2, fill=value)) + 
  geom_raster()+
  scale_fill_gradientn(colours=rainbow(100))+
  theme(axis.text.x = element_text(angle = 0))+ 
  coord_fixed()
print(ggheatmap)

结果是:

Plot/Heatmap

我希望有一个“正常”的彩虹刻度,从红色=高,橙色,黄色,绿色,浅蓝色,深蓝色=低,没有给出固定的离散颜色,例如,使用scale_fill_gradient2 . 我想知道为什么“彩虹”以红色开头=高端以及其他一些红色......

另一个问题:我如何添加一些东西来“平滑”热图,以便人们看不到任何地方的“边缘”?

3 回答

  • 1

    简短的回答:函数 rainbow() 在您通过 100 时会疯狂,因为您要求 100 不同的颜色 .

    你应该怎么做:将 n 传递给 rainbow() ,了解你想要的颜色数量 . 如果你想从蓝色变为红色,那么你还需要用函数 rev() 包装它 .

    library(egg)
    library(ggplot2)
    library(reshape2)
    
    # Heatmap number of rows/columns
    Nvalue <- 1e2
    # n for colors passed to function rainbow
    nColor <- c(1:10, seq(20, 100, 20))
    # dummy data
    df <- melt(matrix(rnorm(N^2), N))
    
    plotList <- list()
    for(i in seq_along(nColor)) {
        plotList[[i]] <- ggplot(df, aes(Var1, Var2, fill = value)) + 
            geom_raster() +
            scale_fill_gradientn(colours = rev(rainbow(nColor[i]))) +
            labs(title = paste0("rainbow(", nColor[i], ")"),
                 x = NULL,
                 y = NULL,
                 fill = NULL) +
            theme_void()
    }
    
    ggarrange(plots = plotList)
    

    enter image description here

    编辑:

    在OP指定颜色后,他想要传递十六进制矢量应该工作:

    hex <- c("#FF0000", "#FFA500", "#FFFF00", "#008000", "#9999ff", "#000066")
    ggplot(df, aes(Var1, Var2, fill = value)) + 
            geom_raster() +
            scale_fill_gradientn(colours = rev(hex)) +
            labs(x = NULL,
                 y = NULL,
                 fill = NULL) +
            theme_void()
    

    enter image description here

  • 0
    ggheatmap <- ggplot(data = dat.plot, aes(x=Var1, y=Var2, fill=value)) + 
      geom_raster(interpolate=TRUE)+
      scale_fill_gradientn(colors=rev(c("darkred", "red", "orange", "yellow",    "green", "lightgreen", "lightblue", "darkblue")))+
      theme(axis.text.x = element_text(angle = 0))+ 
      coord_fixed()
    print(ggheatmap)
    

    现在看起来很模糊 . 但是没问题 . 如果你认为它不能做得更好,我就让它原样 . 非常感谢你!

  • 0

    在“旧忠实”数据中最好地说明了用复杂色标内插颜色的问题 .

    如果使用用户定义的“彩虹”色标,插值将不会那么好(即使结果看起来仍然很好) .

    没有插值

    library(ggplot2)
    cols <- rev(rainbow(7)[-7])
    
    ggplot(faithfuld, aes(waiting, eruptions)) +
      geom_raster(aes(fill = density), interpolate = FALSE) +
      scale_fill_gradientn(colours = cols)
    

    Without interpolation

    用插值

    ggplot(faithfuld, aes(waiting, eruptions)) +
      geom_raster(aes(fill = density), interpolate = TRUE) +
      scale_fill_gradientn(colours = cols)
    

    With interpolation

    使用插值和不太复杂的调色板

    ggplot(faithfuld, aes(waiting, eruptions)) +
      geom_raster(aes(fill = density), interpolate = TRUE) +
      scale_fill_gradientn(colours = c("steelblue", "tomato"))
    

    Two color palette

相关问题