[R] Lattice, combine histogram and line graph

Gabor Grothendieck ggrothendieck at gmail.com
Mon Jan 10 03:42:50 CET 2011

On Sun, Jan 9, 2011 at 8:13 PM, Jim Burke <j.burke at earthlink.net> wrote:
> Hello everyone,
> I have a simple histogram of gasoline prices going back a few years that I
> want to insert a line graph of consumer price index (cpi) over the
> histogram.  I have looked through the "Lattice" book by Deepayan Sarkar but
> don't see anything there. How might this be done? An example would be
> wonderful.
> Current code snippet follows. For example additional field to add as a line
> graph would be a cpi calculation like "gas_data$regular * (2010_cpi /
> gas_data$year )".
> xyplot( regular ~ as.Date(gas_data$dates,"%b %d, %Y") , data = gas_data,
>       type = c("g", "h" ))

xyplot.zoo in the zoo package has facilities for drawing multiple time
series using lattice graphics or in different panels.  The
documentation has many examples.


z1 <- zoo(1:6)
z2 <- z1^2
z <- cbind(z1, z2)
xyplot(z) # different panels
xyplot(z, screen = 1) # all on one panel

vignette(package = "zoo") # lists available vignettes

Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

More information about the R-help mailing list