【问题标题】:Segment my histogram into several parts with different colors in R [duplicate]在R中将我的直方图分割成不同颜色的几个部分[重复]
【发布时间】:2014-03-11 01:21:17
【问题描述】:

您好,我想将直方图分割成几个不同颜色的部分。比如蓝色值在0.3以下,红色值在0.3到0.5之间,绿色值在0.5到0.7之间……等等。

有什么想法吗?

【问题讨论】:

    标签: r histogram


    【解决方案1】:

    Check col 参数和直方图breaks 属性。请参见下面的示例:

    set.seed(0)
    x = rnorm(100, mean=0.5, sd=0.5)
    h = hist(x, breaks=10, plot=F)
    
    colors = rep("blue", length(h$breaks))
    colors[h$breaks >= 0.3] = "red"
    colors[h$breaks >= 0.5] = "green"
    colors[h$breaks >= 0.7] = "orange"
    hist(x, breaks=10, col=colors)
    

    【讨论】:

      【解决方案2】:

      ggplot2怎么样

      require(ggplot2)
      df<-data.frame(x=runif(100))
      ggplot(df)+geom_histogram(aes(x,fill=factor(..x..)),binwidth=0.1) 
      

      【讨论】:

        【解决方案3】:

        必须为每个支柱设置颜色。 在这个例子中,有 8 个柱子 => 你需要将 8 种颜色发布到 hist

          set.seed(123)
            a<-rnorm(20)
            hist(a)
            col<-c(rep("red",2),rep("blue",6))
            hist(a,col=col)
        

        鲍曼的回答要好得多!

        【讨论】:

        • +1 参考 Baumann 的回答。
        【解决方案4】:

        如果您希望整个条具有相同的颜色,那么前面的答案非常有用。如果即使条中的中断不匹配,您也希望转换处于规定的确切值,那么这里采用另一种方法(使用基本图形):

        set.seed(0)
        x = rnorm(100, mean=0.45, sd=0.25)
        
        hist(x, col='blue')
        
        tmp <- par('usr')
        clip(0.3,0.5, tmp[3], tmp[4])
        hist(x, col='red', add=TRUE)
        
        clip(0.5, tmp[2], tmp[3], tmp[4])
        hist(x, col='green', add=TRUE)
        

        【讨论】:

          猜你喜欢
          • 2014-03-18
          • 1970-01-01
          • 2014-10-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-10-15
          • 2018-08-21
          • 1970-01-01
          相关资源
          最近更新 更多