shadowtalker
shadowtalker

Reputation: 13863

geom_raster comes out "smeared" when saving to PDF

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

Upvotes: 14

Views: 2924

Answers (1)

eipi10
eipi10

Reputation: 93821

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

Upvotes: 17

Related Questions