Re: [R] Adding text labels to lattice plots with multiple panels

From: Dennis Murphy <djmuser_at_gmail.com>
Date: Fri, 08 Apr 2011 10:32:38 -0700

Hi:

After a number of false starts, I finally consulted Deepayan's book and the example on p. 73, suitably adapted, yielded a solution. Add a variable for the labels and then...

df$lab <- rep(1:6, each = 3)
bwplot(dv ~ f1 | f2, data = df, ylim = c(0.5, 1),  panel = function(x, y, ..., subscripts) {    lab <- df$lab[subscripts]
   panel.bwplot(x, y, ...)
   panel.text(x, 0.55, labels = lab)
 }
)

If you look closely, you'll see that each label is overplotted three times. A similar plot in ggplot2 would be

library(ggplot2)
ggplot(df, aes(x = f1, y = dv)) + geom_boxplot() +

    geom_text(aes(x = as.numeric(f1), lab = lab), y = 0.55, alpha = 0.5) +     facet_wrap( ~ f2) + ylim(0.5, 1)

The alpha argument in geom_text() is designed to mitigate the overplotting effect somewhat.

HTH,
Dennis

On Fri, Apr 8, 2011 at 5:32 AM, Jeff Stevens <stev0175_at_googlemail.com>wrote:

> Hi,
>
> I am trying to add text to the bottom of a lattice bwplot with
> multiple panels. I would like to add a label below each boxplot, but
> the labels do not come from the data. I've tried the following, code:
>
> f1 <- c(rep(c(rep("a", 3), rep("b", 3), rep("c", 3)), 2))
> f2 <- c(rep("A", 9), rep("B", 9))
> dv <- c(0.9, 0.8, 0.85, 0.6, 0.65, 0.7, 0.8, 0.85, 0.8, 0.95, 0.85,
> 0.9, 0.65, 0.7, 0.75, 0.85, 0.9, 0.85)
> df <- data.frame(f1, f2, dv)
> lab <- c(1, 2, 3, 4, 5, 6)
>
> bwplot(dv ~ f1 | f2, data = df, ylim = c(0.5, 1),
> panel = function(x, y, ...) {
> panel.bwplot(x, y, ...)
> panel.text(x, 0.55, labels = lab)
> }
> )
>
> I have two problems. First, the label values are writing over one
> another. I wrote a horrible hack (below) that fixes that problem, but
> I would like to know if there are better/more flexible ways to do
> this.
>
> bwplot(dv ~ f1 | f2, data = df, ylim = c(0.5, 1),
> panel = function(x, y, ...) {
> x2 <- as.factor(c("a", "b", "c"))
> panel.bwplot(x, y, ...)
> panel.text(x2, 0.55, labels = lab)
> }
> )
>
> Second, when using the horrible hack the first values are repeated in
> the second panel. How do I display the correct values in the second
> panel? The subscripts argument seems to do the trick when the labels
> come from the data, but how do I get the proper values when the labels
> come from outside of the data?
>
>
> Many thanks,
> Jeff
>
> ______________________________________________
> 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.
>

        [[alternative HTML version deleted]]



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. Received on Fri 08 Apr 2011 - 17:34:55 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 09 Apr 2011 - 20:00:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive