在r,ggplot2,lattice或latticeExtra中创建更连续的调色板 [英] creating a more continuous color palette in r, ggplot2, lattice, or latticeExtra

查看:842
本文介绍了在r,ggplot2,lattice或latticeExtra中创建更连续的调色板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Warning.... very novice question follows:

我试图绘制一个相当规则的分布几千(X,Y)值,让我们调用Z,它在例如-20到+20之间非常不规则地变化。我对平滑不感兴趣;我想要点Z值根据一个平滑变化的颜色调色板,很像Gnuplot可以做的适当的平滑调色板。我尝试了base R,ggplot2和latticeExtra,并且尽可能,我可以想出下面这几乎做我想要的:

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))

一个数据点如下:1302525 225167 -3.5

One data point looks like: 1302525 225167 -3.5

当我将我的数据帧与最后代码行中的50一起绘制为3时,我得到红色,白色和蓝色颜色的可预测R循环行为重复五次,第16个颜色条段为白色。将3更改为7导致更多的红色和蓝色色调创建2个重复的颜色范围段,在颜色范围尝试回收时留下两个红色。这表明使这个数字更大导致更精细的颜色毕业。但是如果我把一个大于16的数字,这是我得到的,16色段,从红色,白色,到蓝色均匀变化。但我想让颜色标度更精细,在一个完美的世界,迫使零的零为白色。

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.

我迄今为止使用R的经验是当我无法做到这样简单的事情时,我缺少一个非常基本的概念。是什么?

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?

推荐答案

至于格子,你可以设置你的颜色调色板 RColorBrewer (甚至 colorspace )。使用@Chase提供的示例,但 z 的正值:

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)

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

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=.

这篇关于在r,ggplot2,lattice或latticeExtra中创建更连续的调色板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆