2012-10-20 74 views
5

我是R新手,我已經能夠繪製點,但想知道是否有一種方法可以將顏色漸變應用於散點圖。在R中,有沒有一種方法可以根據一系列數字在漸變上繪製點顏色?

我有一個3列矩陣,其中前兩個將用作座標,第三個具有0到0.0001之間的數字範圍。是否有一種方法根據它們落在數字範圍內的位置對繪圖點進行着色?

x y z 
15 3 6e-4 
34 22 1e-10 
24 1 5e-2 
... 

plot(x, y, main= "Title", ylab = "column y", xlab = "column x", col = rgb(0,100,0,50,maxColorValue=255), pch=16) 

回答

2

如何

plot(x, y, col = gray(z/0.0001)) 

這是灰色的。

3

我在ggplot2包大,因爲它做了很多鼓勵良好的習慣繪圖(雖然語法有點在第一混淆):

require(ggplot2) 
df <- data.frame(x=x, y=y, z=z) #ggplot2 only likes to deal with data frames 
ggplot2(df, aes(x=x, y=y, colour=z) + #create the 'base layer' of the plot 
    geom_point() + #represent the data with points 
    scale_colour_gradient(low="black", high="green") + #you have lots of options for color mapping 
    scale_x_continuous("column x") + #you can use scale_... to modify the scale in lots of other ways 
    scale_y_continuous("column y") + 
    ggtitle("Title") 
0

晚,但對於其他人本的好處可能是你之後的:

mat = cbind(sample(1:30), sample(1:30), 10*rnorm(30,mean=5)) 
n = 255 
data_seq = seq(min(mat[,3]), max(mat[,3]), length=n) 
col_pal = colorRampPalette(c('darkblue','orange'))(n+1) 
cols = col_pal[ cut(mat[,3], data_seq, include.lowest=T) ] 
plot(mat[, 1:2], col = cols, pch=16, cex=2) 
相關問題