Hi, in order to save space for a publication, it would be nice to have a combined scatter and density plot similar to what is shows on http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=78 I wonder if anybody perhaps has already developed code for this and is willing to share. This is the reproducible code for the histogram version obtained from the site: def.par <- par(no.readonly = TRUE) # save default, for resetting... x <- pmin(3, pmax(-3, rnorm(50))) y <- pmin(3, pmax(-3, rnorm(50))) xhist <- hist(x, breaks=seq(-3,3,0.5), plot=FALSE) yhist <- hist(y, breaks=seq(-3,3,0.5), plot=FALSE) top <- max(c(xhist$counts, yhist$counts)) xrange <- c(-3,3) yrange <- c(-3,3) nf <- layout(matrix(c(2,0,1,3),2,2,byrow=TRUE), c(3,1), c(1,3), TRUE) par(mar=c(3,3,1,1)) plot(x, y, xlim=xrange, ylim=yrange, xlab="", ylab="") par(mar=c(0,3,1,1)) barplot(xhist$counts, axes=FALSE, ylim=c(0, top), space=0) par(mar=c(3,0,1,1)) barplot(yhist$counts, axes=FALSE, xlim=c(0, top), space=0, horiz=TRUE) par(def.par) I am basically stuck from line 6 where the bin information from the histogram is used for determining plotting sizes. Density are different and don't have (equal) bins and their size would need to be determined differently. I wonder if somebody here has created such a diagram already and is willing to share ideas/code/pointers to similar examples. Your effort is highly appreciated. Thanks a lot, Ralf
Hi Ralf, I am sure there is a function to do exactly that but I am not sure what is the name and the package (Try plotrix or gregmisc...). To help you with your problem we would need a reproducible example with your data, not the example which obviously works. As a guess, when you run info <- hist(x) it should save you all the information regarding bin sizes etc into "info" and you can start from there. That should also work with probabilities. Best Jannis --- Ralf B <ralf.bierig at gmail.com> schrieb am Di, 21.9.2010:> Von: Ralf B <ralf.bierig at gmail.com> > Betreff: [R] Combined plot: Scatter + density plot > An: "r-help Mailing List" <r-help at r-project.org> > Datum: Dienstag, 21. September, 2010 05:34 Uhr > Hi, > > in order to save space for a publication, it would be nice > to have a > combined scatter and density plot similar to what is shows > on > > http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=78 > > I wonder if anybody perhaps has already developed code for > this and is > willing to share. This is the reproducible code for the > histogram > version obtained from the site: > > def.par <- par(no.readonly = TRUE) # save default, for > resetting... > x <- pmin(3, pmax(-3, rnorm(50))) > y <- pmin(3, pmax(-3, rnorm(50))) > xhist <- hist(x, breaks=seq(-3,3,0.5), plot=FALSE) > yhist <- hist(y, breaks=seq(-3,3,0.5), plot=FALSE) > top <- max(c(xhist$counts, yhist$counts)) > xrange <- c(-3,3) > yrange <- c(-3,3) > nf <- layout(matrix(c(2,0,1,3),2,2,byrow=TRUE), c(3,1), > c(1,3), TRUE) > par(mar=c(3,3,1,1)) > plot(x, y, xlim=xrange, ylim=yrange, xlab="", ylab="") > par(mar=c(0,3,1,1)) > barplot(xhist$counts, axes=FALSE, ylim=c(0, top), space=0) > par(mar=c(3,0,1,1)) > barplot(yhist$counts, axes=FALSE, xlim=c(0, top), space=0, > horiz=TRUE) > > par(def.par) > > I am basically stuck from line 6 where the bin information > from the > histogram is used for determining plotting sizes. Density > are > different and don't have (equal) bins and their size would > need to be > determined differently. I wonder if somebody here has > created such a > diagram already and is willing to share ideas/code/pointers > to similar > examples. Your effort is highly appreciated. > > Thanks a lot, > Ralf > > ______________________________________________ > 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. >
You can get exactly the plot you want (except with relative frequency polygons instead of the histograms on the marginals, but they are equivalent) with the package chplot (which is available from CRAN)! Use just one group (class, sample, category -- i.e., omit the "conditioning variable" as we call it in the package manual) and use the options to show the points and to switch off the convex hull and the display of centre and variability (if you really want to, although multiple groups and the summarised display are the main features of the "augmented convex hull plots"). Still, if you have many points, do consider the "chplots proper", or their "curved variant" (i.e., bivariate density contour instead of the points cloud and confidence ellipse instead of the cross with descriptive statistics and smoothed marginal densities instead of the frequency poligons). Regards, Gaj --- Assist. Prof. Gaj Vidmar, PhD Univ. of Ljubljana, Fac. of Medicine, Inst. for Biostatistics and Medical Informatics "Ralf B" <ralf.bierig at gmail.com> wrote in message news:AANLkTikJv_NUPQT2E2BtAJkji3q9oabgSv+UoKpqpN-J at mail.gmail.com...> Hi, > > in order to save space for a publication, it would be nice to have a > combined scatter and density plot similar to what is shows on > > http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=78 > > I wonder if anybody perhaps has already developed code for this and is > willing to share. This is the reproducible code for the histogram > version obtained from the site: > > def.par <- par(no.readonly = TRUE) # save default, for resetting... > x <- pmin(3, pmax(-3, rnorm(50))) > y <- pmin(3, pmax(-3, rnorm(50))) > xhist <- hist(x, breaks=seq(-3,3,0.5), plot=FALSE) > yhist <- hist(y, breaks=seq(-3,3,0.5), plot=FALSE) > top <- max(c(xhist$counts, yhist$counts)) > xrange <- c(-3,3) > yrange <- c(-3,3) > nf <- layout(matrix(c(2,0,1,3),2,2,byrow=TRUE), c(3,1), c(1,3), TRUE) > par(mar=c(3,3,1,1)) > plot(x, y, xlim=xrange, ylim=yrange, xlab="", ylab="") > par(mar=c(0,3,1,1)) > barplot(xhist$counts, axes=FALSE, ylim=c(0, top), space=0) > par(mar=c(3,0,1,1)) > barplot(yhist$counts, axes=FALSE, xlim=c(0, top), space=0, horiz=TRUE) > > par(def.par) > > I am basically stuck from line 6 where the bin information from the > histogram is used for determining plotting sizes. Density are > different and don't have (equal) bins and their size would need to be > determined differently. I wonder if somebody here has created such a > diagram already and is willing to share ideas/code/pointers to similar > examples. Your effort is highly appreciated. > > Thanks a lot, > Ralf >
Sorry Ralf, I was in a hurry when I replied to your plot. The function you are looking for is: dx=density(x) plot(dx) That works with the code you send in your mail. Just adjust the plot limits and change x and y for the density plot of y and it works. A good reference can be found here: http://onertipaday.blogspot.com/2007/05/rotating-distribution-plot-by-90.html Hope that helped more than my last post! Jannis --- Ralf B <ralf.bierig at gmail.com> schrieb am Di, 21.9.2010:> Von: Ralf B <ralf.bierig at gmail.com> > Betreff: [R] Combined plot: Scatter + density plot > An: "r-help Mailing List" <r-help at r-project.org> > Datum: Dienstag, 21. September, 2010 05:34 Uhr > Hi, > > in order to save space for a publication, it would be nice > to have a > combined scatter and density plot similar to what is shows > on > > http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=78 > > I wonder if anybody perhaps has already developed code for > this and is > willing to share. This is the reproducible code for the > histogram > version obtained from the site: > > def.par <- par(no.readonly = TRUE) # save default, for > resetting... > x <- pmin(3, pmax(-3, rnorm(50))) > y <- pmin(3, pmax(-3, rnorm(50))) > xhist <- hist(x, breaks=seq(-3,3,0.5), plot=FALSE) > yhist <- hist(y, breaks=seq(-3,3,0.5), plot=FALSE) > top <- max(c(xhist$counts, yhist$counts)) > xrange <- c(-3,3) > yrange <- c(-3,3) > nf <- layout(matrix(c(2,0,1,3),2,2,byrow=TRUE), c(3,1), > c(1,3), TRUE) > par(mar=c(3,3,1,1)) > plot(x, y, xlim=xrange, ylim=yrange, xlab="", ylab="") > par(mar=c(0,3,1,1)) > barplot(xhist$counts, axes=FALSE, ylim=c(0, top), space=0) > par(mar=c(3,0,1,1)) > barplot(yhist$counts, axes=FALSE, xlim=c(0, top), space=0, > horiz=TRUE) > > par(def.par) > > I am basically stuck from line 6 where the bin information > from the > histogram is used for determining plotting sizes. Density > are > different and don't have (equal) bins and their size would > need to be > determined differently. I wonder if somebody here has > created such a > diagram already and is willing to share ideas/code/pointers > to similar > examples. Your effort is highly appreciated. > > Thanks a lot, > Ralf > > ______________________________________________ > 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. >
On Sep 21, 2010, at 1:34 AM, Ralf B wrote:> Hi, > > in order to save space for a publication, it would be nice to have a > combined scatter and density plot similar to what is shows on > > http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=78 > > I wonder if anybody perhaps has already developed code for this and is > willing to share.The code for that graphic (and all graphics at that site) is linked from the page on which it appears. All you need to do is click on the R icon that is just lateral to the words "Download or view": http://addictedtor.free.fr/graphiques/sources/source_78.R -- David.> This is the reproducible code for the histogram > version obtained from the site: > > def.par <- par(no.readonly = TRUE) # save default, for resetting... > x <- pmin(3, pmax(-3, rnorm(50))) > y <- pmin(3, pmax(-3, rnorm(50))) > xhist <- hist(x, breaks=seq(-3,3,0.5), plot=FALSE) > yhist <- hist(y, breaks=seq(-3,3,0.5), plot=FALSE) > top <- max(c(xhist$counts, yhist$counts)) > xrange <- c(-3,3) > yrange <- c(-3,3) > nf <- layout(matrix(c(2,0,1,3),2,2,byrow=TRUE), c(3,1), c(1,3), TRUE) > par(mar=c(3,3,1,1)) > plot(x, y, xlim=xrange, ylim=yrange, xlab="", ylab="") > par(mar=c(0,3,1,1)) > barplot(xhist$counts, axes=FALSE, ylim=c(0, top), space=0) > par(mar=c(3,0,1,1)) > barplot(yhist$counts, axes=FALSE, xlim=c(0, top), space=0, horiz=TRUE) > > par(def.par) > > I am basically stuck from line 6 where the bin information from the > histogram is used for determining plotting sizes. Density are > different and don't have (equal) bins and their size would need to be > determined differently. I wonder if somebody here has created such a > diagram already and is willing to share ideas/code/pointers to similar > examples. Your effort is highly appreciated. > > Thanks a lot, > Ralf > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT
On Tue, Sep 21, 2010 at 8:34 AM, Ralf B <ralf.bierig at gmail.com> wrote:> in order to save space for a publication, it would be nice to have a > combined scatter and density plot similar to what is shows on >Not quite the same thing, but I like the scatterplots in Rcmdr, which feature boxplots instead of density graphs: require(car) data(iris) scatterplot(Petal.Width~Sepal.Length, reg.line=lm, smooth=TRUE, spread=TRUE, boxplots='xy', span=0.5, data=iris) Regards Liviu