[R] Solution: ploting a comparison of two scores, including the labels in the plot

Hans Ekbrand hans.ekbrand at gmail.com
Tue Nov 6 14:39:39 CET 2007


Thanks to Greg Snow and John Kane I now have a working function that
does what I wanted, that is compares two scores in a plot.

Here is the function:

## compare.ratings: plots two lists corresponding to two different
## ratings. For each element, a line connects the position of that
## element in the two lists.

compare.ratings <- function(data.frame=df, vector1=rating1, vector2=rating2, vector3=labels) {
  treshold <- 0.1

  data.frame <- data.frame[sort.list(data.frame[[vector2]]),]
    
  for(i in 2:length(data.frame[,vector2])) {
     data.frame[i,vector2] <- data.frame[i,vector2] + (treshold * (i-1))
   }

  data.frame <- data.frame[sort.list(data.frame[[vector1]]),]
  
  for(i in 1:length(data.frame[,vector1])) {
    data.frame[i,vector1] <- data.frame[i,vector1] + (treshold * (i-1))
  }

  tmp <- c(rbind( data.frame[[vector1]], data.frame[[vector2]], NA ))
  tmp2 <- rep( c(1,2,NA), nrow(data.frame) )
 
  plot(tmp2, tmp, type='b', xlim=c(0,3), xlab='', ylab='', lwd=0.5)
  text(0.9, data.frame[[vector1]], data.frame[[vector3]], adj=1, cex=0.75)
  text(2.1, data.frame[[vector2]], data.frame[[vector3]], adj=0, cex=0.75)

}

-- 
Hans Ekbrand (http://sociologi.cjb.net) <hans at sociologi.cjb.net>
A. Because it breaks the logical sequence of discussion
Q. Why is top posting bad?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : https://stat.ethz.ch/pipermail/r-help/attachments/20071106/c5b6a39f/attachment.bin 


More information about the R-help mailing list