3

Define:

df <- data.frame(
        line1 = rep(seq(1,5,by=1),2), 
        line2 = rep(seq(2,6,by=1),2),
        index = rep(seq(1,5,by=1),2),
        factor=rep(c("a","b"),each=5))

where line1 and line2 are two variables, say height and weight.

This is the simple style I would want for each panel (I would also like to change size of point markers, but not use how):

plot(df$line1[df$factor=="a"], type = "o", ylim=c(0,6))
lines(df$line2[df$factor=="a"], type = "o", lty=2, pch=0)

When I try this:

library(lattice)
xyplot(c(line1,line2)~index|factor,data=df,type="o")

the program treats all points as if they belong on one line.

In addition I am not sure how to feed in the arguments for plot style to get the desired results.

PS1. Surprisingly I have Googled around and found lots of scatter plots (with linear fits, densities, etc.) and histogram examples of trellis graphs, but not one of the simple thing I am trying to do.

PS2. I would like to tag this question trellis but do not have reputation points to create tag. Any volunteers?

Joris Meys
  • 106,551
  • 31
  • 221
  • 263
Fred
  • 1,833
  • 3
  • 24
  • 29

2 Answers2

7

The answer is actually very simple. Instead of combining line1 and line2 to a vector, just use the extended formula interface of xyplot :

xyplot(line1+line2~index|factor,data=df,type="o")

gives :

enter image description here

Check also ?xyplot read through the heaps of text and play a bit with the examples there. You'll find out how to customize this with titles, legends and the likes.

As you labeled ggplot2 as well, this is one possibility of getting it done with that package :

require(ggplot2)
require(reshape)
df2 <- melt(df,id=c("index","factor"))

qplot(index,value,variable,facets = .~factor ,
          data=df2,geom="line",colour=variable)

Gives :

enter image description here

Joris Meys
  • 106,551
  • 31
  • 221
  • 263
  • Thanks! I did skim read through the "heaps of text" in `?xyplot`. Unfortunately the simple things were lost along the way... – Fred Jul 28 '11 at 12:45
  • Thanks for `ggplot` example. When I looked at the Hadley's web page for package I got the impression I would have to reshape the data. I found that surprising: That is not how data is typically stored. Also, colors look nice, but for working papers we are typically limited to B&W. Would be nice to be able to set B&W constraints so the color palette is only shades of grey. – Fred Jul 28 '11 at 13:04
  • @Fred : use the `col=c("black","grey")` argument in xyplot or use `+ scale_colour_manual(values=c("black","grey"))` after qplot. See - again - the help files. For xyplot, you might want to look at `?panel.xyplot` as well. – Joris Meys Jul 28 '11 at 13:16
  • 1
    as for reshaping the data: yes, you do have to reshape the data, but Hadley provides tools that make it easy. If you are going to use black and grey lines you may want to use `+theme_bw()` ... – Ben Bolker Jul 28 '11 at 13:31
2

Alternatively you can melt the data and use group:

library(reshape)
df2 <- melt(df,id.var=3:4)
xyplot(value~index|factor,data=df2,type="o",group=variable)
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453