【问题标题】:different colors for each bar in stacked bar graph - base graphics堆叠条形图中每个条的不同颜色 - 基本图形
【发布时间】:2014-05-11 23:30:01
【问题描述】:

我想绘制一个堆积条形图,就像所附的那样,但我希望颜色在 aa、bb 和 cc 类别之间有所不同。具体来说,我希望 bb 中的灰色块为红色,而 cc 中的灰色块为绿色。以下代码作为一个简单的示例,说明了我已经尝试过的内容:

aa=c(0.2,0.6,0.1,0.1)
bb=c(0.4,0.5,0.05,0.05)
cc=c(0.5,0.25,0.1,0.15)
x=cbind(aa,bb,cc)
x #the data
aa   bb   cc

[1,] 0.2 0.40 0.50
[2,] 0.6 0.50 0.25
[3,] 0.1 0.05 0.10
[4,] 0.1 0.05 0.15

默认行为,每个类别中的所有块都具有相同的颜色

col=rep(c("white","grey"),2)
col
# [1] "white" "grey"  "white" "grey" 

barplot(x,col=col)

但我希望bb 中的灰色块为红色,cc 中的灰色块为绿色

col=cbind(rep(c("white","grey"),2),rep(c("white","red"),2),rep(c("white","green"),2))
col

[,1]    [,2]    [,3]   
[1,] "white" "white" "white"
[2,] "grey"  "red"   "green"
[3,] "white" "white" "white"
[4,] "grey"  "red"   "green"

barplot(x,col=col) #not working

col=c(rep(c("white","grey"),2),rep(c("white","red"),2),rep(c("white","green"),2))
col
[1] "white" "grey"  "white" "grey"  "white" "red"   "white" "red"   "white" "green" "white" "green"

barplot(x,col=col) #not working either

非常感谢您的任何建议。

【问题讨论】:

  • plotrix 包中的 barp() 允许将矩阵提供给 col - 但它只做 grouped 条形图,而不是 stacked 条形图.您可能需要在此处滚动您自己的函数(可能需要多次调用rect())。如果你这样做,请在此处发布,我会很高兴地支持它。
  • 该解决方案已在此处解决:stat.ethz.ch/pipermail/r-help/2007-March/126848.html。但是下面的解决方案对我来说更直接,因为我只需要每个条形图一种颜色。

标签: r bar-chart stacked stacked-chart


【解决方案1】:

一种解决方法:扩展您的矩阵,使值对应于虚构的类别,每个类别只有一种颜色。只有aabbcc 中的一个实际上会有这些类别中的数据。

xx <- rep(0,4)
x <- matrix(c(aa,xx,xx,xx,bb,xx,xx,xx,cc),ncol=3)
x
      [,1] [,2] [,3]
 [1,]  0.2 0.00 0.00
 [2,]  0.6 0.00 0.00
 [3,]  0.1 0.00 0.00
 [4,]  0.1 0.00 0.00
 [5,]  0.0 0.40 0.00
 [6,]  0.0 0.50 0.00
 [7,]  0.0 0.05 0.00
 [8,]  0.0 0.05 0.00
 [9,]  0.0 0.00 0.50
[10,]  0.0 0.00 0.25
[11,]  0.0 0.00 0.10
[12,]  0.0 0.00 0.15

然后像你一样绘制:

col <- c(rep(c("white","grey"),2),rep(c("white","red"),2),rep(c("white","green"),2))
barplot(x,col=col)

【讨论】:

    【解决方案2】:

    这可以通过一次向绘图中添加一个彩色条来实现:

    # white bars 
    barplot(x, col='white', axes=F, axisnames=F, yaxp=c(0,1,2), las=1)
    cols=c('grey','red','green')
    
    # add coloured bars
    for (i in 1:ncol(x)){
        xx = x
        xx[,-i] <- NA
        colnames(xx)[-i] <- NA
        barplot(xx,col=c('white',cols[i]), add=T, axes=F) 
    }
    

    【讨论】:

      【解决方案3】:
      library(ggplot2)
      library(reshape2)
      x <- data.frame(aa=c(0.2,0.6,0.1,0.1),
                      bb=c(0.4,0.5,0.05,0.05),
                      cc=c(0.5,0.25,0.1,0.15),
                      dd = 1:4)
      x <- melt(x, "dd")
      col=c(rep(c("white","grey"),2),rep(c("white","red"),2),rep(c("white","green"),2))
      ggplot(x, aes(x = variable, y = value)) + geom_bar(stat = "identity", fill = col)
      

      【讨论】:

      • 这是一个非常简洁的解决方案,只有一个颜色向量和简单的fill = col。这种方法可以适用于任何 ggplot 吗? (我想知道你是怎么知道情节使用颜色的顺序的?)
      • 这已经很老了,我认为应该有更好的方法来构建col 向量。关于ggplot,是的,它被设计为使用长(或整洁)格式,因此它总是“更喜欢”一列。哈德利就是按照这个理论设计的vita.had.co.nz/papers/tidy-data.pdf
      猜你喜欢
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 2021-02-25
      • 2021-03-06
      • 1970-01-01
      • 1970-01-01
      • 2014-10-25
      • 1970-01-01
      相关资源
      最近更新 更多