[R] Lattice: relation = 'free' in scales

Deepayan Sarkar deepayan.sarkar at gmail.com
Wed May 26 15:56:02 CEST 2010

```On Tue, May 25, 2010 at 9:30 PM, Peter Ehlers <ehlers at ucalgary.ca> wrote:
> Albart,
>
> On 2010-05-25 1:51, Albart wrote:
>>
>> Hello list,
>>
>> I am making graphics for an article which I want to publish. The article
>> is
>> about several methods (to calculate breeding values of individuals)
>> applied
>> in several genetic scenarios (scen1 in the example) and using data from
>> two
>> sources (scen 2 in my example). I want to specify the ylim of my plot and
>> have relation = 'free' for the yaxis but I would to avoid plotting the
>> axis
>> for each subplot. I include an example to clarify my problem.
>>
>> require(lattice)
>> nmethods<- 3
>> nscen1<- 3
>> nscen2<- 2
>> gens<- 1:10
>> tab<- expand.grid(method = 1:nmethods,
>>                    scen1 = 1:nscen1,
>>                    scen2 = 1:nscen2,
>>                    gen = gens,
>>                    rep = 1:3)
>> tab\$value<- rnorm(nrow(tab),mean = with(tab,method*1000 + scen1*10 +
>> scen2*10 + gen*100),sd = 100)
>>
>> tab\$color<- factor(tab\$scen2)
>> levels(tab\$color)<- rainbow(length(levels(tab\$color)))
>> tab\$pch<- tab\$color
>> levels(tab\$pch)<- seq(21,21 + length(levels(tab\$pch)))
>> tab\$pch<- as.numeric(as.character(tab\$pch))
>> tab\$color<- as.character(tab\$color)
>>
>> ylim<- with(tab,tapply(value,method,range))
>> ylim<- ylim[rep(seq(1,length(ylim)),each = length(unique(tab\$scen1)))]
>> tab\$method<- factor(tab\$method)
>> tab\$scen1<- factor(tab\$scen1)
>> pplot<- with(tab,xyplot(value~gen|scen1*method,groups = scen2,as.table =
>> TRUE,
>>                          color = color,pch = pch,
>>                          scales = list(alternating = FALSE,y =
>> list(relation
>> = 'free',limits = ylim)),
>>                          panel = function(x,y,groups,...,subscripts){
>>
>> tapply(1:length(y),list(x,groups[subscripts]),function(ii){
>>                              color<- color[subscripts[ii]]
>>                              pch<- pch[subscripts[ii]]
>>                              panel.xyplot(mean(x[ii]),mean(y[ii]),fill =
>> color,col = color,pch = pch,cex= 0.5)
>>                            })},
>>                            key = list(columns = 2,
>>                              space = "top",text =
>> list(unique(as.character(scen2)),col = unique(color)),
>>                              points = list(pch = unique(pch),fill =
>> unique(color),col = unique(color)))))
>>
>> As you can see, I use scales = list(y = list(relation = 'free', limit =
>> ylim)) because the range differs between the methods and otherwise I loose
>> all resolution. I like the result except that the yaxis is now displayed
>> for
>> each plot and not only at the left side of the plot. Since I want the same
>> plotting range for each row of the plot, this represents an unnecessary
>> use
>> of space and I would like to remove these axis from my plot, but I am not
>> able to do so. I would appreciate any help.
>>
>
> # You can achieve your goal by replacing
>
> scales = list(alternating = FALSE,
>  y = list(relation = 'free', limits = ylim)),
>
> # with
>
> L <- vector('list', 9), is.na(L[c(1,4,7)]) <- TRUE,
> scales = list(alternating = FALSE,
>  y = list(relation= 'free', limits = ylim, at = L)),
>
> # or, equivalently, with
>
> scales = list(alternating = FALSE,
>  y = list(relation= 'free', limits = ylim,
>    at = list(NA,NULL,NULL,NA,NULL,NULL,NA,NULL,NULL))),
>
> # If you want to move panels closer together, you can add
>
> between = list(x = -1.5),
>
> # to your pplot code.

Better yet:

par.settings = list(layout.widths = list(axis.panel = c(1, 0, 0)))

-Deepayan

```