Question

I would like to set labels: it should be always "n" (for the number of observations) but with correct indices and values for each plot element.

For example, in the following simple example:

plot(1:4)
value<-c(10,20)
index<-c("a","b")
axis(3,at=c(2,3),labels=n?)

What should I do, so that first n has index "a" and value 10 and the second one index "b" and value 20?

Edit:

axis(3,at=c(2,3),
       labels=c(eval(substitute(expression(n==value), 
                                list(value=value[‌​1]))),
                eval(substitute(expression(n==value),
                                list(value=value[2])))))
Was it helpful?

Solution

It isn't entirely clear what you want to do. But most likely you will need to use ?expression and ?substitute to get this done. This should get you started:

plot(1:4)
value<-c(10,20)
index<-c("a","b")

lab <- rep(expression(paste("n"[index], "=", value, sep="")),2)

for (i in 1:length(lab)) {
  lab[i] <- eval(substitute(substitute(expr, list(value=value[i], index=index[i])), list(expr= as.call(lab[i]))))
}

axis(3,at=c(2,3),labels=lab)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top