priscian
priscian

Reputation: 103

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

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.

Upvotes: 10

Views: 1141

Answers (4)

Erik Iverson
Erik Iverson

Reputation: 882

Does this thread from the ggplot2 mailing list help at all?

http://groups.google.com/group/ggplot2/browse_thread/thread/80016f7ac2a28f28/7f5b7f2c1bd9f716

The upshot is to try puttting the following line in the preamble of your LaTeX document:

\pdfpageattr {/Group << /S /Transparency /I true /CS /DeviceRGB>>} 

If you follow the links from the ggplot2 mailing list thread discussion, you'll find the thread with this potential solution ultimately posted at:

http://tug.org/pipermail/pdftex/2007-December/007480.html

Hope that helps, but I see Baptiste has weighed in both here and on the ggplot2 question, so perhaps this doesn't solve the problem.

Upvotes: 1

Triad sou.
Triad sou.

Reputation: 2971

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

Upvotes: 2

Brian Diggs
Brian Diggs

Reputation: 58835

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

Upvotes: 2

baptiste
baptiste

Reputation: 77106

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    

Upvotes: 2

Related Questions