R 如何使用ggplot2制作3个离散值的热图?
我对aes参数有问题;不知道它想告诉我要修复什么 我的数据框是这样的:R 如何使用ggplot2制作3个离散值的热图?,r,ggplot2,data-visualization,heatmap,R,Ggplot2,Data Visualization,Heatmap,我对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
> 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"))
我试图制作一个热图,其中1用绿色表示,2用黄色表示,3用红色表示,使用ggplots2
这是我的代码:
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])
我哪里做错了
谢谢。如果您将数据从宽格式改为长格式,这将更容易。有很多方法可以做到这一点,但我在这里使用了
reformae2
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)
库(2);图书馆(GG2)
颜色如果将数据从宽格式改为长格式,这将更容易。有很多方法可以做到这一点,但我在这里使用了reformae2
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)
库(2);图书馆(GG2)
颜色