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

David Perlman dperlman at wisc.edu
Wed May 9 18:17:52 CEST 2012


Thanks, that is very helpful.  I agree that my example plot was a bit cluttered, but this is what I actually wanted:
http://brainimaging.waisman.wisc.edu/~perlman/data/MNPT1T2_h_unp_raw.pdf
I just needed to get example code out quickly.  You get better help when you have a self-contained demo of the question.  :)

I have replaced my old horrible code with the nice concise segments code.  Thanks!


On May 9, 2012, at 3:55 AM, Jim Lemon wrote:

> 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

-dave----------------------------------------------------------------------
A neuroscientist is at the video arcade, when someone makes him a $1000 bet
on Pac-Man. He smiles, gets out his screwdriver and takes apart the Pac-Man
game. Everyone says "What are you doing?" The neuroscientist says "Well,
since we all know that Pac-Man is based on electric signals traveling
through these circuits, obviously I can understand it better than the other
guy by going straight to the source!"



More information about the R-help mailing list