0

In R, hist.data.frame (from Hmisc) produces a histogram for each column in a data frame, but I can't figure out how to change the colors of the bars. Is there any way I can do this?

library(Hmisc)
tmp<-data.frame(c(1,1,2,3),c(2,2,1,3))

# histograms of both columns, in white. how do I get them in blue?
hist(tmp)
Jeff
  • 12,147
  • 10
  • 51
  • 87
  • `hist` passes `data.frame` argument? – Khashaa Jan 16 '15 at 16:59
  • 1
    sorry, i don't understand your question? – Jeff Jan 16 '15 at 17:00
  • 1
    `hist` does not accept data frames, the problem is not in the `col` argument. You may want to have a look at: http://stackoverflow.com/questions/3541713/how-to-plot-two-histograms-together-in-r – nico Jan 16 '15 at 17:08
  • 1
    ah, thanks all! I had package `Hmisc` loaded in the background and didn't realize I was implicitly calling `hist.data.frame`. I suspect this function simply doesn't support colors. Is there an alternative maybe? – Jeff Jan 16 '15 at 17:14
  • @Jeff what do you need to do exactly? Plot two histograms on the same graph? If so, look at the question linked above. – nico Jan 16 '15 at 17:26
  • @nico actually no, i have a data.frame with 18 columns and i need 18 separate histograms. `hist.data.frame` works fine, except that i would prefer it colored. – Jeff Jan 16 '15 at 17:29

1 Answers1

3

The hist.data.frame function from Hmisc does not have a parameter option for color. All of the calls to base hist() have no color parameters. A very hack-y workaround would be create your own hist.data.frame to temporarily redefine the hist() function and allow you to change the colors. For example

hist.data.frame <- function(x, ..., colors=rainbow(ncol(x))) {
    col<-1
    hist<-function(...) {
        graphics::hist(..., col=colors[col])
        col <<- col+1
    }
    f <- Hmisc:::hist.data.frame
    environment(f) <- environment()
    f(x,...)
}

hist(iris)

enter image description here

MrFlick
  • 195,160
  • 17
  • 277
  • 295