[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 16:38:34 CET 2022
Indeed, I have to clarify my problem.
I have to display my physical measurements in a log-log representation.
I can do that with xyplot from the lattice package. But I would like to
show the dispersion of my measurement. So I wanted to use the bwplot
function but it is not possible because the x-axis has to be a factor
(as you say). In fact I would like to be able to draw boxplots on a
graph according to a numerical location on a x-axis. I understood that I
cannot use the lattice functions.
Le 11/12/2022 à 16:12, Bert Gunter a écrit :
> There can be **no log scale** for the x axis, only labels, if x is a
> factor (Groups).
>
> If what you mean is that there are no tick marks o the x-axis, they
> can be added in my code in the scales list (for my reprex with 5
> levels of group):
>
> scales = list(
> alternating = 1 ## ticls only on bottom
> , y = list(log=T, tck = c(1,0))
> , x = list(at = 1:5, tck = 1)
> )
>
> If this is not what you meant, you'll need to clarify ... or just move on.
>
> -- Bert
>
> On Sun, Dec 11, 2022 at 1:06 AM Laurent Rhelp <LaurentRHelp using free.fr>
> wrote:
>
> Ok for the labels but the x-axis is not displayed in log scale ?
>
>
>
> Le 10/12/2022 à 22:36, Bert Gunter a écrit :
>> ... and here's a version where the x variable is different than
>> y. It's basically the same.
>>
>>
>> set.seed(123)
>> y <- runif(40,min=0, max= 10)
>> x <- seq(0,10, length = 40)
>> ## 5 equally spaced groups labeled by log10 of the center
>> ## of the intervals
>> xrng <- range(x)
>> eps <- .0001*c(-1,1)*diff(xrng) ## see ?cut
>> xrng <-xrng + eps
>> delta <- diff(xrng)/5 # 5 = number of intervals
>> brks <- seq(xrng[1], xrng[2],length.out =6)
>> labs <- round(log10(brks[-1] - delta/2), 2) ## log10 (centers)
>> groups <- cut(x,breaks = brks, ordered_result = TRUE,
>> labels = paste0('10^',labs))
>> # creating the dataframe for the lattice functions
>> DF <- data.frame( y = y, groups = groups)
>>
>> bwplot( y ~ groups
>> , data=DF
>> , scales = list(
>> y = list(log=T)
>> ),
>> )
>>
>> On Sat, Dec 10, 2022 at 10:20 AM Bert Gunter
>> <bgunter.4567 using gmail.com> wrote:
>>
>> If Deepayan's suggestion does not suit and especially *if* I
>> understand what you want to do correctly, then it seems to me
>> that it is straightforward to create the groups and group
>> labels manually:
>>
>> ## in verbose detail to hopefully improve clarity
>> set.seed(123) ## for reprex
>> y <- runif(40,min=0, max= 10) ##*small* reprex
>> ## cut y into 5 equally spaced groups labeled by log10 of the
>> center
>> ## of the intervals
>> yrng <- range(y)
>> eps <- .0001*c(-1,1)*diff(yrng) ## see ?cut
>> yrng <-yrng + eps
>> delta <- diff(yrng)/5 # 5 = number of intervals
>> brks <- seq(yrng[1], yrng[2],length.out =6)
>> labs <- round(log10(brks[-1] - delta/2), 2) ## log10 (centers)
>> groups <- cut(y,breaks = brks,
>> labels = paste0('10^',labs))
>> # creating the dataframe for the lattice functions
>> DF <- data.frame( y = y, groups = groups)
>>
>> bwplot( y ~ groups
>> , data=DF
>> , scales = list(
>> y = list(log=T)
>> ),
>> )
>>
>> ## Of course, ignore if I have misunderstood.
>>
>> Cheers,
>> Bert
>>
>> On Sat, Dec 10, 2022 at 8:03 AM Deepayan Sarkar
>> <deepayan.sarkar using gmail.com> wrote:
>>
>> 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 <http://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
>> <http://www.R-project.org/posting-guide.html>
>> > and provide commented, minimal, self-contained,
>> reproducible code.
>>
>> ______________________________________________
>> 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
>> <http://www.R-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained,
>> reproducible code.
>>
>
>
> <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient>
> Sans virus.www.avast.com
> <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient>
>
>
> <#m_7461191544387978914_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list