[R] xyplot lattice fine control of axes limits and thick marks (with log scale)

ilai keren at math.montana.edu
Wed Apr 11 21:29:03 CEST 2012


On Wed, Apr 11, 2012 at 7:16 AM, David Winsemius <dwinsemius at comcast.net> wrote:
>
> On Apr 11, 2012, at 9:03 AM, David Winsemius wrote:
>
>>
>> On Apr 11, 2012, at 6:28 AM, maxbre wrote:
>>
>>> hi, I just realised I want to go a little further in the control of the
>>> chart
>>> appearance and I would like to have the same number of ticks displayed in
>>> both axes of all panels
>>
>> I'm wondering if you should be using relation="free" when you have already
>> set a panel specific range for the x and y limits? I'm thinking that the
>> panel function may be reversing your earlier prepanel efforts. (No data
>> offered  ... why don't you use one of the many test datasets in the examples
>> of the lattice package?)
>
>
> On further meandering up this thread I see that you omitted the context of
> earlier data offerings, so not I in turn offer what I think is a your
> request. Change relation from "free" to "sliced"

David, you make a good point. Seems OP's long and winding road {end
quote} is slowly circling back to the origin (see the first couple of
messages in thread).

"slice" is better than "free", but isn't tick.number just a suggestion
? i.e. a better choice of n in ?pretty will override ? For example
this data (below), barely noticeable, but see panel(2,1) has 7 ticks
compare with 6 for the others.

Any one please correct me (as I find I mess with these myself too
often... :) but I think if OP wants to force equal ticks (and lose the
"pretty" axis) there is no avoiding changes to x and yscale.components
?

tm <- structure(list(name_short = structure(1:29, .Label = c("D4",
"D5", "D6a", "D6b", "D6c", "D7", "D8", "F4", "F5a", "F5b", "F6a",
"F6b", "F6c", "F6d", "F7a", "F7b", "F8", "P105", "P114", "P118",
"P123", "P126", "P156", "P157", "P167", "P169", "P189", "P77",
"P81"), class = "factor"), sub_family = structure(c(3L, 3L, 3L,
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L,
1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L), .Label = c("pcb mono-ortho subs",
"pcb non-ortho subs", "pcdd", "pcdf"), class = "factor"), tv = c(1.069,
6.461, 5.461, 12.764, 10.86, 117.912, 256.875, 452.204, 124.02,
327.856, 88.469, 61.539, 17.794, 84.117, 121.668, 13.414, 68.409,
3023.333, 428, 19454.667, 151.333, 324, 11478.667, 1220.667,
5335.333, 124.667, 1542.667, 594.667, 193.333), ms = c(1.787,
4.831, 3.456, 14.105, 10.808, 116.02, 296.957, 30.533, 21.821,
32.969, 33.767, 29.799, 12.812, 49.637, 126.522, 17.522, 106.087,
1787.5, 130, 6751.5, 81, 23, 370, 33.5, 147.5, 5.406, 18.5, 415,
69.906)), .Names = c("name_short", "sub_family", "tv", "ms"), class =
"data.frame", row.names = c(NA, -29L))

# changing to "sliced"
 xyplot(tv ~ ms | sub_family, data=tm,
     #as.table=TRUE,
     aspect="xy",
     xlab = expression(paste('ms [ fg/', m^3, ' ]', sep = '')),
     ylab = expression(paste('tv [ fg/', m^3, ' ]', sep = '')),
     scales= list(x=list(relation="sliced", log=10, cex=0.8),
                  y=list(relation="sliced", log=10, cex=0.8)),
     prepanel = function(x, y, subscripts) {
       rr<- range(cbind(x,y))
       list(xlim = rr, ylim= rr)
     },
     panel = function(x, y ,subscripts,...) {
       panel.xyplot(x, y, cex=0.8,...)
       panel.abline(a = 0, b = 1, lty = 2, col ="gray")
       panel.text(x, y, labels=tm$name_short[subscripts], cex = 0.8, pos=3,
offset=0.5, srt=0, adj=c(1,1))
     },
 subscripts=TRUE,
     xscale.components = xscale.components.logpower,
     yscale.components = yscale.components.logpower
     )

# Compare with

 xyplot(tv ~ ms | sub_family, data=tm,
     #as.table=TRUE,
     aspect="xy",
     xlab = expression(paste('ms [ fg/', m^3, ' ]', sep = '')),
     ylab = expression(paste('tv [ fg/', m^3, ' ]', sep = '')),
     scales= list(relation="free", log=10, cex=0.8),
     prepanel = function(x, y, subscripts) {
       rr<- range(cbind(x,y))
       list(xlim = rr, ylim= rr)
     },
     panel = function(x, y ,subscripts,...) {
       panel.xyplot(x, y, cex=0.8,...)
       panel.abline(a = 0, b = 1, lty = 2, col ="gray")
       panel.text(x, y, labels=tm$name_short[subscripts], cex = 0.8, pos=3,
offset=0.5, srt=0, adj=c(1,1))
     },
 subscripts=TRUE,
     xscale.components = function(...)  {
                     ans <- xscale.components.logpower(...)
                     range <- ans$num.limit
                     newtck <- round(seq(range[1],range[2],l=7),1)
                     ans$bottom$ticks$at <- newtck
                     ans$bottom$labels$at <- newtck
                     ans$bottom$labels$labels <-
parse(text=paste('10^',newtck,sep=''))
                      ans
                     } ,
     yscale.components  = function(...)  {
                     ans <- yscale.components.logpower(...)
                     range <- ans$num.limit
                     newtck <- round(seq(range[1],range[2],l=7),1)
                     ans$left$ticks$at <- newtck
                     ans$left$labels$at <- newtck
                     ans$left$labels$labels <-
parse(text=paste('10^',newtck,sep=''))
                     ans
                     }
     )

Cheers



>
> scales= list(x=list(relation="sliced", log=10, cex=0.8, tick.number=5),
>             y=list(relation="sliced", log=10, cex=0.8, tick.number=5))
>
> --
> David.
>
>
>
>>
>>
>>>    prepanel = function(x, y, subscripts) {
>>>      rr<- range(cbind(x,y))
>>>      list(xlim = rr, ylim= rr)
>>
>>
>>>    },
>>>    panel = function(x, y ,subscripts,...) {
>>>      panel.xyplot(x, y, cex=0.8,...)
>>>      panel.abline(a = 0, b = 1, lty = 2, col ="gray")
>>>      panel.text(x, y, labels=tm$name_short[subscripts], cex = 0.8, pos=3,
>>> offset=0.5, srt=0, adj=c(1,1))
>>>    },
>>> subscripts=TRUE,
>>>    xscale.components = xscale.components.logpower,
>>>    yscale.components = yscale.components.logpower
>>>    )
>>>
>>> ...I have been trying to insert in the 'prepanel' and also in the 'panel'
>>> the statement 'tick.number=5' but this does not seem to have any effect
>>>
>>> some useful hints for this?
>>>
>>> thanks a lot
>>>
>>
>> David Winsemius, MD
>> West Hartford, CT
>>
>> ______________________________________________
>> 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.
>
>
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> 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.



More information about the R-help mailing list