Karin Lagesen
2010-Nov-10 16:27 UTC
[R] plotting histograms/density plots in a triangular layout?
Hi! I have a set of 49 pairwise comparisons that I have done. From this I would like to plot either histograms or the density plots of the values I get. Now, I can plot one histogram per comparison, but I have problems getting the output I want. When plotting like I normally would do: histogram(~percid | orgA_orgB, data = alldata) I get the histograms next to eachother in a boxlike shape. However, since these are pairwise ( 7x7 ) I would like to have them placed in a triangular shape, like this: 1 x 2 x x 3 x x x 1 3 3 where the Xes represent where I want plots, and the 1,2,3 represent the legends. I have seen similar plots done by R, so I know it is possible, but the question is how :D TIA, Karin -- Karin Lagesen, Ph.D. Centre for Ecological and Evolutionary Synthesis (CEES) University of Oslo, Dept. of Biology P.O. Box 1066 Blindern 0316 Oslo, Norway Ph. +47 22844132 Fax. +47 22854001 Email karin.lagesen at bio.uio.no http://folk.uio.no/karinlag
Phil Spector
2010-Nov-10 19:00 UTC
[R] plotting histograms/density plots in a triangular layout?
Karin - An example would have been nice. Perhaps this will be helpful:> somedat = data.frame(x=sample(1:7,1000,replace=TRUE),+ y=sample(1:7,1000,replace=TRUE), + z=rnorm(1000))> somedat$grp = interaction(somedat$x,somedat$y)Now we need to order the groups the way they need to be plotted:> levs = paste(rep(1:7,1:7),unlist(sapply(1:7,function(x)1:x)),sep='.') > levs[1] "1.1" "2.1" "2.2" "3.1" "3.2" "3.3" "4.1" "4.2" "4.3" "4.4" "5.1" "5.2" [13] "5.3" "5.4" "5.5" "6.1" "6.2" "6.3" "6.4" "6.5" "6.6" "7.1" "7.2" "7.3" [25] "7.4" "7.5" "7.6" "7.7"> somedat$grp = factor(somedat$grp,levels = levs)Of course, you're ordering may be different, but I don't know what the levels of your grouping factor are. Now we can use the skip= argument to histogram to tell it which plots to skip:> chk = rep(rep(c(FALSE,TRUE),7),c(1,6,2,5,3,4,4,3,5,2,6,1,7,0)) > chk[1] FALSE TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE [13] TRUE TRUE FALSE FALSE FALSE TRUE TRUE TRUE TRUE FALSE FALSE FALSE [25] FALSE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE [37] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE [49] FALSE> histogram(~z|grp,data=somedat,skip=chk,layout=c(7,7),as.table=TRUE)I'm not sure what you mean by the legends, but maybe this will give you a start. - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Wed, 10 Nov 2010, Karin Lagesen wrote:> Hi! > > I have a set of 49 pairwise comparisons that I have done. From this I would > like to plot either histograms or the density plots of the values I get. Now, > I can plot one histogram per comparison, but I have problems getting the > output I want. When plotting like I normally would do: > > histogram(~percid | orgA_orgB, data = alldata) > > I get the histograms next to eachother in a boxlike shape. However, since > these are pairwise ( 7x7 ) I would like to have them placed in a triangular > shape, like this: > > > 1 x > 2 x x > 3 x x x > 1 3 3 > > where the Xes represent where I want plots, and the 1,2,3 represent the > legends. > > I have seen similar plots done by R, so I know it is possible, but the > question is how :D > > TIA, > > Karin > -- > Karin Lagesen, Ph.D. > Centre for Ecological and Evolutionary Synthesis (CEES) > University of Oslo, Dept. of Biology > P.O. Box 1066 Blindern 0316 Oslo, Norway > Ph. +47 22844132 Fax. +47 22854001 > Email karin.lagesen at bio.uio.no > http://folk.uio.no/karinlag > > ______________________________________________ > 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. >