R语言使用ggraph绘图导出pdf报错——“invalid font type”

使用ggraph绘图,在结果导出pdf的时候报错,不可用的字体类型“invalid font type” 如果继续使用ggsave导出,可以直接载入字体: library(showtext) font_add('Times','/Library/Fonts/T...

使用ggraph绘图,在结果导出pdf的时候报错,不可用的字体类型“invalid font type”


attachments-2024-01-X2KbtP0b659f811a62a29.png


如果继续使用ggsave导出,可以直接载入字体

library(showtext)
font_add('Times','/Library/Fonts/Times.ttf') 
showtext_auto() #调用showtext使其能够在ggsave()中使用

或者可以选择使用cairo_*函数进行结果图片的导出:

plotout<-ggraph(layout) +
  coord_fixed()+
  theme_graph()
#ggsave(plotout,filename = "PPI.png",width =8,height =6,dpi=300)
cairo_pdf("PPI.pdf",height=8,width=10,family="Times")
print(plotout)
dev.off()#记得关闭画布

cairo主要用于SVG、PDF 和 PostScript 格式图形文件的存储

使用方法:

svg(filename = if(onefile) "Rplots.svg" else "Rplot%03d.svg",
    width = 7, height = 7, pointsize = 12,
    onefile = FALSE, family = "sans", bg = "white",
    antialias = c("default", "none", "gray", "subpixel"),
    symbolfamily)

cairo_pdf(filename = if(onefile) "Rplots.pdf" else "Rplot%03d.pdf",
          width = 7, height = 7, pointsize = 12,
          onefile = FALSE, family = "sans", bg = "white",#支持字体自定义
          antialias = c("default", "none", "gray", "subpixel"),
          fallback_resolution = 300, symbolfamily)

cairo_ps(filename = if(onefile) "Rplots.ps" else "Rplot%03d.ps",
         width = 7, height = 7, pointsize = 12,
         onefile = FALSE, family = "sans", bg = "white",
         antialias = c("default", "none", "gray", "subpixel"),
         fallback_resolution = 300, symbolfamily)

成功导出:

attachments-2024-01-GIa1ZI5O659f872548aff.png

  • 发表于 2024-01-11 14:14
  • 阅读 ( 409 )
  • 分类:R

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
每天学习一点点
每天学习一点点

32 篇文章

作家榜 »

  1. omicsgene 658 文章
  2. 安生水 328 文章
  3. Daitoue 167 文章
  4. 生物女学霸 120 文章
  5. 红橙子 78 文章
  6. CORNERSTONE 72 文章
  7. xun 68 文章
  8. rzx 67 文章