代码之家  ›  专栏  ›  技术社区  ›  Daniel Valencia C.

如何根据R中的矩阵绘制热图或等高线图?[重复]

  •  0
  • Daniel Valencia C.  · 技术社区  · 6 年前

    我正在尝试使用ggplot2生成热图。我发现 this example ,我基本上是想用我的数据复制它,但我遇到了困难。我的数据很简单。csv文件,如下所示:

    people,apple,orange,peach
    mike,1,0,6
    sue,0,0,1
    bill,3,3,1
    ted,1,1,0
    

    我想制作一个简单的热图,其中水果的名字在x轴上,人在y轴上。图表应描绘正方形,其中每个正方形的颜色表示食用水果的数量。对应于 mike:peach 应该是最黑暗的。

    下面是我用来生成热图的代码:

    data <- read.csv("/Users/bunsen/Desktop/fruit.txt", head=TRUE, sep=",")
    fruit <- c(apple,orange,peach)
    people <- data[,1]
    (p <- ggplot(data, aes(fruit, people)) + geom_tile(aes(fill = rescale), colour = "white") +    scale_fill_gradient(low = "white", high = "steelblue"))
    

    当我绘制这些数据时,我在x轴上得到水果的数量,在y轴上得到人的数量。我也没有得到代表水果数量的颜色渐变。我怎样才能得到x轴上水果的名称,以及一个人吃的水果的数量,显示为热图? 在R中得到的电流输出如下所示:

    enter image description here

    0 回复  |  直到 13 年前
        1
  •  33
  •   Max Ghenis shoyer    9 年前

    老实说@dr.bunsen-你上面的例子很难复制,而且你没有阅读你所阅读的教程的第一部分 linked .以下可能是你想要的:

     library(reshape)
     library(ggplot2)
     library(scales)
    
     data <- structure(list(people = structure(c(2L, 3L, 1L, 4L), 
                                               .Label = c("bill", "mike", "sue", "ted"), 
                                               class = "factor"), 
                            apple = c(1L, 0L, 3L, 1L), 
                            orange = c(0L, 0L, 3L, 1L), 
                            peach = c(6L, 1L, 1L, 0L)), 
                        .Names = c("people", "apple", "orange", "peach"),
                        class = "data.frame", 
                        row.names = c(NA, -4L))
     data.m <- melt(data)
     data.m <- ddply(data.m, .(variable), transform, rescale = rescale(value))
     p <- ggplot(data.m, aes(variable, people)) + 
             geom_tile(aes(fill = rescale), colour = "white") 
     p + scale_fill_gradient(low = "white", high = "steelblue")
    

    enter image description here

        2
  •  1
  •   asachet    6 年前

    七(!)多年后,正确格式化数据的最佳方法是使用 tidyr 而不是 reshape

    使用 gather 从…起 蒂蒂尔 ,很容易重新格式化数据,以获得预期的3列( person 对于y轴, fruit 对于x轴和 count 对于值):

    library("dplyr")
    library("tidyr")
    
    hm <- readr::read_csv("people,apple,orange,peach
    mike,1,0,6
    sue,0,0,1
    bill,3,3,1
    ted,1,1,0")
    
    hm <- hm %>%
      gather(fruit, count, apple:peach)
      #syntax: key column (to create), value column (to create), columns to gather (will become (key, value) pairs)
    

    现在的数据如下所示:

    # A tibble: 12 x 3
       people fruit  count
       <chr>  <chr>  <dbl>
     1 mike   apple      1
     2 sue    apple      0
     3 bill   apple      3
     4 ted    apple      1
     5 mike   orange     0
     6 sue    orange     0
     7 bill   orange     3
     8 ted    orange     1
     9 mike   peach      6
    10 sue    peach      1
    11 bill   peach      1
    12 ted    peach      0
    

    完美的我们开始策划吧。使用ggplot2绘制热图的基本几何图形是 geom_tile 我们将为其提供美学 x , y fill .

    library("ggplot2")
    ggplot(hm, aes(x=x, y=y, fill=value)) + geom_tile() 
    

    first attempt

    好吧,不算太糟,但我们可以做得更好。

    • 对于热图,我喜欢黑色&白色主题 theme_bw() 去除了灰色背景。
    • 我还喜欢使用来自 RColorBrewer (与 direction = 1 以获得更高值的深色,或-1)。有很多可用的调色板:红色、蓝色、光谱、RdYlBu(红黄蓝)、RdBu(红蓝)等。下面我使用“绿色”。跑 RColorBrewer::display.brewer.all() 看看调色板是什么样子。

    • 如果你想让瓷砖成正方形,只需使用 coord_equal() .

    • 我经常发现图例没有用处,但它取决于您的特定用例。你可以藏起来 填满 传奇 guides(fill=F) .

    • 您可以使用 geom_text (或 geom_label )1.需要美学 十、 , Y label 但就我们而言, 十、 Y 都是遗传的。也可以通过传递来打印更大的值 size=count 作为一种审美观——在这种情况下,你也会想通过考试 size=F guides 隐藏尺寸图例。

    • 你可以通过传递一条线在瓷砖周围画线 color geom_瓷砖 .

    总而言之:

    ggplot(hm, aes(x=fruit, y=people, fill=count)) +
      # tile with black contour
      geom_tile(color="black") + 
      # B&W theme, no grey background
      theme_bw() + 
      # square tiles
      coord_equal() + 
      # Green color theme for `fill`
      scale_fill_distiller(palette="Greens", direction=1) + 
      # printing values in black
      geom_text(aes(label=count), color="black") +
      # removing legend for `fill` since we're already printing values
      guides(fill=F) +
      # since there is no legend, adding a title
      labs(title = "Count of fruits per person")
    

    Final heatmap

    要删除任何内容,只需删除相应的行。