2015-02-24 96 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