我必須繪製一些plot(x,y)散點圖,但是我希望基於連續變量z的值對點進行顏色編碼。基於另一個值對散點圖進行顏色編碼 - no ggplot
我想要一個溫度調色板(從深藍色到鮮紅色)。我嘗試了Rcolorbrewer,但是RdBu調色板(類似於溫度調色板)使用白色作爲中間值,看起來非常糟糕。
我還想繪製一個圖例,用顏色和相應值的樣本來解釋顏色編碼。
任何想法,如果這可以在R中輕鬆執行?請沒有ggplot!
季節問候大家
我必須繪製一些plot(x,y)散點圖,但是我希望基於連續變量z的值對點進行顏色編碼。基於另一個值對散點圖進行顏色編碼 - no ggplot
我想要一個溫度調色板(從深藍色到鮮紅色)。我嘗試了Rcolorbrewer,但是RdBu調色板(類似於溫度調色板)使用白色作爲中間值,看起來非常糟糕。
我還想繪製一個圖例,用顏色和相應值的樣本來解釋顏色編碼。
任何想法,如果這可以在R中輕鬆執行?請沒有ggplot!
季節問候大家
大廈斷@ BenBolker的答案,你可以,如果你把在爲filled.contour
代碼偷看做的傳說。我砍死該功能除了看起來像這樣:
scatter.fill <- function (x, y, z,
nlevels = 20, plot.title, plot.axes,
key.title, key.axes, asp = NA, xaxs = "i",
yaxs = "i", las = 1,
axes = TRUE, frame.plot = axes, ...)
{
mar.orig <- (par.orig <- par(c("mar", "las", "mfrow")))$mar
on.exit(par(par.orig))
w <- (3 + mar.orig[2L]) * par("csi") * 2.54
layout(matrix(c(2, 1), ncol = 2L), widths = c(1, lcm(w)))
par(las = las)
mar <- mar.orig
mar[4L] <- mar[2L]
mar[2L] <- 1
par(mar = mar)
#Some simplified level/color picking
levels <- seq(min(z),max(z),length.out = nlevels)
col <- colorRampPalette(c("blue","red"))(nlevels)[rank(z)]
plot.new()
plot.window(xlim = c(0, 1), ylim = range(levels), xaxs = "i",
yaxs = "i")
rect(0, levels[-length(levels)], 1, levels[-1L], col = colorRampPalette(c("blue","red"))(nlevels)
if (missing(key.axes)) {
if (axes)
axis(4)
}
else key.axes
box()
if (!missing(key.title))
key.title
mar <- mar.orig
mar[4L] <- 1
par(mar = mar)
#Simplified scatter plot construction
plot(x,y,type = "n")
points(x,y,col = col,...)
if (missing(plot.axes)) {
if (axes) {
title(main = "", xlab = "", ylab = "")
Axis(x, side = 1)
Axis(y, side = 2)
}
}
else plot.axes
if (frame.plot)
box()
if (missing(plot.title))
title(...)
else plot.title
invisible()
}
然後再從本的例子應用的代碼,我們得到這樣的:
x <- runif(40)
y <- runif(40)
z <- runif(40)
scatter.fill(x,y,z,nlevels = 40,pch = 20)
產生這樣一個情節:
公平的警告,我真的只是破解了filled.contour
的代碼。您可能需要檢查剩餘的代碼並刪除未使用的位,或修復我無法使用的部分。
這裏是一些自制代碼默認包(基地,圖形,grDevices)來實現它:
# Some data
x <- 1:1000
y <- rnorm(1000)
z <- 1:1000
# colorRamp produces custom palettes, but needs values between 0 and 1
colorFunction <- colorRamp(c("darkblue", "black", "red"))
zScaled <- (z - min(z))/(max(z) - min(z))
# Apply colorRamp and switch to hexadecimal representation
zMatrix <- colorFunction(zScaled)
zColors <- rgb(zMatrix, maxColorValue=255)
# Let's plot
plot(x=x, y=y, col=zColors, pch="+")
對於StanLe,這裏是相應的圖例(以 - 我用藍色,而不是深藍色爲出發點,但你可以
# Resolution of the legend
n <- 10
# colorRampPalette produces colors in the same way than colorRamp
plot(x=NA, y=NA, xlim=c(0,n), ylim=0:1, xaxt="n", yaxt="n", xlab="z", ylab="")
pal <- colorRampPalette(c("darkblue", "black", "red"))(n)
rect(xleft=0:(n-1), xright=1:n, ybottom=0, ytop=1, col=pal)
# Custom axis ticks (consider pretty() for an automated generation)
lab <- c(1, 500, 1000)
at <- (lab - min(z))/(max(z) - min(z)) * n
axis(side=1, at=at, labels=lab)
這是一個合理的解決方案:通過layout
或類似的東西)加入檢查出?rgb
等,以調整你喜歡的顏色。
nbrk <- 30
x <- runif(20)
y <- runif(20)
cc <- colorRampPalette(c("blue","red"))(nbrk)
z <- runif(20)
plot(x,y,col=cc[cut(z,nbrk)],pch=16)
沒有ggplot ...嗯晶格呢? – dickoa 2011-12-23 13:03:44
可以試試,但是,我會很感激r-base解決方案。 – ECII 2011-12-23 13:11:36