Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R ggplot: geom_tile lines in pdf output

Tags:

r

pdf

ggplot2

I'm constructing a plot that uses geom_tile and then outputting it to .pdf (using pdf("filename",...)). However, when I do, the .pdf result has tiny lines (striations, as one person put it) running through it. I've attached an image showing the problem. Minimal example

Googling let to this thread, but the only real advice in there was to try passing size=0 to geom_tile, which I did with no effect. Any suggestions on how I can fix these? I'd like to use this as a figure in a paper, but it's not going to work like this.

Minimal code:

require(ggplot2)
require(scales)
require(reshape)

volcano3d <- melt(volcano) 
names(volcano3d) <- c("x", "y", "z") 
 v <- ggplot(volcano3d, aes(x, y, z = z)) 

pdf("mew.pdf")
print(v + geom_tile(aes(fill=z)) + stat_contour(size=2) + scale_fill_gradient("z"))
like image 610
Winawer Avatar asked Apr 23 '12 07:04

Winawer


2 Answers

This happens because the default colour of the tiles in geom_tile seems to be white.

To fix this, you need to map the colour to z in the same way as fill.

print(v + 
  geom_tile(aes(fill=z, colour=z), size=1) + 
  stat_contour(size=2) + 
  scale_fill_gradient("z")
)

enter image description here

like image 139
Andrie Avatar answered Nov 01 '22 19:11

Andrie


Try to use geom_raster:

pdf("mew.pdf")
print(v + geom_raster(aes(fill=z)) + stat_contour(size=2) + scale_fill_gradient("z"))
dev.off()

good quality in my environment.

enter image description here

like image 7
kohske Avatar answered Nov 01 '22 17:11

kohske