Partially color histogram in R

前端 未结 4 1957
慢半拍i
慢半拍i 2020-12-05 00:51

\"enter

I have a histogram as shown in the picture. I want the bars in the two regions

4条回答
  •  攒了一身酷
    2020-12-05 01:46

    Here's the method I mentioned in comments:

    Make some test data (you should do this in your question!)

    test = runif(10000,-2,0)
    

    get R to compute the histogram but not plot it:

    h = hist(test, breaks=100,plot=FALSE)
    

    Your histogram is divided into three parts:

    ccat = cut(h$breaks, c(-Inf, -0.6, -0.4, Inf))
    

    plot with this palette, implicit conversion of factor to number indexes the palette:

    plot(h, col=c("white","green","red")[ccat])
    

    coloured bars

    ggplot2 solutions are also available.

提交回复
热议问题