[R] There must be a better way to do this

Jim Lemon jim at bitwrit.com.au
Wed May 9 10:55:18 CEST 2012


On 05/09/2012 03:59 AM, David Perlman wrote:
> I made this rather cool plot which I am quite pleased with:
> http://brainimaging.waisman.wisc.edu/~perlman/data/BeeswarmLinesDemo.pdf
>
> However, I feel there must be a better way to do it than what I did.  I'm attaching the code to create it, which downloads the data by http so it should run for you if you have the current version of beeswarm installed (which was just updated today, incidentally).  It might also work with a non-current version of beeswarm.
>
> The problem is that I jumped through all kinds of hoops to:
>
> a) get the subject numbers for each point associated with the point xy coordinates output by beeswarm.  The order of the points is not the same as the order in the input file; they are shuffled in a way that I think depends on the input formula.  The trick I used (ok, I hope you're sitting down when you read this) is to run beeswarm a second time with pwcol=Subj, so then the "col" column of the output becomes the subject numbers.  I know, horrible.  But I don't know how else to do it.  I feel like there is probably some logic to the way the cases were reordered by the formula, but I don't know how to work with that.
>
> b) get the lines() function to pair the xy coordinates properly.  I did this by reshaping the whole thing into wide format, with separate columns for x.1 y.1 x.2 y.2, and then add a third pair of columns x.3 y.3 which is all NA, and then reshaping it back into long format.  Then the lines() function automatically does the right thing, but I feel like that was a horrible hack and there must be a smarter way to do it.
>
>
Hi Dave,
This plot looks like the offspring of a boxplot, a beeswarm plot and a 
bumpchart after a heavy night on the grog. Beauty is in the eye of the 
beholder, I guess.

Let's see, first you plot the boxplots, then the beeswarm on the 
centerlines of the boxplots, then you want to add the lines. Okay, try this:

paindat<-data.frame(
  HEP1=sample(1:20,30,TRUE,
  prob=c(seq(0,0.1,length.out=10),seq(0.1,0,length.out=10))),
  HEP2=sample(1:20,30,TRUE,
  prob=c(seq(0,0.1,length.out=10),seq(0.1,0,length.out=10))),
  MBSR1=sample(1:20,30,TRUE,
  prob=c(seq(0,0.1,length.out=10),seq(0.1,0,length.out=10))),
  MBSR2=sample(1:20,30,TRUE,
  prob=c(seq(0,0.1,length.out=10),seq(0.1,0,length.out=10))),
  Wait1=sample(1:20,30,TRUE,
  prob=c(seq(0,0.1,length.out=10),seq(0.1,0,length.out=10))),
  Wait2=sample(1:20,30,TRUE,
  prob=c(seq(0,0.1,length.out=10),seq(0.1,0,length.out=10))))
boxplot(paindat,ylim=c(0,20),
  col=c("pink","pink","lightgreen","lightgreen","lightblue","lightblue"))
require(beeswarm)
bsinfo<-beeswarm(tangledat,add=TRUE)
segments(bsinfo$x[bsinfo$x.orig=="HEP1"],bsinfo$y[bsinfo$x.orig=="HEP1"],
  bsinfo$x[bsinfo$x.orig=="HEP2"],bsinfo$y[bsinfo$x.orig=="HEP2"])
segments(bsinfo$x[bsinfo$x.orig=="MBSR1"],bsinfo$y[bsinfo$x.orig=="MBSR1"],
  bsinfo$x[bsinfo$x.orig=="MBSR2"],bsinfo$y[bsinfo$x.orig=="MBSR2"])
segments(bsinfo$x[bsinfo$x.orig=="Wait1"],bsinfo$y[bsinfo$x.orig=="Wait1"],
  bsinfo$x[bsinfo$x.orig=="Wait2"],bsinfo$y[bsinfo$x.orig=="Wait2"])

and let me say right here that the beeswarm function is a crackerjack 
piece of work.

Jim



More information about the R-help mailing list