Re: [R] dotplot as a background for multiple barchart plots (with Lattice)

From: Deepayan Sarkar <deepayan.sarkar_at_gmail.com>
Date: Fri, 08 Apr 2011 09:42:50 +0530

On Thu, Apr 7, 2011 at 7:16 PM, Christian Charles <christian.charles_at_inrialpes.fr> wrote:
> Hi R users,
>
> I'm kind stuck in my R experience...
> I want to have a multiple barchart with n windows, but with the same
> background for each window, and I want that background to be a recorded
> dotplot.
> First, is it possible ?
> If so, I guess I have to call a record plot in the panel function but how ?
> As there is no panel.plot and I can't use the recorded plot as an argument
> for the panel.dotplot() call, I'm stuck.
> I think I close but I need some help.
>
> Many thanks and regards,
>
> Christian
>
>
> ### dummy data building
> test <- data.frame(Subject=rep(c("A", "B", "C", "D"), each=6),
>                   Type=rep(c("EEE", "FFF", "GGG", "HHH", "III", "JJJ"),
> times=4),
>                   MeanX=abs(rnorm(24)),
>                   MeanZ=abs(rnorm(24)))
>
> ### data for regrouping all means
> all <- data.frame(Type=levels(factor(test$Type)), MeanX = c(0), MeanZ =
> c(0))
>
> ### building means...
> for(s in levels(factor(test$Subject)))
> {
>    tmp <- data.frame(Type = all$Type)
>    for(t in tmp$Type)
>    {
>            tmp$MeanX[tmp$Type == t] <- mean(test$MeanX[test$Type==t &
> test$Subject==s])
>            tmp$MeanZ[tmp$Type == t] <- mean(test$MeanZ[test$Type==t &
> test$Subject==s])
>    }
>    all$MeanX = all$MeanX + tmp$MeanX
>    all$MeanZ = all$MeanZ + tmp$MeanZ
> }
> l <- length(levels(factor(test$Subject)))
> all$MeanX = all$MeanX/l
> all$MeanX = all$MeanZ/l
>
> ### plot for means
> meanplot <- dotplot(MeanX+MeanZ ~ Type, data=all, cex = 1.2, xlab="",
> ylab="",
>    panel = panel.superpose,
>    panel.groups = function(x, ..., group.number){
>    panel.dotplot(x + (group.number - 1.5)/3, ... )
>    })
>
> ### test plot in order to see if there is something wrong.
> plot(meanplot)
>
> ### final plot
> barchart(    MeanX + MeanZ ~ Type|Subject,
>         data=test ,
>         scales = list(x = list(rot = 60)),
>         layout=c(1, l),
>         panel = function(...) {
>            panel.superpose
>            #panel.dotplot(meanplot) # so this is want I want, my recorded
> dotplot as a background...
>            panel.abline(h=0)
>            panel.grid(v = -length(levels(factor(test$Type))), h=0)
>             panel.barchart(...)
>            })

Here is a literal solution (plot the 'meanplot' object in each panel):

barchart(MeanX + MeanZ ~ Type|Subject,

         data=test ,
         scales = list(x = list(rot = 60)),
         layout=c(1, l),
         panel = function(...) {
             plot(meanplot, newpage = FALSE)
             panel.abline(h=0)
             panel.grid(v = -length(levels(factor(test$Type))), h=0)
             panel.barchart(...)
         })

but a better solution may be to draw just the panel part (which doesn't work very well because the y-limits are different).

barchart(MeanX + MeanZ ~ Type | Subject,

         data=test ,
         scales = list(x = list(rot = 60)),
         layout=c(1, l),
         panel = function(...) {
             mpargs <- trellis.panelArgs(meanplot, 1)
             do.call(meanplot$panel, mpargs)
             panel.abline(h=0)
             panel.grid(v = -length(levels(factor(test$Type))), h=0)
             panel.barchart(...)
         })

-Deepayan



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. Received on Fri 08 Apr 2011 - 04:14:58 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 08 Apr 2011 - 07:50:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive