簡體   English   中英

如何使用ggplot2制作3個離散值的熱圖?

[英]How to make a heatmap of 3 discrete values using ggplot2?

我的aes參數有問題; 不知道它試圖告訴我如何解決。

我的數據框是這樣的:

> qualityScores

          Test1  Test2  Test3  Test4 Test5
Sample1      1     2      2      3     1
Sample2      1     2      2      3     2
Sample3      1     2      1      1     3
Sample4      1     1      3      1     1
Sample5      1     3      1      1     2

其中1表示通過,2表示警告,3表示失敗。

這里的dput我的數據:

structure(list(Test1 = c(1L, 1L, 1L, 1L, 1L), Test2 = c(2L, 2L, 
2L, 1L, 3L), Test3 = c(2L, 2L, 1L, 3L, 1L), Test4 = c(3L, 3L, 
1L, 1L, 1L), Test5 = c(1L, 2L, 3L, 1L, 2L)), .Names = c("Test1", 
"Test2", "Test3", "Test4", "Test5"), class = "data.frame", row.names = c("Sample1", 
"Sample2", "Sample3", "Sample4", "Sample5"))

我正在嘗試使用ggplots2創建一個熱圖,其中1將由gree表示,2將由黃色表示,3由紅色表示。

這是我的代碼:

samples <- rownames(qualityScores)
tests <- colnames(qualityScore)
testScores <- unlist(qualityScores) 

colors <- colorRampPalette(c("red", "yellow", "green"))(n=3)

ggplot(qualityScores, aes(x = tests, y = samples, fill = testScores) ) + geom_tile() + scale_fill_gradient2(low = colors[1], mid = colors[2], high = colors[3])

我收到此錯誤消息:

Error: Aesthetics must either be length one, or the same length as the dataProblems:colnames(SeqRunQualitySumNumeric)

我要去哪里錯了?

謝謝。

如果您將數據的格式從寬格式轉換為長格式,則將更加容易。 有很多方法可以做到這一點,但是在這里我使用了reshape2

library(reshape2); library(ggplot2)

colors <- c("green", "yellow", "red")

ggplot(melt(cbind(sample=rownames(qualityScores), qualityScores)), 
    aes(x = variable, y = sample, fill = factor(value))) + 
    geom_tile() + 
    scale_fill_manual(values=colors)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM