Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save R plot with Mars and Venus symbols as pdf

Tags:

plot

r

pdf

I am trying to save plots which have female (\u2640) and male (\u2642) symbols. Here is an example to create a plot using this symbols (I am using RStudio):

gender <- rbinom(n=100, size=100, prob=0.5)
plot(gender, cex=2.5,
       pch=ifelse(gender %% 2 == 0, -0x2642L, -0x2640L),
       col=ifelse(gender %% 2 == 0, 2, 3), main="\u2640 and \u2642 Symbols")

It works and generates a plot with those symbols Plot. I can save it as picture (PNG) but when I try to save it as pdf all the symbols don't show up Plot.

Here is how I try to save it as pdf:

pdf("plot.pdf")
gender <- rbinom(n=100, size=100, prob=0.5)
plot(gender, cex=2.5,
       pch=ifelse(gender %% 2 == 0, -0x2642L, -0x2640L),
       col=ifelse(gender %% 2 == 0, 2, 3), main="\u2640 and \u2642 Symbols")

dev.off() 

I saw another post about similar problem here and it was suggested to use the CairoPDF. It did not work. I tried other family settings but it did not work either. Is there any other work around to save it as pdf with those symbols or the only way it is to save it as a picture. I would prefer to save it as pdf.

After a lot of tentatives I switched to command line and use quartz. After plotting the graph I use:

quartz.save(type = 'pdf', file = 'output.pdf')

It works perfectly. Why does it not works using the first code pdf("plot.pdf") but works with quartz.save(type = 'pdf', file = 'output.pdf')? Is it something with my system?

Thank you.

like image 278
degopwn Avatar asked Nov 01 '22 19:11

degopwn


1 Answers

On my Mac this gives a pdf with astrological symbols. (Cobbled together from a search of similar questions on SO.) I didn't make the extra effort to "wrap" the full set neatly so the "printing of the later ones doesn't show up, but you can see Mars and Venus.

cairo_pdf("Venus_Mars.pdf",family="ArialUnicodeMS")
plot(1,1)
TestUnicode <- function(start="263c", end="2653", ...)
  {
    nstart <- as.hexmode(start)
    nend <- as.hexmode(end)
    r <- nstart:nend
    s <- ceiling(sqrt(length(r)))
    for(i in seq(r)) {
      try(points(.6+(i/10), .8 , pch=-1*r[i],...))
    }
  }
 TestUnicode()
dev.off()
like image 169
IRTFM Avatar answered Nov 08 '22 04:11

IRTFM