6

I use R to analyse data, ggplot to create plots, tikzDevice to print them and finally latex to create a report. THe problem is that large plots with many points fail due to the memory limit of latex. I found here https://github.com/yihui/tikzDevice/issues/103 a solution that rasterises the plot before printing the tikz file, which allows printing the points and the text individually.

require(png)
require(ggplot2)
require(tikzDevice)

## generate data
n=1000000; x=rnorm(n); y=rnorm(n)

## first try primitive
tikz("test.tex",standAlone=TRUE)
plot(x,y)
dev.off()
## fails due to memory
system("pdflatex test.tex")


## rasterise points first
png("inner.png",width=8,height=6,units="in",res=300,bg="transparent")
par(mar=c(0,0,0,0))
plot.new(); plot.window(range(x), range(y))
usr <- par("usr")
points(x,y)
dev.off()
# create tikz file with rasterised points
im <- readPNG("inner.png",native=TRUE)
tikz("test.tex",7,6,standAlone=TRUE)
plot.new()
plot.window(usr[1:2],usr[3:4],xaxs="i",yaxs="i")
rasterImage(im, usr[1],usr[3],usr[2],usr[4])
axis(1); axis(2); box(); title(xlab="x",ylab="y")
dev.off()
## this works
system("pdflatex test.tex")


## now with ggplot
p <- ggplot(data.frame(x=x, y=y), aes(x=x, y=y)) + geom_point()
## what here?

In this example the first pdflatex fails. The second succeeds due to the rasterisation.

How can I apply this using ggplot?

Jonas
  • 1,639
  • 1
  • 18
  • 29
  • 1
    You could extract the plot panel from the gtable, draw that on a borderless png, then display it as background annotation_raster or annotation_custom. Don't forget to train the scales with the same data, eg with a geom_blank layer. Needless to say this is fragile, error-prone, and limited (eg facets). A ggplot+grid-level way to rasterise specific layers would be nice and was suggested in the past, but never got traction. – baptiste Feb 05 '17 at 23:12
  • hmm, yes sounds like lots of effort that doesn't work in the end... I was hoping for sth like `geom_rasterise`, or `geom_point(raster=T)` ;-) – Jonas Feb 05 '17 at 23:31
  • It wouldn't take much to pass such an argument through to the building stage, but that would require grid grobs to have this low-level capability. And here again it's probably not that far-fetched since grid.cap provides similar functionality. – baptiste Feb 05 '17 at 23:45
  • 1
    For some geoms you can use `ggrastr` as described in [this answer](https://stackoverflow.com/a/51824715/1870254) (by me). – jan-glx Aug 13 '18 at 14:45

2 Answers2

3

here's a proof-of-principle to illustrate the steps that would be involved. As pointed out in the comments it's not recommendable or practical, but could be the basis of a lower-level implementation.

require(png)
require(ggplot2)
require(tikzDevice)

n=100; 
d <- data.frame(x=rnorm(n), y=rnorm(n), z=rnorm(n))

p <- ggplot(d, aes(x=x, y=y, colour=z, size=z, alpha=x)) + geom_point()

## draw the layer by itself on a png file
library(grid)
g <- ggplotGrob(p)
# grid.newpage()
gg <- g$grobs[[6]]$children[[3]]
gg$vp <- viewport() # don't ask me
tmp <- tempfile(fileext = "png")
png(tmp, width=10, height=4, bg = "transparent", res = 30, units = "in")
grid.draw(gg)
dev.off()
## import it as a raster layer
rl <- readPNG(tmp, native = TRUE)
unlink(tmp)

## add it to a plot - note that the positions match, 
## but the size can be off unless one ensures that the panel has the same size and aspect ratio
ggplot(d, aes(x=x, y=y)) + geom_point(shape="+",  colour="red") +
  annotation_custom(rasterGrob(rl, width = unit(1,"npc"), height=unit(1,"npc"))) +
  geom_point(aes(size=z), shape=1, colour="red", show.legend = FALSE)

enter image description here

## to illustrate the practical use, we use a blank layer to train the scales
## and set the panel size to match the png file
pf <-  ggplot(d, aes(x=x, y=y)) + geom_blank() +
  annotation_custom(rasterGrob(rl, width = unit(1,"npc"), height=unit(1,"npc"), interpolate = FALSE))

tikz("test.tex", standAlone=TRUE)
grid.draw(egg::set_panel_size(pf, width=unit(10, "cm"), height=unit(4, "cm")))
dev.off()

system("lualatex test.tex")
system("open test.pdf")

enter image description here

we can zoom in and check that the text is vector-based while the layer is (here low-res for demonstration) raster.

enter image description here

baptiste
  • 75,767
  • 19
  • 198
  • 294
  • FWIW the gridSVG package does something quite similar, with the additional trick of embedding the raster data with base64. – baptiste Feb 06 '17 at 03:22
0

ok, I will write it here because it was too big for the comment box. Instead of adding the rasterised points to a nw plot with new scales you can actually replace the original grob with the rasterised grob by g$grobs[[6]]$children[[3]] <- rasterGrob(rl). The problem is that it doesn't scale, so you have to know the size of the final image before. Then you can sue sth like this:

rasterise <- function(ggp,
                      width  = 6,
                      height = 3,
                      res.raster = 300,
                      raster.id=  c(4,3),
                      file = ""){
    ## RASTERISE
    require(grid)
    require(png)
    ## draw the layer by itself on a png file
    gb <- ggplot_build(ggp)
    gt <- ggplot_gtable(gb)
    ## calculate widths
    h <- as.numeric(convertUnit(sum(gt$heights), unitTo="in"))
    w <- as.numeric(convertUnit(sum(gt$widths) , unitTo="in"))
    w.raster <- width-w
    h.raster <- height-h
    ## print points as png
    grid.newpage()
    gg <- gt$grobs[[raster.id[1]]]$children[[raster.id[2]]]
    gg$vp <- viewport() # don't ask me
    tmp <- tempfile(fileext = "png")
    png(tmp, width=w.raster, height=h.raster, bg = "transparent", res = res.raster, units = "in")
    grid.draw(gg)
    dev.off()
    ## import it as a raster layer
    points <- readPNG(tmp, native = TRUE)
    points <- rasterGrob(points, width = w.raster, height = h.raster, default.units = "in")
    unlink(tmp)
    ## ADD TO PLOT
    gt$grobs[[raster.id[1]]]$children[[raster.id[2]]] <- points
    ## PLOT TMP
    ### HERE YOU CAN ONLY PRINT IT IN THIS DIMENSIONS!
    pdf(file, width = width, height = height)
    grid.draw(gt)
    dev.off()
}

And then use it with

data <- data.frame(x = rnorm(1000), y = rnorm(1000))
plot <- ggplot(data, aes(x = x, y = y)) +
    geom_point() +
    annotate("text", x = 2, y = 2, label = "annotation")

rasterise(ggp        = plot,
          width      = 6,
          height     = 3,
          res.raster = 10,
          raster.id  = c(4,2),
          file       = "~/test.pdf")

The problem remains the ID of the grob you want to rasterise. I didn't figure out a good way to find the correct one automatically. It depends on which layers you add to the plot.

Jonas
  • 1,639
  • 1
  • 18
  • 29