Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use an arrow inside the title of a plot?

Tags:

plot

r

how can I use an right arrow inside the title of a barplot? I tried the following:

counts <- c(0.2, 0.4)
barplot(counts, main="A → B", horiz=TRUE, col=2:3, xlab="Time", xlim=c(0, 1.0))

In R the title of the plot looks good but when I save the plot to file, e.g. pdf, the right arrow in the title is replaced by three dots '...'.

I am using R under Windows 7.

Thanks!

like image 204
Jonas Avatar asked Dec 14 '22 06:12

Jonas


2 Answers

We can use expression() :

set.seed(1)
d1 <- data.frame(y = rnorm(100), x = rnorm(100))

plot(y ~ x, data = d1,ylab = expression(y %->% x),xlab = expression(x %->% y))

List of expressions can be found at : https://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/plotmath.html

like image 157
Gaurav Taneja Avatar answered Dec 16 '22 19:12

Gaurav Taneja


The current answer is correct but I thought I would add some tips on extending it to situations where there were no plotmath %...% infix operators for a "special character". The ?plotmath page refers us to the points page for Symbol-font characters and this is a modified version of the TestChars() function defined there that has octal (or optionally decimal) values needed to choose one of the displayed glyphs on your system:

TestCharsSym <- function(sign = 1, font = 5, oct = TRUE, ...)
{
  MB <- l10n_info()$MBCS
  r <- if(font == 5) { 
    sign <- 1
    c(32:126, 160:254)
  } else {
    if(MB) 32:126 else 32:255
  }
  if (sign == -1) r <- c(32:126, 160:255)
  par(pty = "s")
  plot(c(-1, 16), c(-1, 16), 
       type = "n", 
       xlab = "", 
       ylab = "",
       xaxs = "i", 
       yaxs = "i",
       main = sprintf("sign = %d, font = %d", sign, font)
  )
  grid(17, 17, lty = 1)
  mtext(paste("MBCS:", MB, "  Use symbol(\"...\") "))
  for(i in r){ 
    try(points(i%%16, i%/%16, pch = sign*i, font = font, ...))
    text(i%%16 - 0.3, i%/%16 - 0.3, 
         if( oct ){ 
           paste0("\\", as.octmode(i))
         } else{
           i
         },
         cex = 0.5)
  }
}
TestCharsSym()

enter image description here

My system is OSX as could be seen if you were looking at my interactive plot device for the 240-glyph but the pdf() device no longer uses the Apple Symbol font. To use this information in an expression with *symbol(...), you need to "encode" the decimal value in octal and pass to that plotmath() function with a preceding quoted backslash. The forward arrow could be produced with:

main=expression(A *symbol('\256')* B)
like image 39
IRTFM Avatar answered Dec 16 '22 20:12

IRTFM