[R] Problem with data frame when using xYplot?

Sander Oom slist at oomvanlieshout.net
Fri May 13 11:27:24 CEST 2005


Problem solved!

I was so focused on reproducing the plotmeans() functionality with 
xYplot() that I completely overlooked the fact that my data does not 
allow a x-y plot, as only Sodium is a numeric variable while Position 
and AltGeo are factors!

Using unclass() to make Position a numeric variable does the trick:
tmp$Position <- unclass(tmp$Position)

The code below does the trick. Now I only need to figure out how to 
tweak the x axis to pretend I am plotting a factor, i.e. plotting labels 
"Inside" and "Outside".

Cheers,

Sander.


library(Hmisc)
library(Lattice)
tmp <-
structure(list(Position = structure(as.integer(c(1, 2, 1, 2,
1, 2, 1, 2)), .Label = c("Inside", "Outside"), class = "factor"),
     AltGeo = structure(as.integer(c(1, 1, 2, 2, 3, 3, 4, 4)), .Label = 
c("Basalt-High",
     "Basalt-Low", "Quartz-High", "Quartz-Low"), class = "factor"),
     Sodium = c(27.3333333333333, 26.8888888888889, 25, 18.1111111111111,
     4.66666666666667, 5.55555555555556, 10.6666666666667, 5.66666666666667
     ), SD = c(5.3851648071345, 2.42097317438899, 20.1618451536560,
     15.2679766541317, 5.45435605731786, 8.09492296305393, 10.6183802907976,
     8.06225774829855), Nobs = c(9, 9, 9, 9, 9, 9, 9, 9), Lower = 
c(25.5382783976218,
     26.0818978307592, 18.2793849487813, 13.0217855597339, 2.84854798089405,
     2.85724790120425, 7.12720656973412, 2.97924741723382), Upper = 
c(29.1283882690448,
     27.6958799470186, 31.7206150512187, 23.2004366624884, 6.48478535243929,
     8.25386320990686, 14.2061267635992, 8.35408591609952)), .Names = 
c("Position",
"AltGeo", "Sodium", "SD", "Nobs", "Lower", "Upper"), row.names = c("1",
"2", "3", "4", "5", "6", "7", "8"), class = "data.frame")
tmp$Position <- unclass(tmp$Position)
xYplot(Cbind(Sodium,Lower,Upper) ~ Position|AltGeo, groups=AltGeo,
   data=tmp, ylim=c(min(tmp$Lower)-1,max(tmp$Upper)+1),
   xlab="Position", ylab="Sodium"
   )



Sander Oom wrote:
> Dear all,
> 
> I am trying to plot means and error bars using xYplot, but I get an 
> error message from xYplot which I can not figure out:
>  > Error in Summary.factor(..., na.rm = na.rm) :
>         range not meaningful for factors
> 
> The data frame (tmpNa) was created using aggregate. I have used dump to 
> created the code below, which generates the same error.
> 
> Can anybody tell me what is wrong with the data frame?
> 
> Thanks in advance,
> 
> Sander.
> 
> library(Hmisc)
> tmpNa <-
> structure(list(Position = structure(as.integer(c(1, 2, 1, 2,
> 1, 2, 1, 2)), .Label = c("Inside", "Outside"), class = "factor"),
>     AltGeo = structure(as.integer(c(1, 1, 2, 2, 3, 3, 4, 4)), .Label = 
> c("Basalt-High",
>     "Basalt-Low", "Quartz-High", "Quartz-Low"), class = "factor"),
>     Sodium = c(27.3333333333333, 26.8888888888889, 25, 18.1111111111111,
>     4.66666666666667, 5.55555555555556, 10.6666666666667, 5.66666666666667
>     ), SD = c(5.3851648071345, 2.42097317438899, 20.1618451536560,
>     15.2679766541317, 5.45435605731786, 8.09492296305393, 10.6183802907976,
>     8.06225774829855), Nobs = c(9, 9, 9, 9, 9, 9, 9, 9), Lower = 
> c(25.5382783976218,
>     26.0818978307592, 18.2793849487813, 13.0217855597339, 2.84854798089405,
>     2.85724790120425, 7.12720656973412, 2.97924741723382), Upper = 
> c(29.1283882690448,
>     27.6958799470186, 31.7206150512187, 23.2004366624884, 6.48478535243929,
>     8.25386320990686, 14.2061267635992, 8.35408591609952)), .Names = 
> c("Position",
> "AltGeo", "Sodium", "SD", "Nobs", "Lower", "Upper"), row.names = c("1",
> "2", "3", "4", "5", "6", "7", "8"), class = "data.frame")
> xYplot(Cbind(Sodium,Lower,Upper) ~ AltGeo, groups=Position,  data=tmpNa)
> 
> 
>  > version
>          _
> platform i686-pc-linux-gnu
> arch     i686
> os       linux-gnu
> system   i686, linux-gnu
> status
> major    2
> minor    1.0
> year     2005
> month    04
> day      18
> language R
> 

-- 
--------------------------------------------
Dr. Sander P. Oom
Animal, Plant and Environmental Sciences,
University of the Witwatersrand
Private Bag 3, Wits 2050, South Africa
Tel (work)      +27 (0)11 717 64 04
Tel (home)      +27 (0)18 297 44 51
Fax             +27 (0)18 299 24 64
Email   sander at oomvanlieshout.net
Web     www.oomvanlieshout.net/sander




More information about the R-help mailing list