簡體   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