在 r、ggplot2、lattice 或latticeExtra 中创建更连续的调色板

发布于 2024-11-10 10:09:32 字数 797 浏览 4 评论 0原文

警告...非常新手的问题如下:

我试图绘制数千个(X,Y)点的相当规则的分布,每个点与一个值相关联,我们称之为Z,它在-20到+之间变化非常不规则20.我对平滑不感兴趣;我希望 Z 点值根据平滑变化的调色板进行绘制,就像 Gnuplot 使用适当的平滑调色板所做的那样。我已经尝试过base R、ggplot2和latticeExtra,并且尽我所能,我可以想出以下几乎可以满足我想要的功能:

library(lattice)
library(latticeExtra)
library(colorRamps)
df = read.table(file"whatever", header=T)
levelplot(Z~X*Y, df, panel=panel.levelplot.points, cex=0.2,
   col.regions=colorRampPalette(c("red","white","blue"))(50))

一个数据点看起来像:1302525 225167 -3.5

当我用“绘制我的数据框时50" 在最后一个代码行中作为 3,我得到了红色、白色和蓝色的可预测 R 回收行为,重复五次,第 16 个颜色条段为白色。将 3 更改为 7 会导致更多的红色和蓝色色调,从而创建 2 个重复颜色范围段,并在颜色范围尝试回收时留下两种微红色。这表明增大该数字会导致颜色分级更精细。但如果我输入的数字大于 16,这就是我得到的全部,16 个彩色段,从红色、白色、蓝色均匀变化。但我希望色阶更精细,并且在完美的世界中,强制 Z 为零为白色。

到目前为止,我使用 R 的经验是,当我无法完成像这样简单的事情时,我错过了一个非常基本的概念。它是什么?

Warning.... very novice question follows:

I am trying to plot a fairly regular distribution of several thousand (X,Y) points each associated with a value, let's call Z, which varies very irregularly between, say, -20 to +20. I am not interested in smoothing; I want the point Z values to plot according to a smoothly varying color palette much like Gnuplot can do with the proper smooth color palette. I've tried base R, ggplot2, and latticeExtra, and as best I can, I can come up with the following which does almost what I want:

library(lattice)
library(latticeExtra)
library(colorRamps)
df = read.table(file"whatever", header=T)
levelplot(Z~X*Y, df, panel=panel.levelplot.points, cex=0.2,
   col.regions=colorRampPalette(c("red","white","blue"))(50))

One data point looks like: 1302525 225167 -3.5

When I plot my dataframe with the "50" in the last code line as 3, I get the predictable R recycle behavior of the red, white, and blue colors repeating five times with the 16th color bar segment white. Changing the 3 to a 7 causes more shades of red and blue creating 2 repeat color range segments with two reddish colors left over as the color range tries to recycle. This suggests making this number larger causes a finer graduation of colors. But if I put in a number greater than 16, that's all I get, 16 colored segments, evenly changing from red, to white, to blue. But I'd like the color scale even finer, and in a perfect world, force a Z of zero to be the white color.

My experience so far with R is when I can't do something as simple as this, I'm missing a very fundamental concept. What is it?

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

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

发布评论

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

评论(3

空城旧梦 2024-11-17 10:09:32

就晶格而言,您可以使用 RColorBrewer< 设置调色板/a> (甚至颜色空间)。使用 @Chase 提供的示例,但 z 为正值:

dat <- data.frame(x = rnorm(1000), y = rnorm(1000), z = sample(0:40, 1000, TRUE))
library(RColorBrewer)
# see, e.g.
# display.brewer.all(9, type="seq")
# display.brewer.pal(11, "RdBu")
my.col <- colorRampPalette(brewer.pal(11, "RdBu"))(diff(range(dat$z)))
xyplot(y ~ x, data=dat, col=my.col[dat$z], pch=19, alpha=.5)

请注意,这里还需要通过插值来增加可用颜色的范围。另外,使用 levelplot() 时,您可能想使用 cut=pretty=

在此处输入图像描述

As far as lattice is concerned, you can set up your colors palette with RColorBrewer (or even colorspace). Using the example provided by @Chase, but with positive value for z:

dat <- data.frame(x = rnorm(1000), y = rnorm(1000), z = sample(0:40, 1000, TRUE))
library(RColorBrewer)
# see, e.g.
# display.brewer.all(9, type="seq")
# display.brewer.pal(11, "RdBu")
my.col <- colorRampPalette(brewer.pal(11, "RdBu"))(diff(range(dat$z)))
xyplot(y ~ x, data=dat, col=my.col[dat$z], pch=19, alpha=.5)

Note that it is also necessary here to increase the range of available colors by interpolation. Also, with levelplot(), you might want to play with cut= and pretty=.

enter image description here

摘星┃星的人 2024-11-17 10:09:32

你看过ggplot中的scale_gradient吗?或者用于离散颜色的 scale_brewer ?这是 scale_gradient 的示例

dat <- data.frame(x = rnorm(1000), y = rnorm(1000), z = sample(-20:20, 1000, TRUE))

p <- ggplot(dat, aes(x, y, colour = z)) + geom_point() 
p + scale_colour_gradient()
p + scale_colour_gradient(low = "red", high = "blue")
p + scale_colour_gradient2(low = "red", mid = "white", high = "blue")

Have you looked at scale_gradient in ggplot? Or scale_brewer for discrete colours? Here's an example of scale_gradient

dat <- data.frame(x = rnorm(1000), y = rnorm(1000), z = sample(-20:20, 1000, TRUE))

p <- ggplot(dat, aes(x, y, colour = z)) + geom_point() 
p + scale_colour_gradient()
p + scale_colour_gradient(low = "red", high = "blue")
p + scale_colour_gradient2(low = "red", mid = "white", high = "blue")
风为裳 2024-11-17 10:09:32

您缺少的“概念”是 levelplot() 的 at 参数,它定义颜色级别和/或轮廓线之间的断点。默认值是pretty(z),它只产生几个级别。您可以将 at 设置为涵盖所需值范围的序列。

library(latticeExtra)

dat <- data.frame(x = rnorm(1000), y = rnorm(1000), z = rnorm(1000, mean = 1))
## for centering the colour key around zero
maxz <- max(abs(dat$z))

levelplot(z ~ x * y, dat, at = seq(-maxz, maxz, length = 100), 
    panel = panel.levelplot.points, par.settings = custom.theme.2())

The "concept" you are missing is the at argument to levelplot() which defines the breakpoints between colour levels and/or contour lines. The default is pretty(z) which results in only a few levels. You can set at to be a sequence covering the range of values you want.

library(latticeExtra)

dat <- data.frame(x = rnorm(1000), y = rnorm(1000), z = rnorm(1000, mean = 1))
## for centering the colour key around zero
maxz <- max(abs(dat$z))

levelplot(z ~ x * y, dat, at = seq(-maxz, maxz, length = 100), 
    panel = panel.levelplot.points, par.settings = custom.theme.2())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文