[R] Grouped Histogram (colored)

Greg Snow Greg.Snow at imail.org
Thu Oct 16 19:12:21 CEST 2008


Does this do what you want?

colhist <- function(x,col){
         tmp <- hist(x,plot=F)
         br <- tmp$breaks
         w <- as.numeric(cut(x,br,include.lowest=TRUE))
         sy <- unlist(lapply(tmp$counts,function(x)seq(length=x)))
         sy <- sy[order(order(x))]
            plot( range(br), range( 0, sy ), xlab=deparse(substitute(x)),
                        ylab='Frequency', type='n')
         rect(br[w], sy-1, br[w+1], sy,
            col=col,
            border=NA)
         rect(br[-length(br)], 0, br[-1], tmp$counts)
     }

x <- rnorm(75, rep( c(90,100,110), each=25), 5 )
g <- rep( c('red','green','blue'), each=25 )

colhist(x,g)

note: this colhist function is a modified version of the one from the help file for the tkBrush function in the TeachingDemos package.

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of x0rr0x
> Sent: Thursday, October 16, 2008 10:42 AM
> To: r-help at r-project.org
> Subject: [R] Grouped Histogram (colored)
>
>
> Hi all,
>
> I'm trying to create a histogram which shows the frequency of variables
> within a certain timeframe.
>
> I've been using SPSS before, but I didn't quite like it...
>
> To describe my problem further here are some example variables:
>
> the "event" is actually a string which I recoded using:
> [code]
> data$event_class = as.numeric(as.factor(data$event))
> [/code]
> I've recoded them into numerics
>
>
> csv:
> [code]
> time,event,event_class
> 01,cookies,1
> 05,cookies,1
> 06,pie,2
> 07,coffee,3
> 08,cookies,1
> 30,pie,2
> 31,coffee,3
> [/code]
> and so on...
>
> Now I'd like to create a histogram where X is the time, the color of
> the
> area is the event_class
> and Y is defined by the frequency of event_class around some
> accumulated
> time
>
>
>
> In SPSS I used this:
> Graphs -> Chart Builder
> Gallery->Histogram
> use some horizontal histogram
> put "time" on the x-axis
> select "grouping/stacking variables" in the "groups/point id" tab
> and then set "Stack: set-color" to event_class
> the y-axis will be automatically set to "histogram"
>
> thanks a lot in advance!
>
> Regards,
> - x0rr0x
> --
> View this message in context: http://www.nabble.com/Grouped-Histogram-
> %28colored%29-tp20015941p20015941.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.



More information about the R-help mailing list