Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2: Why Does Semi-Transparency + pdflatex Cause Heavier-Than-Normal PDF Fonts?

Tags:

r

ggplot2

ggplot2: Why Does Semi-Transparency + pdflatex Cause Heavier-Than-Normal PDF Fonts?

I've run into a problem where pdf()ing in R and then pdflatex-ing a ggplot2 image causes all of the text on the same page as the image to become emboldened, but only when alpha < 1. Here's a minimal example in R:

require("ggplot2")
"%_%" <- function(a, b) paste(a, b, sep="")
test <- function(filename, alpha)
{
  pdf(filename %_% "-fig.pdf")
  p <- ggplot(mtcars, aes(wt, mpg)) + geom_point(alpha=alpha)
  print(p); dev.off()

  latexDocument <- c(
    "\\documentclass{article}",
    "\\usepackage{Sweave}",
    "%\\pdfpageattr{/Group <</S /Transparency /I true /CS /DeviceRGB>>}",
    "\\begin{document}",
    "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
    "\\begin{figure}",
    "\\includegraphics{" %_% filename %_% "-fig}",
    "  \\caption{Figure Caption}",
    "\\end{figure}",
    "\\end{document}")

  con <- file(filename %_% ".tex"); writeLines(latexDocument, con); close(con)
  system("pdflatex " %_% filename)
}

test("test1", 1)
test("test2", 0.3)

Comparing the output files test1.pdf and test2.pdf, I notice that the latter document has heavier fonts when viewed in Acrobat or Acrobat Reader. The problem has been discussed here before, but to no resolution.

I can't seem to solve the problem, which messes up the look of reports I generate with Sweave. Does anyone have any insight into it? I'm using R version 2.13.1 on Windows.

like image 375
priscian Avatar asked Sep 07 '11 11:09

priscian


3 Answers

Try the pdf() function with an argument, colormodel = "cmyk"?

require("ggplot2")
pdf("test_cmyk.pdf", colormodel = "cmyk")
ggplot(mtcars, aes(wt, mpg)) + geom_point(size = 3, alpha = 0.2) +
  opts(title = "cmyk, alpha = 0.2")
dev.off()
embedFonts("test_cmyk.pdf")

It seems to be slightly better than colormodel = "rgb" in my environment (Win XP, Adobe Acrobat 9 Pro).

enter image description here

like image 111
Triad sou. Avatar answered Oct 08 '22 14:10

Triad sou.


Can you try to see if it's a problem with R or ggplot2 or Sweave or pdflatex, or simply your pdf viewer? I cannot reproduce the problem. Here's a screenshot using Adobe Reader on Mac OS 10.6, enter image description here

sessionInfo()
R version 2.13.1 (2011-07-08)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] en_NZ.UTF-8/en_NZ.UTF-8/C/C/en_NZ.UTF-8/en_NZ.UTF-8

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

other attached packages:
[1] ggplot2_0.8.9 proto_0.3-9.2 reshape_0.8.4 plyr_1.6    
like image 41
baptiste Avatar answered Oct 08 '22 15:10

baptiste


I think it is a matter of anti-aliasing making the text look bolder/bigger. I can see it on the axis title, but not labels. And it is more pronounced at lower magnifications. I can't prove anything, and a diff of the two PDFs was not enlightening to me.

At 200%:

View of PDFs in Acrobat Reader at 200%

At 800%:

View of PDFs in Acrobat Reader at 800%

Generated using the following code (which eliminates the Sweave and pdflatex steps)

require("ggplot2")
"%_%" <- function(a, b) paste(a, b, sep="")
test <- function(filename, alpha)
{
  pdf(filename %_% "-fig.pdf")
  p <- ggplot(mtcars, aes(wt, mpg)) + geom_point(alpha=alpha)
  print(p); dev.off()
}

test("test1", 1)
test("test2", 0.3)

Session info:

R version 2.13.1 (2011-07-08)
Platform: x86_64-pc-mingw32/x64 (64-bit)

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

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

other attached packages:
[1] xtable_1.5-6         ggplot2_0.8.9        proto_0.3-9.2       
[4] reshape_0.8.4        plyr_1.6             microbenchmark_1.1-0

loaded via a namespace (and not attached):
[1] digest_0.5.0 tools_2.13.1
like image 2
Brian Diggs Avatar answered Oct 08 '22 16:10

Brian Diggs