[R] dot plot with several points for 2 categories

Steve Lianoglou mailinglist.honeypot at gmail.com
Wed Jul 29 18:40:12 CEST 2009


Hi,

On Jul 29, 2009, at 12:11 PM, jaregi wrote:

>
> Hi Michael, Steve, and 1Rnwb,
>
> I'm very impressed by the quick replies on the mailer. Thanks a lot  
> for your
> suggestions. They worked very well.
>
> In general, I have to say that I'm a bit disappointed that in R,  
> like in
> Excel, one basically needs to hack to get a dot blot with categories  
> by
> adding an artificial x value. I was hoping this common type of  
> diagram could
> be generated using a standard function like dotplot or dotchart.

Is it really that disappointing?

I've never used excel for anything serious, so I don't know what life  
is like there, but in R you can make your own functions in order to  
encapsulate functionality you find useful. So, for instance, let's  
take the code I used before and wrap it up a bit nicer:

dotplot <- function(..., labels=NULL, cols=NULL, main="My Awesome  
Dotplot") {
   stuff <- list(...)
   lower <- min(sapply(stuff, min))
   upper <- max(sapply(stuff, max))

   if (is.null(cols)) {
     cols <- rainbow(length(stuff))
   }
   plot(jitter(rep(1, length(stuff[[1]]))), stuff[[1]],  
ylim=c(lower,upper),
        xlim=c(0.5,length(stuff)+0.5), xlab=NULL, xaxt='n', col=cols[1],
        main=main)

   if (length(stuff) > 1) {
     for (i in 2:length(stuff)) {
       points(jitter(rep(i, length(stuff[[i]]))), stuff[[i]],  
col=cols[i])
     }
   }

   if (is.null(labels)) {
     labels <- paste("Group", seq(stuff))
   }
   axis(1, seq(stuff), labels=labels)
}

Copy that function up there and either (i) paste it into your R  
workspace, or (ii) save it to a file and source('/path/to/dotplot.R').

Now look at the difference between these two:

dotplot(rnorm(20,10,2), rnorm(20,15,1), rnorm(10,15,0.1))
dotplot(rnorm(20,10,2), rnorm(20,15,1), rnorm(10,15,0.1),  
cols=c('red','green','blue'), labels=c('A', 'B', 'C'), main="Custom  
Colors")

See ?plot ?title ?par to tweak the plotting functionality as you wish.

-steve

--
Steve Lianoglou
Graduate Student: Computational Systems Biology
   |  Memorial Sloan-Kettering Cancer Center
   |  Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact




More information about the R-help mailing list