Take the 2-minute tour ×

I've tried several approaches, including qqmath, lattice densityplot() and a number of panel functions like panel.mathdensity and panel.densityplot. However, I couldn't get them to do what I want them to do.

An internet search on this topic produces results which focus either on base plots in R or don't draw both distributions in one panel. I could use R base graphics, however, I also want to plot several distribution pairs and one panel for each pair.

The books "RGraphics" and "Lattice: Multivariate Data Visualization with R" couldn't enlighten me in this area either.

The data usually looks something like this:

data <- dgamma(seq(from=0.00001,to=0.01,by=0.00001),shape = .1, scale = .01)

I'm open to either lattice or ggplot package, although I have more experience using lattice.

Please let me know if you need any more information to help me out here.

share|improve this question
Base graphics with par(mfrow)? –  Ari B. Friedman Oct 20 '13 at 12:11
add comment

1 Answer

up vote 1 down vote accepted

You have only given one data-object but are asking for two to be constructed, so I'll try for two items of the same distribution with different parameters. When working with lattice or ggplot2 one need to construct a dataframe of the proper arrangement which is usually a "long" format for the data. The "group" parameter specifies plotting in the same panel with different colors.

?lattice  # essential reading
dfrm <- data.frame(dgam = data, param="s.1.01")
dfrm <- rbind(dfrm, data.frame(dgam =
                                 shape = .2, scale = .01), 
                               param="s.2.01") )
dfrm <- cbind( dfrm, X.val=seq(from=0.00001,to=0.01,by=0.00001) )
#'data.frame':  2000 obs. of  3 variables:
# $ dgam : num  5263 2817 1954 1507 1231 ...
# $ param: Factor w/ 2 levels "s.1.01","s.2.01": 1 1 1 1 1 1 1 1 1 1 ...
# $ X.val: num  1e-05 2e-05 3e-05 4e-05 5e-05 6e-05 7e-05 8e-05 9e-05 1e-04 ...
xyplot( dgam ~ X.val , 
        data=dfrm, type="l")
share|improve this answer
I didn't make that explicitly clear in my question but I wanted a densityplot. However, this approach works perfectly fine for densityplots if you exchange ´xyplot(...)` by densityplot( ~dgam,group=param,data=dfrm). –  strom Oct 22 '13 at 13:32
add comment

Your Answer


