ggsave从ggplot + gridExtra丢失unicode字符 [英] ggsave losing unicode characters from ggplot+gridExtra

查看:44
本文介绍了ggsave从ggplot + gridExtra丢失unicode字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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,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格式获取的绘图):

Here's what it should look like (in terms of the characters in the key; plot taken as jpeg directly from Rstudio plot window):

推荐答案

您有两个选择.一种,在呼叫 ggsave (例如

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)

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

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.

这篇关于ggsave从ggplot + gridExtra丢失unicode字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆