Saturday, March 19, 2011

SAS gplot with pointlabel

o
data crime;
  infile 'c:\crime.txt';
  input state $ crime pctmetro;
run;


/* First, we will plot crime v.s. pctmetro */
goptions reset=all;
axis1 label=(a=90 "Crime per 1,000,000");
symbol1 value=star color=red;
proc gplot data=crime;
      plot crime*pctmetro;
run;



/* In this plot, we don't know which state does the star mean */
/* Try to list the state names in the graph */
/* Need to use 'pointlabel' */

goptions reset=all;
axis1 label=(a=90 "Crime per 1,000,000");
symbol1 pointlabel=('#state' c=red h=1) value=none;
proc gplot data=crime;
      plot crime*pctmetro=1 / vaxis=axis1;
run;
quit;



We can do it in R. To label the points in R, we will use the function textxy (package{calibrate}) to do 2D label plot. The code is:

mydata=read.table('c:/crime.txt',header=F)
names(mydata)[1]="state"
names(mydata)[2]="crime"
names(mydata)[3]="pctmetro"

attach(mydata)

plot(mydata$pctmetro, mydata$crime)
textxy(mydata$pctmetro, mydata$crime, labs=mydata$state, cx=1, dcol=2)

1 comment:

  1. textxy(X, Y, labs, cx = 0.5, dcol = "black", m = c(0, 0))

    Arguments
    X x coordinates of a set of points
    Y y coordinates of a set of points
    labs labels to be placed next to the points
    cx character expansion factor
    dcol colour for the labels
    m coordinates of the origin of the plot (default (0,0))

    ReplyDelete