[R热图GGPLOT2有序的数据文件(R heatmap ggplot2 ordered as da

2019-10-20 14:48发布

我生产这种热图

如何生产出GGPLOT2热图?

在该职位的代码工作正常

library(reshape2)
 library(ggplot2)
 library(scales)
library(plyr)
data <- read.csv("fruits.txt", head=TRUE, sep=",")
 data.m = melt(data)
 data.m <- ddply(data.m, .(variable), transform, rescale = rescale(value))
  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")

我使用的数据文件是这样的

people,1,2,3
Ej1,1,0,0
Ej2,0,0,1
Ej3,0,0,1
Ej4,1,1,0

但我需要证明,因为它们(从开始到EJ1 EJ4)行和它产生它们以相反的顺序,我不能找出如何显示它们,因为它们在数据文件中。

Answer 1:

像这样?

尝试使用:

data$people <- factor(data$people,levels=rev(data$people))

之前调用melt(...)

data$people <- factor(data$people,levels=rev(data$people))
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")

data$people是一个因素, ggplot将字母顺序排序因子水平从下到上“增加”。 上述变化反转因子水平的顺序。



文章来源: R heatmap ggplot2 ordered as data file
标签: r ggplot2