[R] lattice: how to use a log scale on the x-axis with the bwplot function

Laurent Rhelp L@urentRHe|p @end|ng |rom |ree@|r
Sun Dec 11 18:35:44 CET 2022


Excellent, it works.

But, may you please explain me how xyplot knows that it has to apply 
panel.bwplot on every groups according to the groups factor ? Because 
there is only one panel. I introduced the groups argument in order to 
apply the bwplot function only on the values of every group.


Le 11/12/2022 à 17:48, Deepayan Sarkar a écrit :
> On Sun, Dec 11, 2022 at 2:33 PM Laurent Rhelp <LaurentRHelp using free.fr> wrote:
>> I understand the idea but I did not succeed.
>>
>> Here is what I tried:
>>
>> ## 1.    middles of classes calculation
>>
>> m <- tapply(DF$x, groups, mean)
>>
>> ## 2.     create a new factor columns with the levels deduced from
>> ##         the values of the middles of the classes
>> ##
>>
>> DF$m <- DF$groups
>> levels(DF$m) <- as.character(m)
>> DF$m <- as.numeric(levels(DF$m))[DF$m]
>>
>> ## 3. I verify with xyplot
>>
>> xyplot(   y ~ m
>>             , type = "p"
>>             , data=DF
>>             , scales = list(
>>               y = list(log=T)
>>               , x = list(log=T)
>>
>>             )
>> )
>> ## 4. I use the panel.groups and panel.bwplot to display the boxplots
>> without success
>>
>> xyplot(   y ~ m
>>             , groups = groups
>>             , type = "p"
>>             , data=DF
>>             , scales = list(
>>               y = list(log=T)
>>               , x = list(log=T)
>>               , panel = panel.superpose
>>               ,  panel.groups=function(x,y, group.number,...){
>>                 panel.xyplot(x,y,...)
>>                 panel.bwplot(x,y,...)
>>               }
>>               , horizontal = FALSE
>>               , box.width = .0001
>>             )
>> )
> You have a typo here: x = list(log=T) should be followed by a
> close-paren to complete scales.
>
> But I don't understand from your specification why you need groups and
> panel.superpose etc. What is wrong with
>
> xyplot( y ~ m
>       ## , groups = groups # why?
>       , data=DF
>       , scales = list(
>             y = list(log=T)
>           , x = list(log=T)
>         )
>       , panel = panel.bwplot
>       , horizontal = FALSE
>       , box.width = .0001
>         )
>
> ?
>
> And sorry for my wrong calculation of m earlier, but you can simplify
> your version to
>
> m <- tapply(DF$x, groups, mean)
> DF$m <- as.vector(m[DF$groups])
>
> Best,
> -Deepayan
>
>>
>> thx
>>
>>
>>
>> Le 10/12/2022 à 17:02, Deepayan Sarkar a écrit :
>>> Log-scales for the "factor" variable in bwplot() is not allowed.
>>>
>>> You could, however, use the panel function panel.bwplot() with
>>> xyplot(num ~ num). The potential problem with that is the box widths,
>>> which panel.bwplot() will not know how to compute.
>>>
>>> See if the following gives you a reasonable starting point:
>>>
>>> DF <- within(DF, m <- tapply(y, groups, mean))
>>> xyplot(y ~ m, DF, scales = list(log = TRUE),
>>>          panel = panel.bwplot, horizontal = FALSE,
>>>          box.width = .0001)
>>>
>>> Best,
>>> -Deepayan
>>>
>>> On Sat, Dec 10, 2022 at 7:46 PM Laurent Rhelp <LaurentRHelp using free.fr> wrote:
>>>> Dear R-Help list,
>>>>
>>>>       I would like to use bwplot from the lattice package with a log scale
>>>> both on
>>>> the x-axis and the y-axis but I do not know how to do that because I do
>>>> not know
>>>> how to change the factor x-axis in a numeric x-axis.
>>>>
>>>>     Here is my example:
>>>>
>>>>
>>>> library(lattice)
>>>>
>>>> # the mock data
>>>> y <- runif(100000,min=0, max=500)
>>>> x <- seq(0,500,length=length(y))
>>>> # I cut the x variable to create a factor variable in order to calculate
>>>> the boxes
>>>> groups <- cut(x,10,ordered_result = TRUE)
>>>> # creating the dataframe for the lattice functions
>>>> DF <- data.frame( x= x , y = y, groups = groups)
>>>>
>>>>
>>>> ## ok for xyplot
>>>> xyplot(   y ~ x
>>>>              , data=DF
>>>>              , scales = list(
>>>>                y = list(log=T)
>>>>                , x = list(log=T)
>>>>
>>>>              )
>>>> )
>>>>
>>>> ## ok for bwplot with the log scale for the y-axis
>>>> bwplot( y ~ groups
>>>>              , data=DF
>>>>              , scales = list(
>>>>                y = list(log=T)
>>>>                #     , x = list(log=T)
>>>>
>>>>              )
>>>> )
>>>>
>>>>
>>>>
>>>> ## Non ok for bwplot with the log scale for the x-axis
>>>> bwplot( y ~ groups
>>>>            , data=DF
>>>>            , scales = list(
>>>>                  y = list(log=T)
>>>>                , x = list(log=T)
>>>>
>>>>            )
>>>> )
>>>> which gives an error because the x-axis is a factor, I would like to
>>>> replace it
>>>> for the display by the meddle of every class for example and put a log
>>>> scale on the x-axis.
>>>>
>>>> Thank you for your help
>>>> Best regards
>>>> Laurent
>>>>
>>>>
>>>>
>>>> --
>>>> Cet e-mail a été vérifié par le logiciel antivirus d'Avast.
>>>> www.avast.com
>>>>
>>>> ______________________________________________
>>>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>> 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.
>>
>>
>> --
>> Cet e-mail a été vérifié par le logiciel antivirus d'Avast.
>> www.avast.com



More information about the R-help mailing list