Anyone know a quick way to color one bar of a histogram? I want to mark the bar in which the most recent observation falls. So, for instance: x <- rnorm(100) latest.ob <- x[100] hist(x) ## how do I mark the bucket that latest.ob falls into? Thanks, Whit
Whit Armstrong wrote:> Anyone know a quick way to color one bar of a histogram? > > I want to mark the bar in which the most recent observation falls. > > So, for instance: > > x <- rnorm(100) > latest.ob <- x[100] > hist(x) > ## how do I mark the bucket that latest.ob falls into? >Something like this: x <- rnorm(100) (latest.ob <- x[100]) h <- hist(x) bin <- as.numeric(cut(latest.ob, h$breaks)) # need as.numeric for bin+1 lw <- h$breaks[bin] up <- h$breaks[bin+1] sz <- h$counts[bin] rect(lw,0,up,sz,col="red") -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Other option without rect is: h <- hist(x, plot = FALSE) bin <- as.numeric(cut(latest.ob, h$breaks)) plot(h, col = replace(rep("white", length(h$breaks) - 1), bin, "red")) On Tue, Oct 28, 2008 at 4:48 PM, Whit Armstrong <armstrong.whit@gmail.com>wrote:> Anyone know a quick way to color one bar of a histogram? > > I want to mark the bar in which the most recent observation falls. > > So, for instance: > > x <- rnorm(100) > latest.ob <- x[100] > hist(x) > ## how do I mark the bucket that latest.ob falls into? > > > Thanks, > Whit > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]