ggsave losing unicode characters from ggplot+gridExtra

后端 未结 1 791
旧时难觅i
旧时难觅i 2021-02-13 20:03

More code than you really need, but to set the mood:

#Make some data and load packages
data<-data.frame(pchange=runif(80,0,1),group=factor(sample(c(1,2,3),80,         


        
1条回答
  •  [愿得一人]
    2021-02-13 20:46

    You have two options. One, use the cairo_pdf device instead of the default pdf in your call you ggsave, e.g.,

    library(Cairo)
    ggsave(filename="X:/yourpath/Plot1.pdf", plot=g, device=cairo_pdf,
           width = 8, height = 4, units = "in", dpi = 600)
    

    The other option would be to use expressions instead of explicit unicode characters:

    g<-ggplot(data, aes(factor(num),pchange, fill = group,width=.4)) +
      geom_bar(stat="identity", position = "dodge") +
      theme_classic()+
      theme(axis.ticks = element_blank(), 
            axis.text.x = element_blank(),
            legend.position="right")+
      scale_y_continuous(breaks=c(0,.25,.5,.75,1))+
      xlab("")+
      scale_fill_discrete("Arbitrary Group",
                          breaks=c(1,2,3),
                          labels=c(expression(phantom(0) < "1 Year"), 
                                  expression(paste(phantom(0) >= "1 Year &", phantom(0) <= "5 Years")),
                                  expression(phantom(0) > "5 Years")))
    
    
    
    ggsave(filename="Plot1.pdf", plot=g,
           width = 8, height = 4, units = "in", dpi = 600)
    

    Although, as you can see, with the second option the formatting isn't as tight as you might like.

    As to why you are experiencing this issue, according to the answer here, the pdf driver can only handle single byte encodings.

    enter image description here

    0 讨论(0)
提交回复
热议问题