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

"Force" tikzDevice to only make tikz for gradient scales (i.e., no additional pngs) #232

Open bridgeovertroubledhuman opened 1 month ago

bridgeovertroubledhuman commented 1 month ago

Following this linked below, I would love to use tikzdevice without it creating ANY png. This means also for using "gradient scales".

Here is an example:

if (!require("pacman")) install.packages("pacman")
pacman::p_load('tidyverse' , 
               'tikzDevice',  'tinytex')

#forplotting to LATEX
options(tikzDocumentDeclaration = "\\documentclass[12pt]{article}")

set.seed(1)
df <- data.frame(
  x = runif(100),
  y = runif(100),
  z1 = rnorm(100),
  z2 = abs(rnorm(100))
)

corrs <- round(stats::cor(df ,  use = "everything" ), 2)

#to plot the upper triangle set the upper triangle to NA 
corrs[lower.tri(corrs)] <- NA

dfplot <- reshape2::melt(corrs, na.rm = TRUE)

corrplot <- ggplot(data = dfplot, aes(Var2, Var1, fill = value))+
  geom_tile(color = "white")+
  scale_fill_gradient2(low = "darkblue", high = "orange", mid = "white",   space = "Lab",
                       midpoint = 0, limit = c(-2,2), 
                       name="Pearson's Correl. Cofff.")+
  geom_text(aes(Var2, Var1, label = value), color = "black") +
  scale_y_discrete(position = "right" ) +
  guides(fill = guide_colorbar(barwidth = 8, barheight = 1,
                               title.position = "top", title.hjust = 0.5))+
  theme_minimal() + 
  theme(axis.title = element_blank(),
        legend.position = c(0.2, 0.85 ),
        legend.location = "plot",
        legend.direction = "horizontal"   ) +
  coord_fixed()

corrplot
tikz('corrplot.tex',  width =    5,  height =  5, 
     standAlone=TRUE, sanitize = TRUE) 
print(corrplot)
dev.off()

This always generates a nice LaTeX file, but also a figure called "corrplot_ras1.png".

My question: Is there any way I could circumvent this PNG generation process?

Thank you!

          @bridgeovertroubledhuman Could you create a new issue referencing this? Thanks!

Originally posted by @rstub in https://github.com/daqana/tikzDevice/issues/121#issuecomment-2275631985

rstub commented 1 month ago

Thanks for the report. As far as I understand the situation, the gradient is actually output as a raster image (a bitmap). I am not aware of an efficient way to represent that in TikZ, but maybe something will come to mind later.