[R] giving factor names
Deepayan Sarkar
deepayan.sarkar at gmail.com
Tue Jun 21 07:34:09 CEST 2011
On Fri, Jun 10, 2011 at 10:27 PM, Ethan Brown <ethancbrown at gmail.com> wrote:
> Hi Kieran,
>
> I'm not very familiar with lattice, but here's a workaround that works for
> me. Basically, I just created a new data.frame column that was a factor
> (combo$zf), and forced its levels to be what you're looking for here.
This works, but the less brute force approach is:
combo$Z <- factor(combo$z)
levelplot(resp ~ x * y | Z, data=combo,
strip = strip.custom(strip.names = c(TRUE, TRUE), sep = " = "),
pretty=TRUE,region=TRUE,contour=FALSE)
See ?strip.default.
-Deepayan
>
> require(lattice)
>
> x<-c(1,2,3)
> y<-c(2,4,6)
> z<-c(0.1,0.5,2)
> combo<-expand.grid(x,y,z)
> combo<-data.frame(combo)
> names(combo)<-c("x","y","z")
> outcome<-function(l)
> {
> (l[1]*l[2])/l[3]
> }
> resp<-apply(combo,1,outcome)
>
> ## Create new column and assign levels
> combo$zf <- as.factor(combo$z)
> levels(combo$zf) <- paste("z=", levels(combo$zf), sep="")
>
> ## Now I use the new variable as the conditioning variable in the plot
> levelplot(resp~x*y|zf, data=combo
> ,pretty=TRUE,region=TRUE,contour=FALSE)
>
> In the future, it would help if you could specify the packages you're using,
> since I had to do a little research to find where the "levelplot" function
> is from.
>
> Hope this helps,
> Ethan
>
>
>
> On Tue, Jun 7, 2011 at 9:30 AM, kieran martin <kieranjmartin at gmail.com>wrote:
>
>> Hi,
>>
>> I've been driving myself insane with this problem. I have a trellis plot of
>> contours, and I want each level to have something like "z=value" for each
>> one. I can get each one to say z, or each one to say the value (by using
>> as.factor) but not both. Heres an artificial example to show what I mean
>> (as
>> my actual data set is much larger!)
>>
>> x<-c(1,2,3)
>> y<-c(2,4,6)
>> z<-c(0.1,0.5,2)
>> combo<-expand.grid(x,y,z)
>> combo<-data.frame(combo)
>> names(combo)<-c("x","y","z")
>> outcome<-function(l)
>> {
>> (l[1]*l[2])/l[3]
>> }
>> resp<-apply(combo,1,outcome)
>> levelplot(resp~x*y|z,data=combo
>> ,pretty=TRUE,region=TRUE,contour=FALSE)
>>
>> , so in this final graph I want the z=0.1, z=0.5 and z=2 in turn.
>>
>> Thanks,
>>
>> Kieran Martin
>> University of Southampton
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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.
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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