【问题标题】:Change colours of particular bars in a bar chart更改条形图中特定条形的颜色
【发布时间】:2012-10-18 06:09:35
【问题描述】:

我一直在创建一些条形图,我想知道是否可以根据条形图位于 x 轴上方还是下方来为图表上的条形着色?

为了澄清,这是我的意思的条形图类型:

理想情况下,我希望能够将上面的条形图与下面的条形图分开,这样图表看起来更有吸引力,我一直在搜索,但找不到任何方法,谁能帮忙?

提前致谢。 :)

【问题讨论】:

    标签: r colors bar-chart


    【解决方案1】:

    这是一种策略:

    ## Create a reproducible example
    set.seed(5)
    x <- cumsum(rnorm(50))
    
    ## Create a vector of colors selected based on whether x is <0 or >0  
    ## (FALSE + 1 -> 1 -> "blue";    TRUE + 1 -> 2 -> "red")
    cols <- c("blue", "red")[(x > 0) + 1]  
    
    ## Pass the colors in to barplot()   
    barplot(x, col = cols)
    

    如果您想要两种以上基于值的颜色,您可以采用类似的策略(使用findInterval() 代替简单的逻辑测试):

    vals <- -4:4
    breaks <- c(-Inf, -2, 2, Inf)
    c("blue", "grey", "red")[findInterval(vals, vec=breaks)]
    # [1] "blue" "blue" "grey" "grey" "grey" "grey" "red"  "red"  "red" 
    

    【讨论】:

      【解决方案2】:

      一种方法是使用逻辑变量或因子变量来索引颜色向量(这是 R 中的常见习语。

      set.seed(1)
      NAO <- rnorm(40)
      cols <- c("red","black")
      pos <- NAO >= 0
      barplot(NAO, col = cols[pos + 1], border = cols[pos + 1])
      

      这里的诀窍是pos:

      > pos
       [1] FALSE  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE FALSE
      [11]  TRUE  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE  TRUE  TRUE
      [21]  TRUE  TRUE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE
      [31]  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE
      

      我们在barplot() 调用中强制转换为数字:

      > pos + 1
       [1] 1 2 1 2 2 1 2 2 2 1 2 2 1 1 2 1 1 2 2 2 2 2 2 1 2 1 1 1 1 2
      [31] 2 1 2 1 1 1 1 1 2 2
      

      1s 和2s 的向量从颜色cols 的向量中选择元素,这样:

      > cols[pos + 1]
       [1] "red"   "black" "red"   "black" "black" "red"   "black"
       [8] "black" "black" "red"   "black" "black" "red"   "red"  
      [15] "black" "red"   "red"   "black" "black" "black" "black"
      [22] "black" "black" "red"   "black" "red"   "red"   "red"  
      [29] "red"   "black" "black" "red"   "black" "red"   "red"  
      [36] "red"   "red"   "red"   "black" "black"
      

      这是传递给每个绘制的条的颜色。

      在上面的代码中,我还通过参数border 将条形的边框设置为相关颜色。

      生成的图应如下所示

      【讨论】:

      • +1 以获得很好的解释,尤其是。建议为边框着色。
      【解决方案3】:

      使用geom_barstat_identityggplot2 解决方案。

      library(ggplot2)
      ggplot(dat, aes(x= seq_along(x), y = x)) + 
        geom_bar(stat = 'identity', aes(fill = x>0), position = 'dodge', col = 'transparent') + 
        theme_bw() + scale_fill_discrete(guide = 'none') + 
        labs(x = '', y = 'NAO Index')
      

      scale_fill_discrete(guide = 'none') 删除图例,position = 'dodge' 停止来自默认 position = 'stack' 的警告。

      【讨论】:

      • 如何在 barplot(t(as.matrix(mydf)), beside=TRUE, col=heat.colors(3)) 中使用像您这样的颜色?谢谢
      【解决方案4】:

      我在寻找有关如何根据因素为条形图中的各个条形着色的帮助时找到了此页面。我找不到任何其他信息来源,但感谢这个页面,我想出了这个:

      cols

      然后以上述通常的方式将其传递给 barplot:

      条形图(x, col = cols)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-04-07
        • 2011-08-03
        • 2012-01-19
        • 1970-01-01
        • 2013-12-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多