2015-08-08 60 views
3

找不到我在問什麼,可能是錯誤的關鍵詞。從本質上講,我有3個維度的矩陣:繪製普通的2D圖形,但添加第三個維度作爲熱圖

> head(info) 
     [,1] [,2] [,3] 
[1,] 8.59645 251944 22.89 
[2,] 6.95160 141559 21.35 
[3,] 7.43870 131532 22.99 
[4,] 8.64467 126688 22.72 
[5,] 8.77482 123120 22.17 
[6,] 7.22364 122268 24.46 

我繪製信息[3]對信息[,2]

plot(info[,3], info[,2], type="p", pch=20) 

Plotting info[,3] vs info[,2]

,我想色點使用基於info [,1]的熱圖。

我只是做這樣的事情:

plot(info[which(info[,1] <= 2),3], info[which(info[,1] <= 2),2], type="p", pch=20, col="black") 
lines(info[which(info[,1] >= 2),3], info[which(info[,1] >= 2),2], type="p", pch=20, col="red") 

plotting based on info[,1]

但我相信熱圖會更好看。

任何想法?謝謝, Adrian

SOLUTION: 謝謝大家的精彩建議!這是什麼工作:

qplot(info[,3], info[,2], colour=info[,1]) + scale_colour_gradient(limits=c(0, 10), low="green", high="red") 

Solution working!

+1

你可以使用'ggplot2'並使用第三列的顏色。看一下示例[here](http://docs.ggplot2.org/current/scale_gradient.html)可能會有所幫助。 –

+0

謝謝!這工作得很漂亮:qplot(info [,3],info [,2],color = info [,1])+ scale_colour_gradient(limits = c(0,10),low =「green」,high =「red」) –

+0

我不明白你爲什麼使用'lines'是一個錯字?在基本圖形中,您還可以執行cols < - colorRampPalette(c('red','yellow'))(1000)[info [,1] * 1000]; plot(info [,3],info [,2],col = adjustcolor(cols,alpha.f = .3),pch = 19)' – rawr

回答

6

使用ggplot2可以通過第三可變顏色

## Some sample data 
set.seed(0) 
x <- rnorm(1000, rep(c(20, 60), each=500), 8) 
y <- c(rexp(500, 1/5e4)*1/(abs(x[1:500]-mean(x[1:500]))+runif(1)), 
     rexp(500, 1/5e3)*1/(abs(x[501:1000]-mean(x[501:1000]))+runif(1))) 
z <- c(sort(runif(1000))) 
info <- matrix(c(z,y,x), ncol=3) 

## Using ggplot 
ggplot(as.data.frame(info), aes(V3, V2, col=V1)) + 
    geom_point(alpha=0.5) + 
    scale_color_gradient(low="red", high="yellow") 

enter image description here

如果你想使一個熱地圖,你可以使用akima封裝在你的點之間進行插值,並且做,

library(akima) 
dens <- interp(x, y, z, 
       xo=seq(min(x), max(x), length=100), 
       yo=seq(min(y), max(y), length=100), 
       duplicate="median") 
filled.contour(dens, xlab="x", ylab="y", main="Colored by z", 
       color.palette = heat.colors) 

enter image description here

+0

感謝這真棒! –

+0

@AdrianP .:請看看:[我應該怎麼做當有人回答我的問題?](http://stackoverflow.com/help/someone-answers) – Cyrus