[R] bwplot() {lattice}

Deepayan Sarkar deepayan.sarkar at gmail.com
Sun Feb 28 20:08:22 CET 2010


On Sat, Feb 27, 2010 at 8:14 PM, Peng Cai <pengcaimaillist at gmail.com> wrote:
> Thanks a lot Deepayan, one question:
>
> Is it possible to place these barplots side-by-side instaed of super
> imposing? Something like this:
> http://www.imachordata.com/wp-content/uploads/2009/09/boxplot.png

Well, yes; a quick approximation is

data(mpg, package = "ggplot2")

bwplot(hwy ~ class, data = mpg, groups = year,
       pch = "|", box.width = 1/3,
       auto.key = list(points = FALSE, rectangles = TRUE, space = "right"),
       panel = panel.superpose,
       panel.groups = function(x, y, ..., group.number) {
           panel.bwplot(x + (group.number-1.5)/3, y, ...)
       })

A general solution would need more work. But I would instead have suggested

bwplot(hwy ~ factor(year) | class, data = mpg, layout = c(7, 1), pch = "|")

-Deepayan

>
> library(lattice)
> bwplot(yield ~ variety, data = barley, col = 1, pch = 16,
>       panel = panel.superpose, panel.groups = panel.bwplot,
>       auto.key=list(space="right"),
>       groups = year, scales=(x=list(rot=45)))
>
> Thanks,
> Peng
>
> On Fri, Feb 26, 2010 at 3:51 AM, Deepayan Sarkar <deepayan.sarkar at gmail.com>
> wrote:
>>
>> On Fri, Feb 26, 2010 at 8:30 AM, Peng Cai <pengcaimaillist at gmail.com>
>> wrote:
>> > Hi All,
>> >
>> > I'm trying to plot boxplot graph. I tried barchart with "groups=" option
>> > and
>> > it worked fine. But when I try to generate same kind of graph using
>> > bwplot(), "groups=" option doesn't seem to work. Though this works,
>> >
>> > yield ~ variety | site * year
>> >
>> > I'm thinking why "groups=" doesn't work in this case, can anyone help
>> > please...
>>
>> Let's see...you have exactly one observation per site/variety/year
>> combination (otherwise the barchart wouldn't have made sense). So in
>> the boxplot you want (which is supposed to summarize a distribution,
>> not a single point), you only have that single point to plot. For
>> that, you can use
>>
>> dotplot(yield ~ variety | site, data = barley, auto.key = TRUE,
>>        groups = year, layout = c(6,1), scales=(x=list(rot=45)))
>>
>> If you try to come up with a more sensible example, you would realize
>> that boxplots are already grouped (the grouping variable is the
>> categorical variable in the formula y ~ x, not the 'groups' argument).
>> Compare
>>
>> ## Is this really what you want?
>> bwplot(yield ~ variety, data = barley, col = 1, pch = 16,
>>       panel = panel.superpose, panel.groups = panel.bwplot,
>>       groups = year, scales=(x=list(rot=45)))
>>
>> bwplot(yield ~ year | variety, data = barley,
>>       scales=(x=list(rot=45)), layout = c(10, 1))
>>
>> -Deepayan
>>
>>
>> >
>> > #Code:
>> > library(lattice)
>> > barchart(yield ~ variety | site, data = barley,
>> >         groups = year, layout = c(1,6),
>> >  auto.key = list(points = FALSE, rectangles = TRUE, space = "right"))
>> >
>> > bwplot(yield ~ variety | site, data = barley,
>> >         groups = year, layout = c(6,1), scales=(x=list(rot=45)),
>> >  auto.key = list(points = FALSE, rectangles = TRUE, space = "right"))
>
>



More information about the R-help mailing list