[R] locator()

Stephen R. Laniel laniel at cmu.edu
Mon Oct 25 08:55:49 CEST 1999


Hello,

I'm trying to write a command that places the legend for a graph wherever
I click the mouse.  So I tried

legend( locator(1), c("label 1", "label 2", "label 3"), lty=c(1,2,0),
pch=c("  C"))

I ran the command, and it paused until it received a mouseclick.  But when
I clicked well within the graphics window, I got this:

``Error: non-numeric coordinates"

Any idea why I'd get that?  I clicked on a valid part of the graphics
window.  I'm clearly missing something, but I'm not sure what.  I've
attached the full list of commands for this particular script, just in
case the secret lies therein.

Thanks for any help anyone can provide.

--Steve

Stephen R. Laniel            |    "I've got a match:
Carnegie Mellon University   |     Your embrace and my collapse."
laniel at cmu.edu               |     --They Might Be Giants
-------------- next part --------------
# Regress d on latitude and latitude^2
logreg_lm( CMPGlog ~ Minprice + EngSize + EngRPM + FuelCap + Wheelbase
+ Weight + Domestic + Civic, data = cars )

# Coefficients from regression logreg
cf_logreg$coefficients

# Foreign non-Civic x values
xf_seq( min( cars[cars$Domestic==0 & cars$Civic==0, "Weight"] ),
max( cars[cars$Domestic==0 & cars$Civic==0, "Weight"] ), len=100 )

# Domestic x values
xd_seq( min( cars[ cars$Domestic==1, "Weight" ] ),
max( cars[ cars$Domestic==1, "Weight" ] ), len=100 )

# Predicted y values for foreign non-Civic cars
# Here Civic = 0 and Domestic = 0, so we include no terms for
# those variables
yf_(
exp( cf[1]
+ ( cf[2] * median(cars$Minprice) )
+ ( cf[3] * median(cars$EngSize) )
+ ( cf[4] * median(cars$EngRPM) )
+ ( cf[5] * median(cars$FuelCap) )
+ ( cf[6] * median(cars$Wheelbase) )
+ ( cf[7] * xf ) ) )

# Predicted y values for domestic cars
# Here Civic = 0 and Domestic = 1, so there is no term for the
# Civic variable
yd_(
exp(  (cf[1] + cf[8])
+ ( cf[2] * median(cars$Minprice) )
+ ( cf[3] * median(cars$EngSize) )
+ ( cf[4] * median(cars$EngRPM) )
+ ( cf[5] * median(cars$FuelCap) )
+ ( cf[6] * median(cars$Wheelbase) )
+ ( cf[7] * xd ) ) )

# x- and y-coordinates for the cars plot -- here the x-coordinate
# is the Civic's Weight, and the y-coordinate is its CMPGlog

cx_cars[cars$Model=="Civic","Weight"]
cy_cars[cars$Model=="Civic","CMPGlog"]

plot( range(xd, xf, cx), range(yd, yf, cy), type="n" )
lines( xf, yf, lty=1)
lines( xd, yd, lty=2)
points( cx, cy, pch="C" )
abline( v=quantile( cars[cars$Domestic==0 & cars$Civic==0, "Weight"],
c(0.1, 0.9) ), lty=1)

# 10th and 90th percentiles for 
abline( v=quantile( cars[cars$Domestic==1, "Weight"],
c(0.1, 0.9) ), lty=2)
legend( locator(1), c("label 1", "label 2", "label 3"), lty=c(1,2,0),
pch=c("  C"))


More information about the R-help mailing list