[R] Histogram of data with categorical varialbe

Achim Zeileis Achim.Zeileis at wu-wien.ac.at
Fri Sep 15 17:36:57 CEST 2006


On Fri, 15 Sep 2006 16:45:31 +0200 Alexandre Depire wrote:

> Hello,
> I have the following data:
> Km Sex
> 250 1
> 300 2
> 290 2
> 600 1
> 450 2
> 650 1
> .........
> 
> I would like to obtain one histogram where the data (or the part) of
> each sex is visible, it is like cumulative histogram or spinogram.
> To be more comprehensible, i would like to know if the following
> graph is obtainable easily in R. It is the first graph on page 5 in
> the following document
> http://jasp.ism.ac.jp/~nakanoj/workshop04/TalkII.pdf

I think it's not available out of the box, but you can easily generate
this yourself. Using the space shuttle o-ring data as an example (see
also ?spineplot):

  fail <- factor(c(2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1,
    1, 2, 1, 1, 1, 1, 1), levels = c(1, 2), labels = c("no", "yes"))
  temperature <- c(53, 57, 58, 63, 66, 67, 67, 67, 68, 69, 70, 70,
    70, 70, 72, 73, 75, 75, 76, 76, 78, 79, 81)

The you can do the following:

  ## generate unconditional histogram of numeric variable P(x)
  col <- gray.colors(2)
  ht <- hist(temperature, freq = FALSE, col = col[2])
  ## using the same breaks, compute the conditional histogram P(x|group)
  br <- ht$breaks
  ht2 <- hist(temperature[fail == "yes"], plot = FALSE, freq = FALSE,
    breaks = br)
  ## and then add the rectangles using the joint densities
  ## P(x & group) = P(x|group) * P(group)
  rect(br[-length(br)], 0, br[-1], ht2$density * mean(fail == "yes"),
    col = col[1])

Furthermore, you can take a look at the "iplots" package which should
provide an interactive approach to this.
Z
 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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