[R] specifying scales in lattice xyplot makes the lines disappear?

Peter Ehlers ehlers at ucalgary.ca
Tue May 10 23:27:10 CEST 2011


On 2011-05-10 13:10, Christopher W Ryan wrote:
> I have a dataframe concerning manner of death from death certificates,
> from 2005 to 2009 inclusive, with the following structure:
>
>> str(MannerYoung.plot.data)
> 'data.frame':   245 obs. of  4 variables:
>   $ year         : Factor w/ 5 levels "2005","2006",..: 1 2 3 4 5 1 2 3 4 5 ...
>   $ manner       : Factor w/ 7 levels "Accident","Homicide",..: 1 1 1 1
> 1 2 2 2 2 2 ...
>   $ new.age.group: Factor w/ 7 levels "1 to 4","5 to 9",..: 1 1 1 1 1 1
> 1 1 1 1 ...
>   $ Freq         : int  [omitted from this post]...
>>
>
> new.age.group categorizes age of death in 5-year age bands.
>
> The following makes a nice lattice scatterplot, with a panel for each
> age-band, year on the horizontal axis,and frequency on the veritcal
> axis.
>
> xyplot(Freq~year | new.age.group, groups=manner,
> data=MannerYoung.plot.data, type="l",  auto.key =
> list(text=levels(MannerYoung.plot.data$manner), lines=TRUE,
> points=FALSE, space = "top"),
> layout=c(length(levels(MannerYoung.plot.data$new.age.group)),1))
>
> Except that the year tick labels overlap each other. I thought I would
> try to label only every other tick, using scales(), like this:
>
> xyplot(Freq~year | new.age.group, groups=manner,
> data=MannerYoung.plot.data, type="l",
> scales=list(x=list(at=c(2005,2007,2009),
> labels=c("2005","2007","2009"))), auto.key =
> list(text=levels(MannerYoung.plot.data$manner), lines=TRUE,
> points=FALSE, space = "top"),
> layout=c(length(levels(MannerYoung.plot.data$new.age.group)),1))
>
> This draws the panels, vertical axis, horizontal axes, with the
> odd-numbered years, everything as desired, except that there are no
> lines in the panels--no actual content.
>
> I suppose I could decrease the font size for the year labels, using
> cex(), and that cured the overlap, but I wanted to understand
> scales().
>
> Where am I going wrong?

You're using 'year' as a factor, but thinking about it as a numeric.
Use at=c(1,3,5) in your scales list.

But I would have expected the lines to plot and the axis labels
not to be visible. Are you telling the whole story?
_Minimal reproducible_ code is always a good thing.

Peter Ehlers

>
> Thanks.
>
> --Chris Ryan
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list