I'm trying to plot a scatterplot of two variables using pch to plot different characters based on a third factor. Here is my example> data("ToothGrowth") > attach(ToothGrowth) > levels(supp)[1] "OJ" "VC"> plot(len ~ dose,pch=as.numeric(supp)) > legend(locator(1),pch=as.numeric(supp),legend=levels(supp))The command as.numeric(supp) returns 2 2 2 2 ... 1 1 1 ... for the factor supp which the plot command uses nicely, but the pch command for the legend uses the first two values 2 2 for the command. This isn't correct of course. How can I get the correct numbers for the pch command for the levels? I tried pch=as.numeric(levels(supp)) in legend but that didn't work. Thanks for any help. John -- .................................................................... . John Verzani mailto:verzani at math.csi.cuny.edu . . Dept. of Mathematics http://www.math.csi.cuny.edu/~verzani . . City University of New York tel: (718) 982-3623 . . College of Staten Island fax: (718) 982-3631 . . Staten Island, NY 10314 . .................................................................... -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hi> I'm trying to plot a scatterplot of two variables using pch to plot > different characters based on a third factor. Here is my example > > > data("ToothGrowth") > > attach(ToothGrowth) > > levels(supp) > [1] "OJ" "VC" > > plot(len ~ dose,pch=as.numeric(supp)) > > legend(locator(1),pch=as.numeric(supp),legend=levels(supp)) > > The command as.numeric(supp) returns 2 2 2 2 ... 1 1 1 ... for the > factor supp which the plot command uses nicely, but the pch command > for the legend uses the first two values 2 2 for the command. This > isn't correct of course. How can I get the correct numbers for the pch > command for the levels? I tried pch=as.numeric(levels(supp)) in legend > but that didn't work.The simple answer is ... legend(locator(1),pch=1:2,legend=levels(supp)) ... because the numeric equivalent of the levels of a factor are the integers 1:nlevels(<factor>) This might be viewed as a not very nice programming style because it makes use of knowledge of the underlying implementation of factors. A more complicated and harder to understand, but possibly more pure solution might be ... legend(locator(1),pch=as.numeric(supp[match(levels(supp), supp)]),legend=levels(supp)) Paul -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
John Verzani <verzani at math.csi.cuny.edu> writes:> I'm trying to plot a scatterplot of two variables using pch to plot > different characters based on a third factor. Here is my example > > > data("ToothGrowth") > > attach(ToothGrowth) > > levels(supp) > [1] "OJ" "VC" > > plot(len ~ dose,pch=as.numeric(supp)) > > legend(locator(1),pch=as.numeric(supp),legend=levels(supp)) > > The command as.numeric(supp) returns 2 2 2 2 ... 1 1 1 ... for the > factor supp which the plot command uses nicely, but the pch command > for the legend uses the first two values 2 2 for the command. This > isn't correct of course. How can I get the correct numbers for the pch > command for the levels? I tried pch=as.numeric(levels(supp)) in legend > but that didn't work.The legend() function doesn't care or know about the points you plotted, so it expects pch to be a vector as long as the list of legends. pch=seq(along=levels(supp)) looks about right. Or just pch=1:2 Alternately, think ahead: sym <- c(1,2) # this could be changed to get a different set of symbols plot(len ~ dose, pch=sym[supp]) legend(locator(1), pch=sym, legend=levels(supp)) -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._