Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

geom_raster comes out "smeared" when saving to PDF

Tags:

r

pdf

ggplot2

When I save a ggplot that uses geom_raster, the tiles come out "smeared". It's the same result if I use ggsave() or pdf(). I don't have this problem with geom_tile or image. I don't have this problem with the RStudio, X11, or PNG graphics devices.

What's causing this? How can I fix it?

Examples:

library(ggplot2)

## doesn't work: tiles are smeared together

ggsave("smeared1.pdf",
  ggplot(cbind(expand.grid(x = 1:3, y = 1:3), fill = rnorm(9))) +
    geom_raster(aes(x = x, y = y, fill = fill)))

pdf("smeared2.pdf")
ggplot(cbind(expand.grid(x = 1:3, y = 1:3), fill = rnorm(9))) +
    geom_raster(aes(x = x, y = y, fill = fill))
dev.off()

## works fine

ggsave("not-smeared0.png",
  ggplot(cbind(expand.grid(x = 1:3, y = 1:3), fill = rnorm(9))) +
    geom_raster(aes(x = x, y = y, fill = fill)))

ggsave("not-smeared1.pdf",
  ggplot(cbind(expand.grid(x = 1:3, y = 1:3), fill = rnorm(9))) +
    geom_tile(aes(x = x, y = y, fill = fill)))

pdf("not-smeared2.pdf")
ggplot(cbind(expand.grid(x = 1:3, y = 1:3), fill = rnorm(9))) +
  geom_tile(aes(x = x, y = y, fill = fill)))
dev.off()

pdf("not-smeared3.pdf")
image(matrix(rnorm(9), 3))
dev.off()
like image 319
shadowtalker Avatar asked Apr 10 '15 19:04

shadowtalker


1 Answers

This is probably due to your PDF viewer doing interpolation of the raster. I recreated your "smeared2.pdf" on my Mac (see below) and it looked fine in Adobe Reader (right) and blurred in Preview (left). Depending on your PDF viewer, you might be able to get rid of the blurring effect by changing a setting. For example, in Preview, in the PDF tab under Preferences you can uncheck "Smooth Text and Line Art" and the PDF will display properly.

enter image description here

like image 194
eipi10 Avatar answered Nov 09 '22 09:11

eipi10