Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何更改ggplot2中的字体?_R_Ggplot2_Fonts_Extrafont - Fatal编程技术网

R 如何更改ggplot2中的字体?

R 如何更改ggplot2中的字体?,r,ggplot2,fonts,extrafont,R,Ggplot2,Fonts,Extrafont,我最近了解到,我需要保持我论文的字体与我嵌入的图形的字体一致。因为我已经决定使用泰晤士报新罗马版作为我的正文,我需要重新审视我的数字,并用相同的字体对它们进行编码。不幸的是,我所想的将是一个简单的函数已经变成了一个令人头痛的问题。按照其他人的建议,我继续安装了“extrafont”软件包,并加载了我设备上可用的所有字体(我现在运行的是Macbook供参考)。在使用了从其他堆栈溢出查询中检索到的所有必要代码后,我看到了我的图形预览,显示了正确的字体(同样是Times New Roman)。然而,在

我最近了解到,我需要保持我论文的字体与我嵌入的图形的字体一致。因为我已经决定使用泰晤士报新罗马版作为我的正文,我需要重新审视我的数字,并用相同的字体对它们进行编码。不幸的是,我所想的将是一个简单的函数已经变成了一个令人头痛的问题。按照其他人的建议,我继续安装了“extrafont”软件包,并加载了我设备上可用的所有字体(我现在运行的是Macbook供参考)。在使用了从其他堆栈溢出查询中检索到的所有必要代码后,我看到了我的图形预览,显示了正确的字体(同样是Times New Roman)。然而,在尝试使用ggsave函数导出它时,头痛就开始了。不幸的是,我收到了以下错误:

有50个或更多警告(使用warnings()查看前50个)

在运行建议函数以了解有关这些警告的更多信息时,我看到了一长串重复出现的错误:

> warnings()
Warning messages:
1: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family 'Times New Roman' not found in PostScript font database
2: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
 font family 'Times New Roman' not found in PostScript font database
3: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family 'Times New Roman' not found in PostScript font database
4: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family 'Times New Roman' not found in PostScript font database
5: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family 'Times New Roman' not found in PostScript font database
6: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
等等。这里发生了什么,如何在代码中修复它?下面提供了我使用的代码。是的,在运行loadfonts(设备)函数时,系统会适当地提示我接受该函数(y/n),我会等到它完全加载后再继续编写代码。是的,我立即运行了fonts()命令,再次检查Times New Roman是否在列表中。这是我的密码:

install.packages("extrafont")
library(extrafont)
font_import()
loadfonts(device)
fonts()

ggplot(plot1, aes(x=ecotype, y=mean, fill=treatment))+
  geom_bar(stat="identity", position="dodge")+
  geom_errorbar(aes(ymin=mean-se, ymax=mean+se), position=position_dodge(.9), 
width=.3)+
  scale_fill_manual(values=c("#CA3542","#37AFA9"), 
labels=c("Control","Exclusion"), name="Treatment")+
  labs(x="Ecotype", y="Expected Mean Dry Aboveground Biomass (g)")+
  geom_hline(aes(yintercept=0), size=.3)+
  facet_grid(site~., scales="free")+
  theme_bw()+
  theme(axis.title.x=element_text(size=16))+
  theme(axis.title.y=element_text(size=16))+
  theme(axis.text.x=element_text(size=16))+
  theme(axis.text.y=element_text(size=16))+
  theme(strip.text.y=element_text(size=16))+
  theme(legend.title=element_text(size=12))+
  theme(legend.text=element_text(size=12))+
  theme(text=element_text(family="Times New Roman"))+
  theme(legend.justification = c(0.9,1.1), legend.position=c(0.95,1), 
panel.grid.major = element_blank(), panel.grid.minor = element_blank())
ggsave("plot1.eps", height = 8, width = 5)

免责声明:我仍然是R方面的新手,因此我希望您能在上述代码的上下文中给出清晰的建议。提前谢谢

也许这个问题会有帮助,你需要
loadfonts(device='win')
谢谢。我一会儿就来看看这个。我相信我尝试了建议的代码,但我的理解是,这只是一种指定您正在使用Windows字体的方法(我在Mac上)。如果我错了,请纠正我,我会进一步研究。您也可以使用
showtext
包@DamianPopovic
font()
函数的输出是什么?当您导出到
png
或其他光栅格式时是否有警告?否则,在将绘图导出到.eps时,请尝试使用
embed_fonts()
嵌入字体