Tell me more ×
Cross Validated is a question and answer site for statisticians, data analysts, data miners and data visualization experts. It's 100% free, no registration required.

I'm familiar with very basic plotting in R, but I'm not sure how best to create the reasonably-complicated plot described below.

I have developed a density estimation method that essentially fits an empirical distribution to multiple univariate samples, under distinct conditions. To be clear, let $\mathbf{x}_i^\mathrm{j}$ be a vector of univariate values for the $i^\mathrm{th}$ sample under condition $j$. My method allows me to approximate the distribution from which the values in $\mathbf{x}_i^\mathrm{j}$ were drawn. Although in principle, $j$ could take on many values, in practice I am interested in just two conditions (e.g., $j$ specifies whether the sample is for a pre- or post-intervention condition).

In particular, the method I have developed attempts to establish correspondences between common features of the fitted density estimates (e.g., modes). I want to be able to plot curves for all the density estimates in such a way as to allow comparison between the two conditions, and between corresponding features of the distributions.

I would like to create a grid of subplots (e.g., m × n if there are a total of 2 × m × n samples; note that m and n are only specified so that the overall plot has a convenient aspect ratio, they do not relate to the design of the experiment etc.). Each subplot would show two curves: one for the distribution underlying $\mathbf{x}_i^\mathrm{pre}$ and the other for $\mathbf{x}_i^\mathrm{post}$. Further, to allow the correspondences to be compared, I would like to color the line used to draw the curves using a “rainbow” (or other color map), so that one could, for example, look at the red regions of two curves and visually determine whether they do in fact correspond. To this end, I can provide vectors (x, p(x), c), where x is an arbitrary value on the horizontal axis of the density estimate plot, p(x) is the density estimate for x, and c is a value in (0, 1), such that a particular value of c should correspond across all density estimates, and should, therefore, be plotted in the same color (e.g., a value of 0.2 might sit at the peak of the mode of the distributions).

Lastly, in order to allow the two curves in each subplot to be distinguished, I'd like to be able to style each differently, for example by using a thin line for the pre condition and a thick line for the post condition.

Thanks.

share|improve this question
4  
Please give a data sample and one subplot if that is possible. – mpiktas Aug 31 '11 at 13:05
I haven't even got one subplot figured out yet. The underlying data isn't really relevant; just imagine I have 2 × m × n functions, where each takes a value x and returns a density, p(x), and color, c(x). – Chris Aug 31 '11 at 13:29
So like a planar slice through a 3D height map showing two peaks (before/after)? – user6098 Aug 31 '11 at 14:18
3  
I think this is the kind of situation where busting out microsoft paint is totally reasonable. – Brandon Bertelsen Aug 31 '11 at 16:36

1 Answer

up vote 3 down vote accepted

I'm just guessing here... "sample" below, in this case is 2 x m x n (whatever m x n are)

dat <- 
  data.frame(
    x=sort(runif(100),decreasing=T),
    px=sort(sample(1:100/100,100, replace=T),decreasing=T),
    c=sample(1:100/100,100, replace=T),
    xprepost=sample(c("xpre","xpost"),100,replace=T),
    sample=sample(1:3,100,replace=T)
    )

ggplot(dat, aes(x,px)) + geom_line(aes(color=c, size=xprepost)) + geom_point() + facet_grid(~sample)
share|improve this answer
Thanks Brandon. – Chris Sep 2 '11 at 9:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.