daqana / tikzDevice

A R package for producing graphics output as PGF/TikZ code for use in TeX documents.
https://daqana.github.io/tikzDevice
132 stars 26 forks source link

Continuous color scale with ggplot2 #132

Closed krlmlr closed 8 years ago

krlmlr commented 8 years ago
library(ggplot2)
library(tikzDevice)
jpeg()
tikz()
recordGraphics({ggplot(data.frame(x=1,y=1,color=1), aes(x=x,y=y,color=color)) +
    geom_point() +
    theme_minimal() +
    theme(axis.text = element_blank(), axis.title = element_blank(), legend.text = element_blank())
}, list(), getNamespace("graphics"))
# dev.off()
# dev.off()

raises:

Error in UseMethod("depth") : 
  no applicable method for 'depth' applied to an object of class "NULL"
33: depth(path)
32: upViewport(recording = FALSE)
31: grid.draw.gTableChild(x$children[[i]], recording = FALSE)
30: grid.draw(x$children[[i]], recording = FALSE)
29: drawGTree(x)
28: recordGraphics(drawGTree(x), list(x = x), getNamespace("grid"))
27: grid.draw.gTree(gt)
26: NextMethod()
25: grid.draw.gTableChild(gt)
24: grid.draw(gt)
23: grid.draw.gtable(x$children[[i]], recording = FALSE)
22: NextMethod()
21: grid.draw.gTableChild(x$children[[i]], recording = FALSE)
20: grid.draw(x$children[[i]], recording = FALSE)
19: drawGTree(x)
18: recordGraphics(drawGTree(x), list(x = x), getNamespace("grid"))
17: grid.draw.gTree(gt)
16: NextMethod()
15: grid.draw.gTableChild(gt)
14: grid.draw(gt)
13: grid.draw.gtable(x$children[[i]], recording = FALSE)
12: NextMethod()
11: grid.draw.gTableChild(x$children[[i]], recording = FALSE)
10: grid.draw(x$children[[i]], recording = FALSE)
9: drawGTree(x)
8: recordGraphics(drawGTree(x), list(x = x), getNamespace("grid"))
7: grid.draw.gTree(gt)
6: grid.draw(gt)
5: grid.draw.gtable(gtable)
4: grid.draw(gtable) at plot.r#154
3: print.ggplot(yy$value)
2: print(yy$value)
1: source("~/.active-rstudio-document", echo = TRUE)

The error is gone when color = color is omitted or replaced by color = factor(color). It's crucial to have a device open to reproduce this issue (can be tikz, png, pdf, jpeg, doesn't matter). On the other hand, if the current device isn't tikz, the issue cannot be reproduced.

EDIT: If I return immediately from TikZ_Raster(), the error doesn't occur. Will investigate.

krlmlr commented 8 years ago

I'm pretty sure this is caused by tikz_writeRaster() opening a new PNG device for creating a PNG file. It might be that the graphics code isn't fully reentrant in this particular use case. I'll try changing this to png::writePNG(), let's hope the image differences won't be too large.

@yihui: I'm not really familiar with recording and replaying of plots. The tikzDevice creates an auxiliary PNG file for each raster grob, through the C function TikZ_Raster() which calls the R function tikz_writeRaster(). Do you think the current implementation (short of re-opening a device for this) is the right way to do it?

yihui commented 8 years ago

Sorry I have no idea here.