【问题标题】:Embedding font in graphic with semi-transparency (PDF)在半透明图形中嵌入字体 (PDF)
【发布时间】:2014-03-29 09:51:36
【问题描述】:

我一直在关注Winston Chang's excellent tutorial,了解如何更改 r 图形(base 和 ggplot2)中的字体,只要我在图形中不包含任何半透明度,一切都可以正常工作。 下面的两个示例是相同的,除了第二个包含参数“col=alpha("black", 0.5)"。然而,虽然第一个看起来不错并且可以完美缩放,但第二个似乎已保存为光栅图形(不能很好地缩放)。

使用时问题仍然存在,例如knitr,但它甚至会导致与图形在同一页面上的所有文本和图形显示为光栅。

这个问题似乎与this question 有点相关,但我没有遇到与该问题中的发帖人相同的问题,并且那里提出的解决方案也不能解决我的问题。

这有效(因为没有 alpha):

library(extrafont)
font_install("fontcm")
loadfonts()
library(scales)

pdf("plot_cm.pdf", family="CM Roman", width=4, height=4.5)
plot(1:10, 1:10, pch=16)
dev.off()

embed_fonts("plot_cm.pdf", outfile="plot_cm_embed.pdf")

这不是(可能是因为 alpha):

library(extrafont)
font_install("fontcm")
loadfonts()
library(scales)

pdf("plot_cm.pdf", family="CM Roman", width=4, height=4.5)
plot(1:10, 1:10, pch=16, col=alpha("black", 0.5))
dev.off()

embed_fonts("plot_cm.pdf", outfile="plot_cm_embed.pdf")

会话信息:

R version 3.0.2 (2013-09-25)
Platform: i386-w64-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

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

other attached packages:
 [1] extrafont_0.16         xtable_1.7-1           knitr_1.5              dplyr_0.1.1           
 [5] directlabels_2013.6.15 quadprog_1.5-5         lubridate_1.3.3        scales_0.2.3          
 [9] ggthemes_1.6.0         stringr_0.6.2          plyr_1.8               Hmisc_3.14-0          
[13] Formula_1.1-1          survival_2.37-7        lattice_0.20-24        ggplot2_0.9.3.1       
[17] reshape2_1.2.2        

loaded via a namespace (and not attached):
 [1] assertthat_0.1      cluster_1.14.4      colorspace_1.2-4    dichromat_2.0-0     digest_0.6.4       
 [6] evaluate_0.5.1      extrafontdb_1.0     formatR_0.10        gtable_0.1.2        labeling_0.2       
[11] latticeExtra_0.6-26 MASS_7.3-29         memoise_0.1         munsell_0.4.2       proto_0.3-10       
[16] RColorBrewer_1.0-5  Rcpp_0.11.0         Rttf2pt1_1.2        tools_3.0.2   

【问题讨论】:

    标签: r pdf ggplot2 knitr


    【解决方案1】:

    我不知道如何使用 extrafont 包解决这个问题,但您可以尝试一下 showtext,这是另一个处理 R 图形字体的包。

    重现你的情节的代码如下:

    # download font file
    old = setwd(tempdir())
    download.file("http://downloads.sourceforge.net/project/cm-unicode/cm-unicode/0.7.0/cm-unicode-0.7.0-ttf.tar.xz",
        "cm-unicode-0.7.0-ttf.tar.xz")
    untar("cm-unicode-0.7.0-ttf.tar.xz", compressed = "xz")
    setwd("cm-unicode-0.7.0")
    
    # register font to "showtext" package
    library(showtext)
    font.add("CM Roman",
             regular = "cmunrm.ttf",
             italic = "cmunti.ttf",
             symbol = "cmunti.ttf")
    setwd(old)
    
    # create graphs -- enclose plotting codes by a pair of
    # showtext.begin()/showtext.end()
    library(scales)
    pdf("plot_cm.pdf", width=4, height=4.5)
    par(family = "CM Roman")
    showtext.begin()
    
    # your example
    plot(1:10, 1:10, pch=16, col=alpha("black", 0.5))
    
    # example in Winston Chang's tutorial
    curve(dnorm, from=-3, to=3, main="Normal Distribution")
    text(x=0, y=0.1, cex=1.5, expression(italic(y == frac(1, sqrt(2 * pi)) *
                                         e ^ {-frac(x^2, 2)} )))
    
    showtext.end()
    dev.off()
    

    使用showtext 创建的图表应该看起来与extrafont 的图表非常相似。使用showtext 需要采取的步骤基本上是:

    1. 查找安装在系统中或从 Web 下载的字体文件。
    2. 将这些字体注册到showtext
    3. 在一对showtext.begin()showtext.end() 中运行绘图函数。

    希望这对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2011-09-16
      • 2021-04-10
      • 1970-01-01
      • 1970-01-01
      • 2018-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-25
      相关资源
      最近更新 更多