【问题标题】:ggsave losing unicode characters from ggplot+gridExtraggsave 从 ggplot+gridExtra 丢失 unicode 字符
【发布时间】:2015-02-26 15:54:46
【问题描述】:

比你真正需要的更多的代码,但要设置心情:

#Make some data and load packages
data<-data.frame(pchange=runif(80,0,1),group=factor(sample(c(1,2,3),80,replace=T)))
library(dplyr)
library(magrittr)
library(gridExtra)
library(ggplot2)
data%<>%arrange(group,pchange) %>% mutate(num=1:80)

#Make plot that includes unicode characters
g1<-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("< 1 Year", "\u2265 1 Year & \n\u2264 5 Years","> 5 Years"))


#I want to add an A below the plot (this may not be necessary for the issue, but its a part of the workflow so I thought I'd include it.
g <- arrangeGrob(plot=g1,
                 sub = textGrob("A",
                                x = .1,
                                hjust = .5,
                                vjust=-2,
                                gp = gpar(fontface = "bold",
                                          fontsize = 16,
                                          col="black")))

#Save the plot
ggsave(filename="X:/yourpath/Plot1.pdf", plot=g,
       width = 8, height = 4, units = "in", dpi = 600)

它是这样的:

它应该是这样的(就键中的字符而言;直接从 Rstudio 绘图窗口以 jpeg 形式绘制的绘图):

【问题讨论】:

    标签: r unicode ggplot2


    【解决方案1】:

    你有两个选择。一、使用cairo_pdf设备代替默认的pdf在你调用ggsave,例如,

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

    另一种选择是使用表达式而不是显式的 unicode 字符:

    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)
    

    尽管如您所见,使用第二个选项的格式并不像您希望的那样严格。

    至于你为什么会遇到这个问题,根据here的回答,pdf驱动只能处理单字节编码。

    【讨论】:

    • 我无法通过ggsave()device=cairo_pdf 合作。我收到错误:Error in grid.newpage() : cairo error 'error while writing to output stream' 有什么建议吗?
    • 你能用 Cairo 包中的CairoPDF 试试吗?
    • 1) 要么。我收到 g 或 g1 对象的错误。 2) 此外,从quick 搜索来看,Cairo 似乎正在生成位图而不是矢量输出。如果这是真的,我最好保持矢量格式。因此,我将使用expressions,尽管我想不出一个像以前那样将第二个标签拉到两行的好方法。
    • 任何 pdf 都是矢量格式。我认为你误解了那个帖子。
    • device=cairo_pdf 在 Ubuntu 16.04 上解决了这个问题。
    猜你喜欢
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    • 2016-05-22
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    相关资源
    最近更新 更多