0

Folks,

I have the following example where I use lattice for a scatterplot of (random) data. What I am trying to do is paste three variables, in a new line each. The problem is found with the third variable which needs to have a subscript.

The code found below will generate the same problem as described:

library(lattice)
library(grid)

set.seed(20)
a <- data.frame(par1=runif(10, min=1, max=100),par2=runif(10, min=1, max=100))

print(xyplot( par1 ~ par2,
              data=a,
              panel = function(x,y){
                n <- length(x)
                r <- round(cor(x, y, use="pairwise.complete.obs"), 2)
                rc <- 1
                panel.abline(a=0, b=1, lty=2, col="grey59", lwd=1.5)
                panel.xyplot(x,y, type = c("p", "g"), col="grey29", cex=0.75)
                panel.abline(lm(y ~ x), col = "red", lwd=1.5)
                grid.text(paste("n=",length(x), "\n", "r=", r, "\n", expression(paste(r[c])), "=", rc, sep=""), 0.60, .90, gp=gpar(col="black", fontsize=11), just="left")
              }
))
Andronikos K.
  • 105
  • 2
  • 9
  • I don't use lattice much, but `paste(...,expression(paste(r[c])),...)` seems like a very strange operation. – Frank May 15 '13 at 17:22
  • It is strange indeed. I only wrote it down to show that expression(paste(r[c])) is actually working on its own but when combined with paste() it doesn't. Thanks for replying. – Andronikos K. May 15 '13 at 19:17
  • as explained in `?plotmath` one cannot mix newlines and expressions. Your best bet here is to have two separate calls to `grid.text`, one for the text, one for the plotmath expression slighlty shifted in y. – baptiste May 15 '13 at 21:33
  • @baptiste Thank you. That's a good idea which I had thought as well. I was only trying to avoid it so the entire text would be perfectly aligned. I'll give it a shot this way but if someone else has a better idea of how to combine expressions with newlines, I would appreciate it. – Andronikos K. May 16 '13 at 08:32

2 Answers2

1

an alternative, if you want R to compute the line heights for you, is to use an invisible grid.table,

library(gridExtra)
multiline_text <- function(label){
 grid.table(as.matrix(strsplit(label, "\\n")[[1]]), parse=TRUE,
                  theme=theme.list(gpar.corefill = gpar(fill = NA, col = NA),
                                   core.just = "center"))
}
txt <- "First~line \n italic('see that slant') \n integral(f, a, b)"
grid.newpage() ; multiline_text(txt)  

enter image description here

baptiste
  • 75,767
  • 19
  • 198
  • 294
0

As suggested from @baptiste the code can be changed to avoid mixing newlines and expressions as below:

library(lattice)
library(grid)

set.seed(20)
a <- data.frame(par1=runif(10, min=1, max=100),par2=runif(10, min=1, max=100))

print(xyplot( par1 ~ par2,
              data=a,
              panel = function(x,y){
                n <- length(x)
                r <- round(cor(x, y, use="pairwise.complete.obs"), 2)
                rc <- 1
                panel.abline(a=0, b=1, lty=2, col="grey59", lwd=1.5)
                panel.xyplot(x,y, type = c("p", "g"), col="grey29", cex=0.75)
                panel.abline(lm(y ~ x), col = "red", lwd=1.5)
                grid.text(paste("n=",length(x), "\n", "r=", r, "\n", sep=""), 0.60, .90, gp=gpar(col="black", fontsize=11), just="left")
                grid.text(bquote(r[c] == .(rc)), 0.60, .86, gp=gpar(col="black", fontsize=11), just="left")
              }
))

Should you have other alternatives, will be happy to see them.

Andronikos K.
  • 105
  • 2
  • 9