繁体   English   中英

如何使用 ggplot2 生成热图?

[英]How to produce a heatmap with ggplot2?

我正在尝试使用 ggplot2 生成热图。 我找到了这个 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 中获得的当前输出如下所示:

在此处输入图片说明

老实说@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")

在此处输入图片说明

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

使用从tidyr gather ,可以很容易地重新格式化您的数据以获得预期的 3 列(y 轴为person ,x 轴为fruitcountcount ):

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热图的基本 geom 是geom_tile ,我们将为其提供美学xyfill

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

第一次尝试

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

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

  • 如果您希望将瓷砖平方,只需使用coord_equal()

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

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

  • 您可以通过将color传递给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")

最终热图

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM