Hello, I would be very grateful if anyone could help with what seems like a simple lattice task. I want to use xyplot, where the symbols for the plotted points are taken from another column in the data frame. So if the data frame looked like: a <- as.data.frame(matrix(data=c(1,1,10,2,2,20,3,3,30), nrow=3, ncol=3, byrow=TRUE)) a V1 V2 V3 1 1 1 10 2 2 2 20 3 3 3 30 you would get an xy scatter plot using where "10" (not a dot) is at coordinate 1,1, "20" is at 2,2, and so on. I have made two attempts. The first, below, almost works, but only takes the first character from the V3 column: library(lattice) xyplot(V1~V2, data=a, pch=as.character(a$V3)) I've also tried this example, which is given in an online user guide for trellis. It uses the built-in "ethanol" data set (which is also in lattice), and subscripts... but when I try the same code in lattice, I get an "invalid graphics state" error. library(lattice) xyplot(NOx ~ E | C, data = ethanol, aspect = 1/2, panel = function(x, y, subscripts) text(x, y, subscripts, cex = .75) ) Thank you very much in advance! Owen Solberg ------------> version_ platform i686-redhat-linux-gnu arch i686 os linux-gnu system i686, linux-gnu status major 2 minor 0.0 year 2004 month 10 day 04 language R
Ignacio Colonna
2005-Mar-15 23:21 UTC
[R] How to plot points as numbers/strings in lattice
Owen, I think this gives the plot you are looking for. There may be other better ways to do it, this is just the one I know. Inside 'panel' you would need to use 'ltext()' instead of 'text()', as in the example you provided. xyplot(V1~V2, data=a, groups=V3, panel = function(x, y, groups) ltext(x, y, label=groups, cex = .75) ) Ignacio -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Owen Solberg Sent: Tuesday, March 15, 2005 4:55 PM To: r-help at stat.math.ethz.ch Subject: [R] How to plot points as numbers/strings in lattice Hello, I would be very grateful if anyone could help with what seems like a simple lattice task. I want to use xyplot, where the symbols for the plotted points are taken from another column in the data frame. So if the data frame looked like: a <- as.data.frame(matrix(data=c(1,1,10,2,2,20,3,3,30), nrow=3, ncol=3, byrow=TRUE)) a V1 V2 V3 1 1 1 10 2 2 2 20 3 3 3 30 you would get an xy scatter plot using where "10" (not a dot) is at coordinate 1,1, "20" is at 2,2, and so on. I have made two attempts. The first, below, almost works, but only takes the first character from the V3 column: library(lattice) xyplot(V1~V2, data=a, pch=as.character(a$V3)) I've also tried this example, which is given in an online user guide for trellis. It uses the built-in "ethanol" data set (which is also in lattice), and subscripts... but when I try the same code in lattice, I get an "invalid graphics state" error. library(lattice) xyplot(NOx ~ E | C, data = ethanol, aspect = 1/2, panel = function(x, y, subscripts) text(x, y, subscripts, cex = .75) ) Thank you very much in advance! Owen Solberg ------------> version_ platform i686-redhat-linux-gnu arch i686 os linux-gnu system i686, linux-gnu status major 2 minor 0.0 year 2004 month 10 day 04 language R ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Deepayan Sarkar
2005-Mar-15 23:38 UTC
[R] How to plot points as numbers/strings in lattice
On Tuesday 15 March 2005 16:54, Owen Solberg wrote:> Hello, > > I would be very grateful if anyone could help with what seems like a > simple lattice task. I want to use xyplot, where the symbols for the > plotted points are taken from another column in the data frame. So > if the data frame looked like: > > a <- as.data.frame(matrix(data=c(1,1,10,2,2,20,3,3,30), nrow=3, > ncol=3, byrow=TRUE)) a > > V1 V2 V3 > 1 1 1 10 > 2 2 2 20 > 3 3 3 30 > > you would get an xy scatter plot using where "10" (not a dot) is at > coordinate 1,1, "20" is at 2,2, and so on. > > I have made two attempts. The first, below, almost works, but only > takes the first character from the V3 column: > > library(lattice) > xyplot(V1~V2, data=a, pch=as.character(a$V3)) > > I've also tried this example, which is given in an online user guide > for trellis. It uses the built-in "ethanol" data set (which is also > in lattice), and subscripts... but when I try the same code in > lattice, I get an "invalid graphics state" error. > > library(lattice) > xyplot(NOx ~ E | C, data = ethanol, aspect = 1/2, > panel = function(x, y, subscripts) > text(x, y, subscripts, cex = .75) > )text() will not do anything useful in a lattice panel function, you want to use ltext or panel.text. There's a very similar example in the help page for xyplot. Deepayan