【问题标题】:Color histogram bins by majority class多数类的颜色直方图箱
【发布时间】:2012-10-09 21:46:11
【问题描述】:

我编写了一个有用的函数,用于按每个条形中的多数类为直方图条着色:

color_hist <- function(x, cats, ...){
  hist <- hist(x, plot=FALSE, ...)
  cuts <- cut(x, breaks=hist$breaks)

  color = apply(table(cuts, cats), 1, which.max)

  hist(x, col=color,...)
}

color_hist(iris[,4], iris[,5])

我想尽可能地复制hist() 的行为,但我不知道如何将标题和 x 标签从原始直方图传递到彩色直方图:

我希望新直方图与旧直方图具有相同的默认标题/xlabels,并且我还希望传递任何其他用户指定的参数。谁能帮我解决这个问题,或者以任何其他方式改进这个功能?

(如果我能根据班级百分比让颜色相互混合,那也很酷……)

【问题讨论】:

    标签: r histogram


    【解决方案1】:

    看看hist.default的代码,那里是怎么处理的……

    color_hist <- function(x, cats, xlab = xname, main = paste("Histogram of", xname), ...){
      xname <- paste(deparse(substitute(x), 500), collapse = "\n")
      hist <- hist(x, plot=FALSE, ...)
      cuts <- cut(x, breaks=hist$breaks)
    
      color = apply(table(cuts, cats), 1, which.max)
    
      hist(x, col=color, xlab = xlab, main = main, ...)
    }
    
    color_hist(iris[,4], iris[,5])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-04
      • 2013-10-17
      • 1970-01-01
      • 2012-08-22
      • 2018-04-25
      • 1970-01-01
      相关资源
      最近更新 更多