Christopher W Ryan
2011-Mar-22 19:12 UTC
[R] adding vertical segments to an xyplot in lattice
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. Thanks. --Chris Ryan SUNY Upstate Medical University Clinical Campus at Binghamton
Well, a custom panel function is what you need (or one that may already exist somewhere: try googling on "high low intervals in R graphs" or some such). So if you haven;t already done so, try Paul Morrell's Chapter on lattice plots from his book for how panel functions work: http://www.stat.auckland.ac.nz/~paul/RGraphics/chapter4.pdf -- Bert On Tue, Mar 22, 2011 at 12:12 PM, Christopher W Ryan <cryan at binghamton.edu> 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. > > Thanks. > > --Chris Ryan > SUNY Upstate Medical University > Clinical Campus at Binghamton > > ______________________________________________ > 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. >-- Bert Gunter Genentech Nonclinical Biostatistics
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