Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Created November 25, 2014 21:39
Show Gist options
  • Save abikoushi/e1869bcd7fb097b12c44 to your computer and use it in GitHub Desktop.
Save abikoushi/e1869bcd7fb097b12c44 to your computer and use it in GitHub Desktop.
paint the color on a part of the histogram.
histcol <-function(hst, from, to, col="cornflowerblue"){
bw<-diff(hst$breaks)[1]
part<-c(from,to)
xv <-seq(part[1],part[2],bw)
yv <- hst$counts[hst$breaks[-length(hst$breaks)] %in% xv]
for(k in 1:(length(xv)-1)){
polygon(sort(rep(xv[k:(k+1)], 2)), c(0,rep(yv[k], 2),0),
col=col)
}
}
@abikoushi
Copy link
Author

Argument

hst: histogram object
from: specified by the x coordinate the beginning of add color range
to: specified by the x coordinate the end of add color range
col: color

Example

x<-rnorm(100)
tmp <-hist(x)
histcol(tmp,-1,1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment