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

Christian Charles christian.charles at inrialpes.fr
Fri Apr 8 09:43:15 CEST 2011


Thanks,

The first literal solution does not do what I want: the 'meanplot' was 
not a background but a smaller object in the middle of the barcharts.
The second solution is exactly what I want. I just changed the plotting 
order and the global ylim in order to have a better plot.

Thanks for your help.

Regards,

Christian

The final solution :
m <- max(all$MeanX, all$MeanZ, test$MeanX, test$MeanZ)

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

On 08/04/2011 06:12, Deepayan Sarkar wrote:
> 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$MeanZ = 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



More information about the R-help mailing list