Noah Hoffman
2013-Aug-23  23:43 UTC
[R] hide unused labels and resize panels in lattice barchart
Hello - I'm stumped on a lattice question. I'll start with my existing
code:
library(lattice)
library(latticeExtra)
# no https for read.csv...
tab <- read.csv(pipe('curl -s https://raw.github.com/gist/6323455'))
fig <- barchart(assignment ~ freq | label,
                 groups=method,
                 data=tab,
                 scales=list(y=list(relation='free')),
                 auto.key=TRUE,
                 strip = FALSE, strip.left = TRUE,
                 layout=c(1, length(levels(tab$label))),
                 drop.unused.levels=TRUE
                 )
resizePanels(fig)
pdf('plot.pdf', height=17)
plot(fig)
dev.off()
Here's the output: http://cl.ly/image/3B3s3L032I3p
My intention is to produce a plot showing the relative frequency of each
category in "assignment" conditioned by "label" (with paired
bars for each
value of "method") - but I can't figure out how to resize each
panel along
the y axis and show only categories that have corresponding x values in
each panel.  Looking at Fig 10.21 in the lattice book (
http://lmdvr.r-forge.r-project.org/figures/figures.html) I thought that
resizePanels() might do the trick, but no luck there.
Any help would be much appreciated.
Thanks a lot,
Noah
	[[alternative HTML version deleted]]
Duncan Mackay
2013-Aug-25  13:47 UTC
[R] hide unused labels and resize panels in lattice barchart
Hi Noah
All the categories have masked what is going on. I coverted some 
columns to factors and replotted
tab$Label <- sapply(tab$label, pmatch, sort(unique(as.character(tab$label))))
tab$Assign <-
as.numeric(sub("[^0-9]+","",as.character(tab$assignment)))
To get a look try
xyplot(freq ~ Assign|Label, tab, groups = method, type = "h", 
drop.unused =T, lwd = c(2,1), scales = list(x= list(relation = 
"free")), as.table = T)
I think for you to resize the panels you need to recode 
tab$assignment so that there are 1:max(tab$assignment) for each panel 
or label - ie there would be 7 categories ie 1:7  in panel 1 and 6 in 
2 ie 1:6 in panel 2.
As it stands now there are few overlapping codes.
  HTH
Duncan
Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
-----Original Message-----
From: r-help-bounces@r-project.org 
[mailto:r-help-bounces@r-project.org] On Behalf Of Noah Hoffman
Sent: Saturday, 24 August 2013 09:43
To: r-help@r-project.org
Subject: [R] hide unused labels and resize panels in lattice barchart
Hello - I'm stumped on a lattice question. I'll start with my existing
code:
library(lattice)
library(latticeExtra)
# no https for read.csv...
tab <- read.csv(pipe('curl -s 
<https://raw.github.com/gist/6323455')>https://raw.github.com/gist/6323455'))
fig <- barchart(assignment ~ freq | label,
                  groups=method,
                  data=tab,
                  scales=list(y=list(relation='free')),
                  auto.key=TRUE,
                  strip = FALSE, strip.left = TRUE,
                  layout=c(1, length(levels(tab$label))),
                  drop.unused.levels=TRUE
                  )
resizePanels(fig)
pdf('plot.pdf', height=17)
plot(fig)
dev.off()
Here's the output: 
<http://cl.ly/image/3B3s3L032I3p>http://cl.ly/image/3B3s3L032I3p
My intention is to produce a plot showing the relative frequency of 
each category in "assignment" conditioned by "label" (with
paired
bars for each value of "method") - but I can't figure out how to 
resize each panel along the y axis and show only categories that have 
corresponding x values in each panel.  Looking at Fig 10.21 in the 
lattice book (
<http://lmdvr.r-forge.r-project.org/figures/figures.html>http://lmdvr.r-forge.r-project.org/figures/figures.html)
I thought that
resizePanels() might do the trick, but no luck there.
Any help would be much appreciated.
Thanks a lot,
Noah
                [[alternative HTML version deleted]]
______________________________________________
<mailto:R-help@r-project.org>R-help@r-project.org mailing list
<https://stat.ethz.ch/mailman/listinfo/r-help>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.
	[[alternative HTML version deleted]]
Duncan Mackay
2013-Aug-26  14:38 UTC
[R] hide unused labels and resize panels in lattice barchart
Hi Noah
I have modified to give you an idea of panel widths. Also for factors 
you must have the same factors for all panels although not all will 
have the full complement.
I usually do this by hand as latticeExtra does not do exactly what i want.
library(lattice)
library(latticeExtra)
tab <-
structure(list(label = c("specimen1", "specimen1",
"specimen2",
"specimen2", "specimen2", "specimen2"), assignment
= c("species1",
"species2", "species2", "species3",
"species4", "species5"),
     freqA = c(0.4, 0.6, NA, 0.3, 0.3, 0.4), freqB = c(0.25, 0.3,
     0.2, 0.2, 0.3, 0.3), Label = c(1, 1, 2, 2, 2, 2), Assign = c(1,
     2, 2, 3, 4, 5)), .Names = c("label", "assignment",
"freqA",
"freqB", "Label", "Assign"), row.names = c(NA,
-6L), class = "data.frame")
   xyplot(freqA+freqB ~ Assign|factor(Label, label = c("specimen1", 
"specimen2")), tab,
          type = "h",
          lwd = c(2,5),
          outer = FALSE,
          par.settings = list(layout.widths = list(panel = c(1/4, 3/4))),
          scales = list(x = list(relation = "free",
                                 limits = list(c(1,2),c(2,5)),
                                 at = list(c(1,2),
                                           c(2:5)),
                                 labels = list(c("species
1","species 2"),
                                               paste("species", 2:5)),
                                 rot = 60)
          ))
   useOuterStrips(
   xyplot(freqA+freqB ~ Assign|factor(Label, label = c("specimen1", 
"specimen2")), tab,
          type = "h",
          as.table = T,
          par.settings = list(layout.heights = list(axis.panel = c(0,1) ),
                              layout.widths = list(panel = c(1/4, 3/4))),
          scales = list(x = list(relation = "free",
                                 limits = rep(list(c(1, 2), c(2, 5)),2),
                                 at = list(NULL,NULL, c(1:2),c(2:5)),
                                 labels = list(NULL,NULL,c("species 
1","species 2"), paste("species", 2:5)),
                                 rot=60
                        )),
          groups = assignment,
          lwd = 2
          )
  )
Hi Duncan,
Thanks for the reply - my apologies: my example data set was 
definitely overly complex, and the labels too abstract. I'd really 
like to provide labels for the assignments on the axis ("species" in 
the new example below), and I'm not sure how I could accomplish this 
using your approach.
Hopefully this simplified example illustrates the problem (and 
motivation for the plot) more clearly.  Note that "labels" now 
correspond to specimens and "assignments" to species to make things a 
bit less abstract.
tab <- read.csv(textConnection(
     'label,method,assignment,freq
specimen1,A,species1,0.4
specimen1,A,species2,0.6
specimen1,B,species1,0.25
specimen1,B,species2,0.3
specimen2,A,species3,0.3
specimen2,A,species4,0.3
specimen2,A,species5,0.4
specimen2,B,species2,0.2
specimen2,B,species3,0.2
specimen2,B,species4,0.3
specimen2,B,species5,0.3'))
fig <- barchart(assignment ~ freq | label,
                 groups=method,
                 data=tab,
                 scales=list(y=list(relation='free')),
                 auto.key=TRUE,
                 strip = FALSE, strip.left = TRUE,
                 layout=c(1, length(levels(tab$label))),
                 drop.unused.levels=TRUE
                 )
fig <- resizePanels(fig)
plot(fig)
Here, despite using resizePanels(), the panels for specimen1 and 
specimen2 occupy the same amount of vertical space, and the width of 
the bars are scaled proportionally to the number of species. I'd like 
to have specimen1 occupy half the vertical space, and for all bars in 
the plot to be the same width.
Thanks again,
Noah
On Sun, Aug 25, 2013 at 6:47 AM, Duncan Mackay 
<<mailto:mackay@northnet.com.au>mackay@northnet.com.au>wrote:
 >  Hi Noah
 >
 > All the categories have masked what is going on. I coverted some
 > columns to factors and replotted
 >
 > tab$Label <- sapply(tab$label, pmatch,
 > sort(unique(as.character(tab$label))))
 > tab$Assign <-
 >
as.numeric(sub("[^0-9]+","",as.character(tab$assignment)))
 >
 > To get a look try
 >
 > xyplot(freq ~ Assign|Label, tab, groups = method, type = "h",
 > drop.unused =T, lwd = c(2,1), scales = list(x= list(relation  >
"free")), as.table = T)
 >
 > I think for you to resize the panels you need to recode tab$assignment
 > so that there are 1:max(tab$assignment) for each panel or label - ie
 > there would be 7 categories ie 1:7  in panel 1 and 6 in 2 ie 1:6 in panel
2.
 > As it stands now there are few overlapping codes.
 >
 >  HTH
 >
 > Duncan
 >
 > Duncan Mackay
 > Department of Agronomy and Soil Science University of New England
 > Armidale NSW 2351
 >
 >
 > -----Original Message-----
 > From:
<mailto:r-help-bounces@r-project.org>r-help-bounces@r-project.org [
 > mailto:r-help-bounces@r-project.org<r-help-bounces@r-project.org>]
 > On Behalf Of Noah Hoffman
 > Sent: Saturday, 24 August 2013 09:43
 > To: <mailto:r-help@r-project.org>r-help@r-project.org
 > Subject: [R] hide unused labels and resize panels in lattice barchart
 >
 > Hello - I'm stumped on a lattice question. I'll start with my
existing
 > code:
 >
 > library(lattice)
 > library(latticeExtra)
 >
 > # no https for read.csv...
 > tab <- read.csv(pipe('curl -s 
<https://raw.github.com/gist/6323455')>https://raw.github.com/gist/6323455'))
 >
 > fig <- barchart(assignment ~ freq | label,
 >                  groups=method,
 >                  data=tab,
 >                  scales=list(y=list(relation='free')),
 >                  auto.key=TRUE,
 >                  strip = FALSE, strip.left = TRUE,
 >                  layout=c(1, length(levels(tab$label))),
 >                  drop.unused.levels=TRUE
 >                  )
 > resizePanels(fig)
 >
 > pdf('plot.pdf', height=17)
 > plot(fig)
 > dev.off()
 >
 > Here's the output: 
<http://cl.ly/image/3B3s3L032I3p>http://cl.ly/image/3B3s3L032I3p
 >
 > My intention is to produce a plot showing the relative frequency of
 > each category in "assignment" conditioned by "label"
(with paired bars
 > for each value of "method") - but I can't figure out how to
resize
 > each panel along the y axis and show only categories that have
 > corresponding x values in each panel.  Looking at Fig 10.21 in the
 > lattice book (
 > 
<http://lmdvr.r-forge.r-project.org/figures/figures.html>http://lmdvr.r-forge.r-project.org/figures/figures.html)
I thought
 > that
 > resizePanels() might do the trick, but no luck there.
 >
 > Any help would be much appreciated.
 >
 > Thanks a lot,
 > Noah
 >
 >                [[alternative HTML version deleted]]
 >
 > ______________________________________________
 > <mailto:R-help@r-project.org>R-help@r-project.org mailing list
 > 
<https://stat.ethz.ch/mailman/listinfo/r-help>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.
 >
                [[alternative HTML version deleted]]
______________________________________________
<mailto:R-help@r-project.org>R-help@r-project.org mailing list
<https://stat.ethz.ch/mailman/listinfo/r-help>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.
	[[alternative HTML version deleted]]