I am using the tikzDevice library in R to produce tikzpicture plots in latex. It works when plots are produced one by one. However, when I want to produce the plots in a loop, I get empty files. This issue is illustrated in the following minimal example:
library(ggplot2)
library(tikzDevice)
df <- data.frame(
x = c(1, 2),
y = c(1, 2)
)
path <- 'some path here'
for (j in 1:25){
filename <- paste(path, j, sep = '')
filename <- paste(filename, '.tex', sep = '')
tikz(file = filename)
plot <-
ggplot(data=df, aes(x=x, y=y))
plot
dev.off()
}
This produces 25 empty plots. However, if I run the code without the for loop (for instance, copying the content in the loop 25 times and changing j
accordingly, then it produces the plots. What is the problem here?
plot(plot)
or one loses the plot