[R] pch with plot and legend

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Thu Jul 26 00:48:46 CEST 2001


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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list