[R] combination xyplot and barchart?
Deepayan Sarkar
deepayan.sarkar at gmail.com
Wed Nov 16 04:09:32 CET 2005
On 11/15/05, Erin Berryman <berr0179 at umn.edu> wrote:
> Dear R community,
>
> I am having trouble determining how to create the graph I want
> utilizing my relatively limited knowledge of R. So far I have been
> using the lattice library to create most of what I need.
> The dataset (enviro) consists of 2 variables (Temp and Precip) for each
> Day of a 2-yr period (Year). I wish to display Temp and Precip along
> the y axis plotted by Day on the x axis to allow comparison (one year's
> data in each of 2 panels stacked on top of each other) between the
> years. Essentially what I want it to look like is an xyplot (Temp ~ Day
> | Year, type='l') superimposed onto a barchart(Precip ~ Day | Year,
> horizontal=F), with scales adjusted so one can see detail in both
> variables.
> The closest I have come to what I need is by the following code :
>
> library(lattice)
> barchart(Precip + Temp ~ Day | Year, data=enviro, layout=c(1,2),
> horizontal=F, origin=0,
> panel=function(x,y,subscripts,...){panel.xyplot(x=enviro$Day,
> y=enviro$Temp, type='l',subscripts=subscripts, ...);
> panel.barchart(x=enviro$Day, y=enviro$Precip, subscripts=subscripts,
> ...)})
>
> Two panels are produced; however, both years' data are plotted in each
> panel (panels look identical). And I get this error:
>
> Error in grid.Call.graphics("L_rect", x$x, x$y, x$width, x$height,
> resolveHJust(x$just, :
> invalid line type
A reproducible example, even if it's a toy one, would have been helpful.
Your usage is confused. In particular, panel.xyplot ignores the
subscripts argument, so you end up giving exactly the same set of
values to panel.xyplot for both panels (so it's not surprising that
your panels show the same data). It seems that you are looking for
something like the following:
enviro <-
data.frame(Year = rep(2001:2002, each = 365),
Day = rep(1:365, 2),
Precip = pmax(0, rnorm(365 * 2)),
Temp = 2 + 0.2 * rnorm(365 * 2))
xyplot(Precip + Temp ~ Day | Year, data=enviro,
layout = c(1, 2),
panel = panel.superpose.2,
type = c('h', 'l'))
In case it helps, this is shorthand for
xyplot(Precip + Temp ~ Day | Year, data=enviro,
layout = c(1, 2),
panel = function(x, y, groups, subscripts, ...) {
panel.superpose.2(x = x, y = y,
groups = groups,
subscripts = subscripts,
...)
},
type = c('h', 'l'))
Note that the panel function is defined in terms of arguments it gets,
and does not explicitly refer to any external variables (like the data
frame 'enviro'). If you find yourself writing code that does, it's a
likely sign that you are doing something wrong (or at least
unnecessarily convoluted).
> From the documentation or the help archives, I cannot understand how to:
> 1) indicate a conditioning variable (Year) for panel.barchart and
> panel.xyplot
> 2) have 2 y axes with different scales in one panel
You can't easily. A panel has one set of scales, and that's it. You
can of course fake it by transforming the relevant part of your data
and adding a set of tick marks with appropriate (fake) labels (see
?panel.axis).
-Deepayan
More information about the R-help
mailing list