简体   繁体   中英

Save R plot with Mars and Venus symbols as pdf

I am trying to save plots which have female (\♀) and male (\♂) 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.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM