从ggplot2中提取颜色信息?

发布于 2024-10-01 18:47:49 字数 1192 浏览 4 评论 0原文

使用保存在名为 foo.txt 的文件中的虚拟代码...

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 中使用来自 遵循 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 但用颜色代码代替每个变量)。知道该怎么做吗?

Using this dummy code saved in a file named foo.txt...

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

I made a heatmap in ggplot2 with the accompanying code from 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')

It produces a really nice heatmap, but I'm really just after the color codes of each tile (basically the original foo but with color codes in place of each variable). Any idea how to go about this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最美的太阳 2024-10-08 18:47:49

我正在努力将所有与比例相关的代码从 ggplot2 提取到一个单独的包中 - 这将使以不同方式使用相同比例变得更加容易。有关正在进行的代码,请参阅 https://github.com/hadley/scales

I'm working on pulling all scale related code from ggplot2 into a separate package - this will make it much easier to use the same scales in different ways. See https://github.com/hadley/scales for the in progress code.

放手` 2024-10-08 18:47:49

不要从图中提取颜色,而是使用 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))

返回

“颜色渐变”

Rather than extracting the colours from the plot, use colorRampPalette:

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

where n is the number of colours in your heat map. In your example, I get n=6 so:

n<-6
a(n)

returns

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

and

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

returns

color-ramp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文