[R] adding vertical segments to an xyplot in lattice
Peter Ehlers
ehlers at ucalgary.ca
Wed Mar 23 13:24:44 CET 2011
On 2011-03-22 12:12, Christopher W Ryan wrote:
> I have a dataframe that looks like this:
>
> > str(chr)
> 'data.frame': 84 obs. of 7 variables:
> $ county: Factor w/ 3 levels "Broome","Nassau",..: 3 3 3 3 3 3 3 3 3 3 ...
> $ item : Factor w/ 28 levels "Access to healthy foods",..: 21 19 20
> 18 16 3 2 6 17 8 ...
> $ value : num 8644 15 3.5 3.9 7.7 ...
> $ low : num 7897 9 2.5 2.6 7 ...
> $ high : num 9390 22 4.5 5.2 8.4 37 30 23 24 101 ...
> $ target: num 5034 11 2.7 2.6 6.1 ...
> $ nys : num 6099 16 3.5 3.3 8 ...
>
>> head(chr)
> county item value low high target nys
> 1 Sullivan Premature death 8644.0 7897.0 9390.0 5034.0 6099.0
> 2 Sullivan Poor or fair health 15.0 9.0 22.0 11.0 16.0
> 3 Sullivan Poor physical health days 3.5 2.5 4.5 2.7 3.5
> 4 Sullivan Poor mental health days 3.9 2.6 5.2 2.6 3.3
> 5 Sullivan Low birthweight 7.7 7.0 8.4 6.1 8.0
> 6 Sullivan Adult smoking 29.0 22.0 37.0 15.0 20.0
>
> I'd like to graph high and low for "Premature death" for each of the
> three counties, with 3 vertical line segments, one connecting those
> two points for each county. I can get the two points for each county:
>
>> xyplot(low+high ~ county, data=subset(chr, item=="Premature death"))
>
> but I have not yet been able to figure out how to draw the 3 vertical
> line segments. Been struggling to understand panel functions, but no
> success so far. I'd be grateful for any advice.
For lattice, I usually prefer the long version of a dataset.
Try this:
dd <- data.frame(county = letters[1:3],
lo = c(5,2,3),
hi = c(9,5,10))
## convert to 'long' format (you can use the reshape() function in stats
or the reshape package:
require(reshape)
dd.long <- melt(dd, id = "county")
dd.long
require(lattice)
xyplot(value ~ county, data = dd.long, groups = county,
pch = 19, type = 'b', cex = 2, lwd = 5, col = 2:4)
Peter Ehlers
>
> Thanks.
>
> --Chris Ryan
> SUNY Upstate Medical University
> Clinical Campus at Binghamton
More information about the R-help
mailing list