Insert a font into graphics with translucency (PDF)

I followed Winston Chang an excellent tutorial on how to change the font in r-graphs (base and ggplot2), and all this works fine until I turn on some translucency on the graph. The two examples below are identical, except for the second parameter: col = alpha (black, 0.5). However, while the first one looks great and scales perfectly, the second one seems to have been saved as bitmap graphics (not scales beautifully).

The problem persists when using, for example, knitr, but then it even calls up all the text and graphics on the same page as the graphic displayed as rasters.

The question seems somewhat related to this question , but I do not have the same problem as the poster in this issue, and the proposed solutions there do not solve my problem.

This works (since alpha is missing):

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")

This is not (presumably due to 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")

Session Information:

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   
+3
source share
1 answer

I'm not sure how to solve the problem with the package extrafont, but you can try showtextanother package for processing fonts in R-graphics.

The code for playing your story is as follows:

# 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 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()

A graph created with help showtextshould look something like on extrafont. The steps you need to follow with help showtextare basically:

  • , .
  • showtext.
  • showtext.begin() showtext.end().

, .

+2

All Articles