R热图ggplot2作为数据文件订购 [英] R heatmap ggplot2 ordered as data file
问题描述
我正在制作这张热图
该文章中的代码正常工作
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,。(变量),变换,rescale = rescale(值))
data.m < - ddply(data.m,。(variable),transform,rescale = rescale(value))
p < - ggplot (data.m,aes(variable,people))+ geom_tile(aes(fill = rescale),
color =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开始),它们以相反的顺序生成它们,我无法找到如何显示它们,因为它们在数据文件中。
$ b
尝试使用:
< pre $
data $ people< - factor(data $ people,levels = rev(data $ people))
就在调用 melt(...)
之前:
<$ (数据$ 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 =重新定标),
color =white)
p + scale_fill_gradient(low =white,high =steelblue)
data $ people
是一个因子, ggplot
按字母顺序从底部到顶部增加。上面的变化反转了因素水平的顺序。
I'm producing this heatmap
How to produce a heatmap with ggplot2?
The code in that post works fine
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")
The data file I'm using is this
people,1,2,3
Ej1,1,0,0
Ej2,0,0,1
Ej3,0,0,1
Ej4,1,1,0
But I need to show the rows as they are (starting from Ej1 to Ej4) and it produces them in reverse order, I can't find out how to show them as they are in the data file.
Like this?
Try using:
data$people <- factor(data$people,levels=rev(data$people))
just before the call to 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
is a factor and ggplot
will order the factor levels alphabetically "increasing" from bottom to top. The change above reverses the order of the factor levels.
这篇关于R热图ggplot2作为数据文件订购的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!