2010-11-13 37 views
3

使用保存在一個名爲foo.txt的文件這個虛設碼...從ggplot2中提取顏色信息?

COG,station1,station2,station3,station4,station5 
COG000Z,0.019393497,0.183122497,0.089911227,0.283250444,0.074110521 
COG0002,0.044632051,0.019118032,0.034625785,0.069892277,0.034073709 
COG0001,0.033066112,0,0,0,0 
COG0004,0.115086472,0.098805295,0.148167492,0.040019101,0.043982814 
COG0005,0.064613057,0.03924007,0.105262559,0.076839235,0.031070155 
COG0006,0.079920475,0.188586049,0.123607421,0.27101229,0.274806929 
COG0007,0.051727492,0.066311584,0.080655401,0.027024185,0.059156417 
COG0008,0.126254841,0.108478559,0.139106704,0.056430812,0.099823028 

我在做GGPLOT2熱圖與following this answer on stackexchange附帶的代碼。

> library(ggplot2) 
> foo = read.table('foo.txt', header=T, sep=',') 
> foomelt = melt(foo) 
Using COG as id variables 
> ggplot(foomelt, aes(x=COG, y=variable, fill=value)) + geom_tile() + scale_fill_gradient(low='white', high='steelblue') 

它產生一個非常好的熱圖,但我只是每瓦的顏色代碼後(基本上是原來的FOO但在地方各變量的顏色代碼),我真的。任何想法如何去解決這個問題?

回答

3

不是從情節中提取的顏色相反,使用colorRampPalette

a<-colorRampPalette(c("white","steelblue")) 
plot_colours<-a(n) 

其中n是在你的熱圖的顏色數。在你的榜樣,我得到n=6這樣:

n<-6 
a(n) 

回報

[1] "#FFFFFF" "#DAE6F0" "#B4CDE1" "#90B3D2" "#6A9BC3" "#4682B4" 

image(1:n,1,as.matrix(1:n),col=a(n)) 

回報

color-ramp

+0

我結束了使用colorRampPalette和一些Python排序來替換顏色的值。謝謝。 – zachwill 2010-11-15 19:45:06

6

我WOR king將ggplot2中所有與縮放有關的代碼放入一個單獨的包中 - 這將使得以不同方式使用相同的縮放比較容易。進行中的代碼見https://github.com/hadley/scales

+0

感謝有關尺度的信息。我一定會研究它。 – zachwill 2010-11-15 19:45:37