Skip to content
Prev 246882 / 398506 Next

Lattice, combine histogram and line graph

Hi Jim,

Some example data would help us.  I typically think of a histogram as
the frequency of values falling within a certain range (determined by
bins).  Since they are univariate plots, I'm not sure how you are
planning on adding a line graph to that.  If you just want bars of the
average gasoline price at different years, perhaps something along
these lines would work for you:

## Load required packages
require(lattice)
require(latticeExtra)

## Sample Data
dat <- data.frame(year = 1996:2010,
  x1 = rnorm(15, 3, .2), x2 = rnorm(15, 200, 1))

## Base xyplot (not a histogram) adding a layer with different y axis
xyplot(x1 ~ year, data = dat, type = "h") +
  as.layer(xyplot(x2 ~ year, data = dat, type = "l", col = "black"),
  y.same = FALSE)

## See
?xyplot
?as.layer
?hist # for info about histograms

HTH,

Josh
On Sun, Jan 9, 2011 at 5:13 PM, Jim Burke <j.burke at earthlink.net> wrote: