2015-02-24 131 views
-2

映射顏色的堆疊barplot我剛剛在R開始,我被困在房間裏。 我想製作一個堆疊的barplot,其中的顏色是根據變量進行選擇的。R根據變量

我的數據看起來LIK此:

厚度
點3點5點6點7點8點9點11
層1 25 20 90 80 100 45 75
二層5 20 0 0 0 70 0
三層80 5 0 0 0 0 0

類型
點3點5點6點7點8點9點11
層1 4 3 3 3 3 3 3
二層5 5 6 6 6 5 6
三層4 3 6 6 6 6 6

對於每個點有一個數字的厚度AL需要在彼此的頂部堆疊,並且類型AL_col指示堆疊酒吧需要的顏色。

我的腳本:

col1<-c("green","brown","purple","black","yellow", "white") 
barplot(Thickness, col=col1[Type]) 

謝謝!

EDIT

我會試圖解釋好一點: 我有10.000位置,這些是點(如這裏的點上方的實施例3,5,6,7,8,9,11) 這些位置可以由3層組成。 這些層的厚度和材料類型不同。 我想要做的是: 每個點都有一個堆疊的barplot,其中可以看到每層的厚度。 並查看我想要使用顏色方案的材料類型。每個點都有所不同。

+0

在您的AL的定義點的數量不匹配的數量列。你能發佈生成AL和AL_col的R代碼嗎? – cmbarbu 2015-02-24 14:35:36

回答

0

標準barplot也不錯,你只需要把你的數據放在一個矩陣中。例如:

L <- data.frame(point = c(3, 5, 6, 7, 8, 9, 11), 
    layer1 = c(1, 25, 20, 90, 80, 100, 45),     
    layer2 = c(5, 20, 0, 0, 0, 70, 0), 
    layer3 = c(80, 5, 0, 0, 0, 0, 0)) 

    barplot(t(as.matrix(L)),col=c("blue","black","yellow","orange")) 

enter image description here

做的正是你想要的東西意味着循環:

L <- data.frame(
      layer1 = c(25, 20, 90, 80, 100, 45),  
      layer2 = c(5, 20, 0, 0, 70, 0), 
      layer3 = c(80, 5, 0, 0, 0, 0)) 
    rownames(L)<-c(3, 5, 6, 7, 8, 9) 

    AL_col <- matrix(c(
       4,3,3,3,3,3, 
       5,5,6,6,6,5, 
       4,3,6,6,6,6),ncol=6,byrow=TRUE) 
    colnames(AL_col) <- c("point3","point5","point6","point7","point8","point9") 
    rownames(AL_col) <- c("layer1","layer2","layer3") 

    col1<-c("green","brown","purple","black","yellow", "white","blue") 

    # the problem is then to make polygons corresponding to your colors 

    maxHeight <- max(as.matrix(L) %*% rep(1,dim(L)[2])) 
    widthPol <- 0.5 
    plot(c(1-widthPol,dim(L)[1]+widthPol),c(0,maxHeight),type="n",xlab="Points",ylab="Height") 

    for(iPoint in 1:6){ 
     currentY <- 0 
     for(iLayer in 1:3){ 
      addedY <- L[iPoint,iLayer] 
       if(addedY>0){ 
        xs <- c(rep(iPoint-widthPol/2,2),rep(iPoint+widthPol/2,2)) 
         ys <- c(0,addedY,addedY,0) 
         colPoly <- col1[AL_col[iLayer,iPoint]] 
         polygon(x=xs,y=ys+currentY,col=colPoly) 
         currentY <- currentY + addedY 
       } 
     } 
    } 

enter image description here

+0

這再次不是我所需要的。上面的圖有藍色的第1層,黑色的第2層等等,對於每個條 我將嘗試用圖片作爲我想要做的示例 – IlhameO 2015-02-24 14:43:20

+0

Apperently我可以添加圖片,因爲我是新的:所以我會嘗試用文字表示... 因此,第3點,第一個酒吧,將有黑色,黃色,黑色,各自的高度爲25,5,80。 點5,第二個酒吧會有紫色,黑色,紫色與20,20,5 – IlhameO 2015-02-24 14:44:17

+0

respecative higth在你的顏色方案的特殊情況下的問題是,L是非常嚴重的定義。你需要每種顏色一行。理想情況下,L可以轉換爲符合該要求,因爲您不需要保留「圖層」信息,但不幸的是,您的計劃中同一個欄中的顏色相同,因此無法使用標準barplot。因此,您必須手動完成,我很害怕:您將一個接一個地多邊形繪製出來...... – cmbarbu 2015-02-24 14:55:53

1

而不是基地R圖形,或者用ggplot

library(ggplot) 

AL <- data.frame(point = c(3, 5, 6, 7, 8, 9, 11), layer1 = c(1, 25, 20, 90, 80, 100, 45), 
       layer2 = c(5, 20, 0, 0, 0, 70, 0), layer3 = c(80, 5, 0, 0, 0, 0, 0)) 
AL.m <- melt(AL, id.vars = "point") 

col1<-c("green","brown","purple","black","yellow", "white") 
barplot(AL, col=col1[AL_col]) 

ggplot(AL.m, aes(x = point, y = value, fill = variable)) + geom_bar(stat = "identity", position = "stack") + 
    scale_fill_manual(values = col1) 

enter image description here

EDIT COMMENT 後由於只有三層,僅使用COL1的第一三種顏色。 目前還不清楚你正在尋找什麼替代染色方案。

+0

感謝您的快速響應。但這不是我所要的。我希望顏色與每個點的每個圖層的值都與AL_col相匹配。所以點3,第一個酒吧,將有黑色,黃色,黑色,各自的高度爲25,5,80. 點5,第二個酒吧會有紫色,黑色,紫色,具有高度20,20,5 。 這可能嗎? – IlhameO 2015-02-24 14:11:50