You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将ggplot导出为EPS格式并保留文本为可编辑文本元素?

ggplot导出EPS保留可编辑文本的解决方案

问题背景

使用ggplot绘制的图形导出为EPS格式后,在Affinity Designer中打开时文本被识别为矢量路径而非可编辑文本,需调整导出设置实现文本可编辑。

用户绘图代码:

mosai %>%
  ggplot(aes(x = Elements_mosai, y = Area_km2, fill = Période)) + 
  geom_bar(stat = "identity", width = 1, position = "dodge") + 
  scale_fill_manual(breaks = c("Avant restauration","Pendant restauration", "Après restauration"), 
values = c("#E7298A", "#A6761D", "#66A61E")) +
  labs(x = "Eléments de la mosaïque paysagère", y = "Superficie (km2)") + 
  facet_wrap(~ Rivière + Type) + 
  theme_bw()

字体配置:

# 文本元素默认配置
List of 11
 $ family       : chr ""
 $ face         : chr "plain"
 $ colour       : chr "black"
 $ size         : num 11
 $ hjust        : num 0.5
 $ vjust        : num 0.5
 $ angle        : num 0
 $ lineheight   : num 0.9
 $ margin       : 'margin' num [1:4] 0points 0points 0points 0points
  ..- attr(*, "unit")= int 8
 $ debug        : logi FALSE
 $ inherit.blank: logi TRUE
 - attr(*, "class")= chr [1:2] "element_text" "element"

# Windows字体映射
> windowsFonts()
$serif
[1] "TT Times New Roman"

$sans
[1] "TT Arial"

$mono
[1] "TT Courier New"

原导出命令:

ggsave("test.eps", device = "eps", plot = last_plot(), path = DIR_OUT, width = 40, 
height = 20, units = "cm", dpi = 500)

解决方法

1. 用Cairo PDF转EPS(最可靠)

ggplot默认EPS设备会将文本轮廓化,改用Cairo后端生成PDF再转EPS,可完美保留可编辑文本:

  • 安装并加载Cairo设备包:
install.packages("cairoDevice")
library(cairoDevice)
  • 导出Cairo PDF:
ggsave("test_cairo.pdf", device = cairo_pdf, plot = last_plot(), path = DIR_OUT, width = 40, height = 20, units = "cm")
  • 通过Ghostscript将PDF转EPS:
gs -dNOCACHE -dNOPAUSE -dBATCH -sDEVICE=eps2write -sOutputFile=test.eps test_cairo.pdf

2. 自定义PostScript设备参数

直接使用EPS设备时,通过postscript函数指定字体和编码,避免文本轮廓化:
修改导出命令:

ggsave("test.eps", 
       device = function(filename) {
         postscript(filename, 
                    family = "TT Arial", 
                    paper = "special", 
                    width = 40/2.54,  # 转换为英寸单位
                    height = 20/2.54,
                    horizontal = FALSE, 
                    onefile = FALSE, 
                    encoding = "ISOLatin1")  # 支持法语特殊字符
       }, 
       plot = last_plot(), 
       path = DIR_OUT)

3. 明确指定ggplot主题字体

确保绘图时所有文本元素都绑定正确字体,避免继承空的family值:
修改绘图代码,在主题中添加字体指定:

mosai %>%
  ggplot(aes(x = Elements_mosai, y = Area_km2, fill = Période)) + 
  geom_bar(stat = "identity", width = 1, position = "dodge") + 
  scale_fill_manual(breaks = c("Avant restauration","Pendant restauration", "Après restauration"), 
values = c("#E7298A", "#A6761D", "#66A61E")) +
  labs(x = "Eléments de la mosaïque paysagère", y = "Superficie (km2)") + 
  facet_wrap(~ Rivière + Type) + 
  theme_bw() +
  theme(text = element_text(family = "TT Arial"))  # 全局指定文本字体

额外注意事项

  • Windows系统下,确保windowsFonts()中的字体映射与实际安装字体一致,设备能正确识别"TT Arial";
  • Affinity Designer导入EPS时,检查导入设置,确认勾选"保留文本可编辑"选项(部分版本默认未开启)。

内容的提问来源于stack exchange,提问作者Jude

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 16:07:07