Mo *_*der 13 svg r ggplot2 knitr
我试图以svg格式保存ggplot2图.我没有使用ggsave函数,因为绘图是作为knitr文档的一部分生成的 - 我为绘图指定的设备是'svg'.
问题是原始图中的文本元素在svg文件中显示为路径,至少在inkscape中打开时.svg的源代码看起来不包含任何文本.
我的绘图功能在一个单独的文件中定义:
## @knitr plot_histogram
ggplot(mainFrame[complete.cases(mainFrame),])
+ geom_boxplot(aes(x=source, y = pPfam, fill = source))
+ scale_y_continuous(limits = c(0,1))
Run Code Online (Sandbox Code Playgroud)
在knitr文档中,我调用该函数并使用'svg'设备保存图像.
```{r plot_histogram, dev = 'svg', fig.width= 7, fig.height=4, fig.show='hold', fig.path="figure/summary"}
```
Run Code Online (Sandbox Code Playgroud)
所以我不知道如何告诉'svg'设备或ggplot2我想在保存svg时保留文本?如果可以解决问题,我也很乐意使用其他图形设备.
提前谢谢了.
Yih*_*Xie 15
无论是svg()在基础R装置也没有CairoSVG()在所述装置Cairo包支持这一点.所有文本都变成了像这样的字形
<symbol overflow="visible" id="glyph0-2">
<path style="stroke:none;" d="M 3.921875 -2.046875 L 3.921875 0.... "/>
</symbol>
Run Code Online (Sandbox Code Playgroud)
我不知道为什么会出现这种情况,您可能想要向r-devel邮件列表提交功能请求.这个问题不是特定于ggplot2/ knitr.它来自SVG设备.
OP指出RSvgDevice实际工作,我们可以通过以下方式指定设备:
my_svg <- function(file, width, height) {
library(RSvgDevice)
devSVG(file = file, width = width, height = height, bg = "white", fg = "black",
onefile = TRUE, xmlHeader = TRUE)
}
Run Code Online (Sandbox Code Playgroud)
然后在knitr代码块中,使用该选项dev='my_svg'.