r - 使用cairo在轴标题中使用表达式和unicode保存ggplot

标签 r ggplot2 cairo

bounty结束了。此问题的答案有资格获得 +100 声望奖励。赏金宽限期将在 4 小时后结束。
erc想引起对这个问题的更多关注。







我想保存以下情节:

library(ggplot2)

myplot <- ggplot(iris) +
  geom_point(aes(x = Sepal.Width, y = Sepal.Length)) +
  ylab(expression(~delta^13*C~"[\211]"))

ggsave(myplot, 
       filename = "myplot.png", 
       type = "cairo")
但我收到以下错误:
Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y,  : 
  Metric information not available for this family/device
问题必须是表达式和​​ unicode 的组合,因为这两个工作:
myplot <- ggplot(iris) +
  geom_point(aes(x = Sepal.Width, y = Sepal.Length)) +
  ylab("[\211]")

myplot <- ggplot(iris) +
  geom_point(aes(x = Sepal.Width, y = Sepal.Length)) +
  ylab(expression(~delta^13*C))
我怎么解决这个问题?
编辑:我想打印“每千位”符号。显然它的 unicode 是 U2030,我错误地认为 "\211"将是一个 unicode,但一定是别的东西。
编辑 2:与此同时,我找到了this有类似问题的问题。在那里,建议使用 encoding = "MacRoman" 保存绘图。 ,不幸的是对我不起作用:
Error in png_dev(..., res = dpi, units = "in") : 
  unused argument (encoding = "MacRoman")
编辑 3:
> capabilities()["cairo"]
cairo 
 TRUE 
> sessionInfo()
R version 4.0.1 (2020-06-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252    LC_MONETARY=German_Germany.1252 LC_NUMERIC=C                   
[5] LC_TIME=German_Germany.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] patchwork_1.1.1 ggplot2_3.3.5   dplyr_1.0.7  

最佳答案

您尝试实际使用哪个 unicode 符号。我想这只是您指定 unicode 符号的语法错误。
试试这个代码:

myplot <- ggplot(iris) +
  geom_point(aes(x = Sepal.Width, y = Sepal.Length)) +
  ylab(expression(~delta^13*C~"\U0211"))

ggsave(myplot, 
       filename = "myplot.png", 
       type = "cairo")
这对我来说非常有效。
给出一个看起来像这样的情节:
enter image description here
您使用 "\U0211" .所以"\U用于指定您想要的 unicode,然后是代码。
在此处查找 unicode 符号:
https://en.wikipedia.org/wiki/List_of_Unicode_characters
这是您必须输入以获得正确符号的第一列。
所以例如对于 U+020F 你写 "\U020F" 更新
这是有关您的 unicode 符号的新信息的示例
myplot <- ggplot(iris) +
  geom_point(aes(x = Sepal.Width, y = Sepal.Length)) +
  ylab(expression(~delta^13*C~"\U2030"))

 ggsave(myplot, 
           filename = "myplot.png", 
           type = "cairo")
给了我这个情节:
enter image description here
你介意尝试上面的确切代码吗?也尝试在不保存的情况下运行 - 因为保存情节实际上不应该是问题。
所以也试着跑
library("ggplot2")
ggplot(iris) +
      geom_point(aes(x = Sepal.Width, y = Sepal.Length)) +
      ylab(expression(~delta^13*C~"\U2030"))
在那里,看看 y 轴标签是否正确显示。
如果还是不行:
也可能是您使用的字体不包含您尝试使用的 unicode 字符。
然后将以下内容添加到您的绘图代码中:
ggplot(iris) +
  geom_point(aes(x = Sepal.Width, y = Sepal.Length)) +
  ylab(expression(~delta^13*C~"\U2030")) +
  theme(text=element_text(family="Arial Unicode MS"))
“Arial Unicode MS”实际上可以是任何可用的字体,并以您想要的形式包含 unicode 字符。
也许这些提示已经解决了问题——如果不只是写另一个评论,也许你还需要一些进一步的步骤。
进一步的步骤:
由于它仍然无法正常工作并且您提到它似乎特定于使用 type = cairo 进行保存尝试以下操作:
首先提供更多信息并检查 cairo用这个命令:
capabilities()["cairo"]
结果应该是真的。
然后向我们发布有关您的系统的一些信息
sessionInfo()
由于这些字体和绘图内容通常取决于您的操作系统。
然后我们 100% 确定,我们有可用的字体/unicode 符号:
library(ggplot2)
install.packages("extrafont")
library("extrafont")
font_import()
loadfonts()
fonts()
对 fonts() 的调用现在应该会输出哪些字体可用。
也许您可以另外发布这些信息,我们可以为您提供进一步的步骤。
之后你可以再试一次,单独使用 extrafonts() 有时已经解决了问题:
library("ggplot2")
library("extrafont")
font_import()
loadfonts()

myplot <- ggplot(iris) +
  geom_point(aes(x = Sepal.Width, y = Sepal.Length)) +
  ylab(expression(~delta^13*C~"\U2030")) +
  theme(text=element_text(family="Arial Unicode MS"))
  
ggsave(myplot, 
       filename = "myplot.png", 
       type = "cairo")
正如我所说的 capabilities()["cairo"]必须返回 truefamily="Arial Unicode MS"必须是 fonts() 的某种字体回来
在准备好 extrafont 后也值得尝试不同的 grDevice
library("extrafont")
font_import()
loadfonts()

myplot <- ggplot(iris) +
  geom_point(aes(x = Sepal.Width, y = Sepal.Length)) +
  ylab(expression(~delta^13*C~"\U2030")) +
  theme(text=element_text(family="Arial Unicode MS"))
  
ggsave(myplot, 
       filename = "myplot.png", 
       type = "cairo-png")
还有更多你可以尝试......这是一篇非常有趣的文章about saving graphics on different OS .有趣的是,根据您的操作系统和图形设备,导出的数字看起来略有不同。
只需在您的评论中阅读您想使用开罗来提高质量。试试看它是否适用于 ragg而不是开罗 - 质量可能会更好。

ragg provides a set of high quality and high performance raster devices, capable of producing png, tiff, or ppm files, or a matrix of raw color values directly within R.

ragg is part of our broader effort to improve graphics performance and quality in R at all levels of the stack

library(ragg)

myplot <- ggplot(iris) +
  geom_point(aes(x = Sepal.Width, y = Sepal.Length)) +
  ylab(expression(~delta^13*C~"\U2030")) +
  theme(text=element_text(family="Arial Unicode MS"))

ggsave(
       filename = "myplot3.png",
       myplot,
       device = ragg::agg_png( 
         width = 3000,
         height = 3000, 
         units = "px",
         res = 500)
)
由于您现在仍然有一些问题 expression ,您也可以将此与 ggtext 结合使用.那么就不需要expression .
myplot <- ggplot(iris) +
  geom_point(aes(x = Sepal.Width, y = Sepal.Length)) +
  ylab("\U03B4<sup>13</sup>C\U2030") +
  theme(
    axis.title.y = element_markdown(size = 11, lineheight = 1.2)
  )

ggsave(
       filename = "myplot4.png",
       myplot,
       device = ragg::agg_png( 
         width = 3000,
         height = 3000, 
         units = "px",
         res = 500)
)
使用包 ggtext基本上,您可以使用轴标题执行各种操作(粗体、多种颜色、不同大小,......全部在一个标签中)。另见 ggtext手动的。

关于r - 使用cairo在轴标题中使用表达式和unicode保存ggplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69407793/

相关文章:

c++ - 如果我想关闭之前打开的窗口,则 X11-Window-ID 无效。 (C/C++)

html - '###'在flexdashboard/rmarkdown中添加了什么HTML?

随着 data.table 的不同窗口的滚​​动总和

r - stat_compare_means 对选定组进行方差分析

r - 使用 gridExtra 和 annotation_custom() 向 ggplot 添加表格会更改 y 轴限制

使用 cairo 库创建高光效果

Cairo C 程序不会绘制到 x11 窗口

删除输出中的级别属性 - R

r - 如何在 R 中将 str_split 与正则表达式一起使用?

r - 如果使用scale_x_datetime,时间序列图会偏移2小时