2013-03-05 88 views
0

更新:現在刪除了不相關的前導碼和更新的進度。混合顏色以在R中創建自定義繪圖

嗨我試圖犁一個水平條有點像熱圖,這將根據下面的值改變顏色。該欄默認爲紅色,但根據兩個參數的值更改顏色。

我發現一些網頁,對於類似的效果試試這樣:R matrix plot with colour threshold and grid

How can one mix 2 or more color palettes to show a combined color value

所以我想去了解這將是水平繪製了一系列瓷磚的最佳途徑,每個人都有一個基於兩個值的計算顏色。

我設法做的是生成我想要的顏色。我通過獲得一個矩陣[2xn]來完成,其中n是瓦片的數量。 ROW1包含值1的每個瓦片,並且行2含有值2爲每一瓦片:

col.deter <- function(invalues) { 
    cols <- rgb(green=invalues[1], red=100, blue=invalues[2], maxColorValue=100) 
    return(cols) 
} 

cols <- unlist(lapply(1:ncol(colourmatrix), function(i) col.deter(colourmatrix[,i]))) 

然後我做:

colourmatrix[,c(1:5)] 
    [,1]  [,2]  [,3]  [,4]  [,5]  
    "81"  "82"  "82"  "82"  "82"  
    "58"  "58"  "58"  "58"  "58" 

我然後通過施加下面的所有列中產生針對每一瓦片的顏色一個rbind()我的結果是:

> colourmatrix[,c(1:5)] 
    [,1]  [,2]  [,3]  [,4]  [,5]  
    "81"  "82"  "82"  "82"  "82"  
    "58"  "58"  "58"  "58"  "58"  
cols "#FFCF94" "#FFD194" "#FFD194" "#FFD194" "#FFD194" 

所以每個瓦片,較高值1,更多的綠色被添加到混合物中,較高值2,更多的藍色被添加到混合物中。因此,每個瓷磚的顏色被確定並添加爲第三行。

我想現在要做的,是像在使用此代碼的答案在本頁面生成與GGPLOT2電網:

dat <- expand.grid(blue=seq(0, 100, by=10), red=seq(0, 100, by=10)) 
dat <- within(dat, mix <- rgb(green=0, red=red, blue=blue, maxColorValue=100)) 

library(ggplot2) 
ggplot(dat, aes(x=red, y=blue)) + 
    geom_tile(aes(fill=mix), color="white") + 
    scale_fill_identity() 

但試想只有一行。我試圖從左到右實現一行瓷磚,它們的顏色由生成的值決定。

我試圖做到這一點與ggplot2和圖像()但我的嘗試下降平坦,我通常不使用ggplot 2我不明白aes和所有我得到的錯誤,但我試過了,因爲這是用於生成上面代碼塊生成的紅色和藍色瓦片圖的內容。

我的失敗嘗試:

ggplot(c(1,ncol(colourmatrix)), 1, aes(x=x, y=y)) + 
    geom_tile(aes(fill=colourmatrix[3,]), color="white") + 
    scale_fill_identity() 

Error in ggplot.data.frame(fortify(data, ...), mapping) : 
    Mapping should be created with aes or aes_string 


    qplot(c(1,ncol(colourmatrix)), c(1,ncol(colourmatrix)), fill=colourmatrix[3,], geom='tile') 

Error: Aesthetics must either be length one, or the same length as the dataProblems:c(1, ncol(colourmatrix)), c(1, ncol(colourmatrix)) 

我也有一些失敗的嘗試,試圖像()我讀過,試圖實現這個頁面:

How can one mix 2 or more color palettes to show a combined color value

R matrix plot with colour threshold and grid

謝謝,

Ben W.

+0

你打算在將來做更多的自定義可視化嗎?如果是這樣,你可能想關閉並學習基礎「網格」包。有了它,您可以創建任何**視覺效果,但首先需要稍長的時間才能設置。不過,對於高度自定義的可視化,它比用'ggplot'等函數打包更快。 – Dinre 2013-03-05 19:28:39

回答