[R] histograms embedded in a plot (as alternative to jitter)

David Carlson dcarlson at tamu.edu
Wed Mar 5 20:56:02 CET 2014


Not histograms, but here are two alternatives. The first gives
you kernel density plots for each value and the second uses
violin plots. Both plot points if there are fewer than 5.

set.seed(42)
y<-rpois(500,2)
x<-rnorm(500,y,1)

plot(x,y, type="n")
for (i in seq(min(y), max(y), by=1)) {
  if (length(x[y==i])<5) {
    points(x[y==i], rep(i, length(x[y==i])))
  } else {
    a <- density(x[y==i])
    polygon(a$x, a$y+i, col="gray")
  }
}

require(vioplot)
plot(x,y, type="n")
for (i in seq(min(y), max(y), by=1)) {
  if (length(x[y==i])<5) {
    points(x[y==i], rep(i, length(x[y==i])))
  } else {
    vioplot(x[y==i], horizontal=TRUE, add=TRUE, at=i,
col="gray")
  }
}

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: r-help-bounces at r-project.org
[mailto:r-help-bounces at r-project.org] On Behalf Of Bernard North
Sent: Wednesday, March 5, 2014 7:44 AM
To: r-help at r-project.org
Subject: [R] histograms embedded in a plot (as alternative to
jitter)

Dear R list,

I am plotting a discrete valued number on the y axis against a
continuous variable on the x axis.
To allow sample size to be viewed for the discrete groups I am
using vertical jitter.
So my code is along the lines of
y<-rpois(500,2)
x<-rnorm(500,y,1)
plot(x,jitter(y))

It has not been suggested that a more informative view of the
sample size might be if a histogram could be inserted into the
plot (instead of the jittered rows of points) for the values at
each y-value
Many thanks if anyone can think of a way to do this





This email may contain information that is privileged,
confidential or otherwise protected from disclosure.
It must not be used by, or its contents copied or disclosed to,
persons other than the addressee.
If you have received this email in error please notify the
sender immediately and delete the email.


______________________________________________
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.




More information about the R-help mailing list