Search code examples
rgraphicshistogram

Plot with conditional colors based on values in hist


Is it possible to have the bars painted in only one color whose condition is X < 980? in the histogram below.

R code:

Sample <- rnorm(2500,1000,20) # 2500 norm values
plot(Sample, col = ifelse(Sample < 980,'red','green')) # Paint in 'red' when X<980

enter image description here


Solution

  • You could determine the colors according to the mid-point of each bin.

    Sample <- rnorm(2500, 1000, 20)
    h <- hist(Sample, plot = FALSE)
    hist(Sample, col = ifelse(h$mids < 980, 'red', 'green'))
    

    enter image description here