如何使用ggplot2制作3个离散值的热图? [英] How to make a heatmap of 3 discrete values using ggplot2?

查看:760
本文介绍了如何使用ggplot2制作3个离散值的热图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对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
样本5 1 3 1 1 2

其中1代表PASS,2对于WARN和3对于FAIL。

以下是我的数据的 dput


1

 结构(列表(Test1 = c(1L,1L,1L,1L,1L),Test2 = c(2L,2L,
2L,测试3 = c(2L,2L,1L,3L,1L),测试4 = c(3L,3L,
1L,1L,1L),测试5 = c(1L,2L,3L,1L ,2L)),.Names = c(Test1,
Test2,Test3,Test4,Test5),class =data.frame,row.names = c( Sample1,
Sample2,Sample3,Sample4,Sample5))

我试图制作一个热图,其中1代表gree,2代表黄色,3代表使用ggplots2代表红色。

这是我的代码:

  samples <-rownames(qualityScores)
tests < - colnames(qualityScore)
testScores< - unlist(品质分数)

颜色< - colorRampPalette(c(红色,黄色,绿色))(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])
  

>错误:美学必须是长度为1或与dataProblems相同的长度:colnames(SeqRunQualitySumNumeric)

我在哪里出错了?



谢谢。

解决方案

如果您将数据从宽转换为长格式,这将更容易。有很多方法可以做到,但在这里我使用了 reshape2

  library (reshape2);库(ggplot2)

颜色< -c(green,yellow,red)

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


I have a problem with the aes parameters; not sure what it's trying to tell me to fix.

My data frame is like so:

> 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

Where 1 stands for PASS, 2 for WARN, and 3 for FAIL.

Here's the dput of my data:

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

I am trying to make a heatmap where 1 will be represented by gree, 2 by yellow, and 3 by red, using ggplots2.

This is my code:

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

And I get this error message:

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

Where am I going wrong?

Thank you.

解决方案

This will be much easier if your reshape your data from wide to long format. There are many ways to do that but here I used 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)

这篇关于如何使用ggplot2制作3个离散值的热图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆