0

I tried several times to replace label-columns (Item1, Item2,Item3,...) with symbols in plot. I have used the ltm package. Here are codes:

fsc <- factor.scores(rasch(LSAT))
plot(fsc, include.items = TRUE)

I want to replace label-columns with the black bullets (symbols) in plot and then rename them as A1,A2,A3,A4,A5. Is it possible to do it in this case? If yes, is it possible to make them color?

  • 1
    Please read [this](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) about asking questions in a way that makes it easy for people to help you. More resources [here.](http://stackoverflow.com/help/how-to-ask) – Bryan Hanson Apr 12 '15 at 13:01
  • It is really clear my question. –  Apr 12 '15 at 13:06
  • 1
    Well, not really. What do you mean by `label-columns`? There is nothing by that name in `fsc`. And the symbols are already in the plot. Also, please add `library('ltm')` to your code to make it reproducible by others. – Bryan Hanson Apr 12 '15 at 13:30
  • The item names trace back to `str(LSAT)`. Perhaps you can change the names in a copy of the data before processing/plotting. Do you want the black bullets replaced by "item 1" etc? – Bryan Hanson Apr 12 '15 at 13:42
  • Type `plot.fscores` and you'll see the code that makes the plot. The symbols are added using `stripchart` which cannot accommodate text. You could hack the function and make it accept colors however. – Bryan Hanson Apr 12 '15 at 13:48

1 Answers1

3

You could make your own function for adding text based on the method of plot.fscores()

If you don't want to do it programmatically, you can use a dump of the function with dump("plot.fscores") and make the changes in 'dumpdata.R'.

library(ltm)
dump("plot.fscores")
## now we can make the following changes to 'dumpdata.R':

## 1. change the function name.  I used 'myPlot.fscores'

## 2. add formal arguments to the argument list so we can pass them 
##    to text(). I added 
text.labels = NULL
col.labels = NULL

## 3. on line 28, change the entire line to 
text(Beta, rep_len(0L, length(Beta)), labels = text.labels, col = col.labels)

And now we can give it a try ...

fsc <- factor.scores(rasch(LSAT))
myPlot.fscores(fsc, include.items = TRUE, 
    text.labels = rownames(fsc$coef), col.labels = seq_len(nrow(fsc$coef)))

enter image description here

Otherwise you can write some code...

## copy plot.scores for modification
ff <- plot.fscores
## add an argument to ff() - I use 'text.labels' to avoid multiple matching
fm <-formals(ff)
formals(ff) <- append(fm, list(text.labels = NULL), length(fm)-1)
## change the stripchart() call to a call to text() with out new argument
body(ff)[[6]][[3]][[2]][[4]][[6]] <- quote(
    text(Beta, rep_len(0, length(Beta)), labels = text.labels)
)
## see if it works
fsc <- factor.scores(rasch(LSAT))
ff(fsc, include.items = TRUE, text.labels = rownames(fsc$coef))

enter image description here

I'll let you have a go at the colors on that one. And because I'm reasonably terrible with recursion, I have no simpler way to find body(ff)[[6]][[3]][[2]][[4]][[6]] other than to investigate the function body manually. I know there's a recursive function out there to locate parts of a function body, but I can't remember the name of it right now and searched without success.

Rich Scriven
  • 97,041
  • 11
  • 181
  • 245