[R] color individual bar of histogram?

Peter Dalgaard P.Dalgaard at biostat.ku.dk
Tue Oct 28 20:44:45 CET 2008


Henrique Dallazuanna wrote:
> 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"))

Nice. Some small improvements seem possible:

h <- hist(x, plot = FALSE)
bin <- cut(latest.ob, h$breaks)
clr <- rep("white", length(h$counts))
clr[bin] <- "red"
plot(h, col=clr)

I.e., you don't need as.numeric for simple indexing with "bin", and it
is easier to get the number of bins by counting something other than
h$breaks. I also avoided the inlined replace() construct, but that is
possibly more of a matter of taste.

> 
> On Tue, Oct 28, 2008 at 4:48 PM, Whit Armstrong <armstrong.whit at 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 at 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.
>>
> 
> 
> 
> 
> ------------------------------------------------------------------------
> 
> ______________________________________________
> R-help at 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.


-- 
   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



More information about the R-help mailing list