gpt4 book ai didi

r - ggsave 从 ggplot+gridExtra 中丢失 unicode 字符

转载 作者:行者123 更新时间:2023-12-01 18:05:40 27 4
gpt4 key购买 nike

代码比您真正需要的多,但为了营造气氛:

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

它看起来像这样: Actual Plot

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

最佳答案

你有两个选择。一,在调用 ggsave 时使用 cairo_pdf 设备而不是默认的 pdf,例如,

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)

尽管如此,正如您所看到的,使用第二个选项时,格式并不像您希望的那么严格。

至于为什么会遇到这个问题,根据答案herepdf 驱动程序只能处理单字节编码。

enter image description here

关于r - ggsave 从 ggplot+gridExtra 中丢失 unicode 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28746938/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com