15

我正在尝试使用 ggplot2 生成热图。我找到了这个例子,我基本上是想用我的数据来复制它,但我遇到了困难。我的数据是一个简单的 .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 中得到的当前输出如下所示:

在此处输入图像描述

4

2 回答 2

33

老实说@dr.bunsen - 你上面的例子很难重现,你没有阅读你链接的教程的第一部分。这可能是您正在寻找的内容:

 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")

在此处输入图像描述

于 2011-12-06T21:00:41.523 回答
1

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

使用gatherfrom tidyr,很容易重新格式化您的数据以获得预期的 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和.yfill

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

第一次尝试

好的还不错,但我们可以做得更好。

  • theme_bw()对于热图,我喜欢摆脱灰色背景的黑白主题。
  • 我也喜欢使用调色板RColorBrewer(使用direction = 1以获得更高值的较暗颜色,否则为 -1)。有很多可用的调色板:Reds、Blues、Spectral、RdYlBu(红-黄-蓝)、RdBu(红-蓝)等。下面我使用“Greens”。运行RColorBrewer::display.brewer.all()以查看调色板的外观。

  • 如果您希望瓷砖是方形的,只需使用 coord_equal().

  • 我经常发现图例没有用,但这取决于您的特定用例。您可以使用 隐藏fill图例guides(fill=F)

  • geom_text您可以使用(或geom_label)在图块顶部打印值。它需要美学xylabel在我们的例子中,xy继承的。size=count您还可以通过作为美学传递来打印更大的值- 在这种情况下,您还需要传递size=Fguides以隐藏尺寸图例。

  • 您可以通过传递 a 来在瓷砖周围绘制线条colorto geom_tile

把它们放在一起:

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")

最终热图

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

于 2018-12-04T14:13:16.717 回答