I use R and ggplot to create figures, which I then import as tikz code into my LaTeX file. The publisher wants me to submit in their template, which is based on [12pt]{article}. However, whenever I use the following code to export my ggplot2-figure, there is a small problem.
if (!require("pacman")) install.packages("pacman")
pacman::p_load( 'ggplot2', 'tikzDevice', 'tinytex')
trialfigure <- ggplot(mpg, aes(y = class)) +
geom_bar(aes(fill = drv)) +
scale_fill_discrete(labels = c("4" = "really long label")) +
theme(legend.position = "bottom")
trialfigure
tikz('trialfigure.tex', width = 6.5, height = 4.875, sanitize = TRUE, documentDeclaration = "\documentclass[12pt]{article}")
print(trialfigure)
dev.off()
In the LaTex Output from Overleaf, I feel like the fonts are too big, which can be seen as the "longish color legend" texts always interfere with the legend colors.
If I set my document to 10 points, everything works as expected.
I include the tikz-code as follows:
\documentclass[12pt,authoryear]{article}
\usepackage{tikz}
\usepackage{xcolor}
\usepackage[utf8]{inputenc} % for easy quotation marks ""
%%%%% for the TIKZPictures
\makeatletter
\tikzset{dangerous style/.code={
\tikzoption{clip}[]{\pgf@relevantforpicturesizefalse}
\tikzoption{use as bounding box}[]{\pgf@relevantforpicturesizefalse}
}
}
\makeatother
%%%%% for the TIKZPictures
\usepackage{graphicx}
\begin{document}
\begin{figure}[htbp!]
\centering
\resizebox{\textwidth}{!}{
% Created by tikzDevice version 0.12.3.1 on 2023-07-24 15:04:05
% !TEX encoding = UTF-8 Unicode
\begin{tikzpicture}[x=1pt,y=1pt]
\definecolor{fillColor}{RGB}{255,255,255}
\path[use as bounding box,fill=fillColor,fill opacity=0.00] (0,0) rectangle (469.75,352.32);
\begin{scope}
\path[clip] ( 0.00, 0.00) rectangle (469.75,352.32);
\definecolor{drawColor}{RGB}{255,255,255}
\definecolor{fillColor}{RGB}{255,255,255}
\path[draw=drawColor,line width= 0.6pt,line join=round,line cap=round,fill=fillColor] ( 0.00, 0.00) rectangle (469.75,352.32);
\end{scope}
\begin{scope}
\path[clip] ( 59.42, 67.12) rectangle (464.26,346.82);
\definecolor{fillColor}{gray}{0.92}
\path[fill=fillColor] ( 59.42, 67.12) rectangle (464.26,346.82);
\definecolor{drawColor}{RGB}{255,255,255}
\path[draw=drawColor,line width= 0.3pt,line join=round] (137.18, 67.12) --
(137.18,346.82);
\path[draw=drawColor,line width= 0.3pt,line join=round] (255.90, 67.12) --
(255.90,346.82);
\path[draw=drawColor,line width= 0.3pt,line join=round] (374.62, 67.12) --
(374.62,346.82);
\path[draw=drawColor,line width= 0.6pt,line join=round] ( 59.42, 90.42) --
(464.26, 90.42);
\path[draw=drawColor,line width= 0.6pt,line join=round] ( 59.42,129.27) --
(464.26,129.27);
\path[draw=drawColor,line width= 0.6pt,line join=round] ( 59.42,168.12) --
(464.26,168.12);
\path[draw=drawColor,line width= 0.6pt,line join=round] ( 59.42,206.97) --
(464.26,206.97);
\path[draw=drawColor,line width= 0.6pt,line join=round] ( 59.42,245.81) --
(464.26,245.81);
\path[draw=drawColor,line width= 0.6pt,line join=round] ( 59.42,284.66) --
(464.26,284.66);
\path[draw=drawColor,line width= 0.6pt,line join=round] ( 59.42,323.51) --
(464.26,323.51);
\path[draw=drawColor,line width= 0.6pt,line join=round] ( 77.82, 67.12) --
( 77.82,346.82);
\path[draw=drawColor,line width= 0.6pt,line join=round] (196.54, 67.12) --
(196.54,346.82);
\path[draw=drawColor,line width= 0.6pt,line join=round] (315.26, 67.12) --
(315.26,346.82);
\path[draw=drawColor,line width= 0.6pt,line join=round] (433.98, 67.12) --
(433.98,346.82);
\definecolor{fillColor}{RGB}{248,118,109}
\path[fill=fillColor] (285.58,111.79) rectangle (356.81,146.75);
\path[fill=fillColor] (303.39,150.64) rectangle (321.20,185.60);
\path[fill=fillColor] ( 77.82,228.33) rectangle (273.71,263.29);
\path[fill=fillColor] (261.84,267.18) rectangle (285.58,302.14);
\path[fill=fillColor] (143.12,306.03) rectangle (445.85,340.99);
\definecolor{fillColor}{RGB}{0,186,56}
\path[fill=fillColor] ( 77.82,111.79) rectangle (285.58,146.75);
\path[fill=fillColor] ( 77.82,150.64) rectangle (303.39,185.60);
\path[fill=fillColor] ( 77.82,189.49) rectangle (143.12,224.45);
\path[fill=fillColor] (131.24,267.18) rectangle (261.84,302.14);
\definecolor{fillColor}{RGB}{97,156,255}
\path[fill=fillColor] ( 77.82, 72.94) rectangle (107.50,107.91);
\path[fill=fillColor] ( 77.82,267.18) rectangle (131.24,302.14);
\path[fill=fillColor] ( 77.82,306.03) rectangle (143.12,340.99);
\end{scope}
\begin{scope}
\path[clip] ( 0.00, 0.00) rectangle (469.75,352.32);
\definecolor{drawColor}{gray}{0.30}
\node[text=drawColor,anchor=base east,inner sep=0pt, outer sep=0pt, scale= 0.73] at ( 54.47, 87.90) {2seater};
\node[text=drawColor,anchor=base east,inner sep=0pt, outer sep=0pt, scale= 0.73] at ( 54.47,126.75) {compact};
\node[text=drawColor,anchor=base east,inner sep=0pt, outer sep=0pt, scale= 0.73] at ( 54.47,165.59) {midsize};
\node[text=drawColor,anchor=base east,inner sep=0pt, outer sep=0pt, scale= 0.73] at ( 54.47,204.44) {minivan};
\node[text=drawColor,anchor=base east,inner sep=0pt, outer sep=0pt, scale= 0.73] at ( 54.47,243.29) {pickup};
\node[text=drawColor,anchor=base east,inner sep=0pt, outer sep=0pt, scale= 0.73] at ( 54.47,282.14) {subcompact};
\node[text=drawColor,anchor=base east,inner sep=0pt, outer sep=0pt, scale= 0.73] at ( 54.47,320.98) {suv};
\end{scope}
\begin{scope}
\path[clip] ( 0.00, 0.00) rectangle (469.75,352.32);
\definecolor{drawColor}{gray}{0.20}
\path[draw=drawColor,line width= 0.6pt,line join=round] ( 56.67, 90.42) --
( 59.42, 90.42);
\path[draw=drawColor,line width= 0.6pt,line join=round] ( 56.67,129.27) --
( 59.42,129.27);
\path[draw=drawColor,line width= 0.6pt,line join=round] ( 56.67,168.12) --
( 59.42,168.12);
\path[draw=drawColor,line width= 0.6pt,line join=round] ( 56.67,206.97) --
( 59.42,206.97);
\path[draw=drawColor,line width= 0.6pt,line join=round] ( 56.67,245.81) --
( 59.42,245.81);
\path[draw=drawColor,line width= 0.6pt,line join=round] ( 56.67,284.66) --
( 59.42,284.66);
\path[draw=drawColor,line width= 0.6pt,line join=round] ( 56.67,323.51) --
( 59.42,323.51);
\end{scope}
\begin{scope}
\path[clip] ( 0.00, 0.00) rectangle (469.75,352.32);
\definecolor{drawColor}{gray}{0.20}
\path[draw=drawColor,line width= 0.6pt,line join=round] ( 77.82, 64.37) --
( 77.82, 67.12);
\path[draw=drawColor,line width= 0.6pt,line join=round] (196.54, 64.37) --
(196.54, 67.12);
\path[draw=drawColor,line width= 0.6pt,line join=round] (315.26, 64.37) --
(315.26, 67.12);
\path[draw=drawColor,line width= 0.6pt,line join=round] (433.98, 64.37) --
(433.98, 67.12);
\end{scope}
\begin{scope}
\path[clip] ( 0.00, 0.00) rectangle (469.75,352.32);
\definecolor{drawColor}{gray}{0.30}
\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale= 0.73] at ( 77.82, 57.12) {0};
\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale= 0.73] at (196.54, 57.12) {20};
\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale= 0.73] at (315.26, 57.12) {40};
\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale= 0.73] at (433.98, 57.12) {60};
\end{scope}
\begin{scope}
\path[clip] ( 0.00, 0.00) rectangle (469.75,352.32);
\definecolor{drawColor}{RGB}{0,0,0}
\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale= 0.92] at (261.84, 46.63) {count};
\end{scope}
\begin{scope}
\path[clip] ( 0.00, 0.00) rectangle (469.75,352.32);
\definecolor{drawColor}{RGB}{0,0,0}
\node[text=drawColor,rotate= 90.00,anchor=base,inner sep=0pt, outer sep=0pt, scale= 0.92] at ( 11.81,206.97) {class};
\end{scope}
\begin{scope}
\path[clip] ( 0.00, 0.00) rectangle (469.75,352.32);
\definecolor{fillColor}{RGB}{255,255,255}
\path[fill=fillColor] (178.83, 5.50) rectangle (344.84, 33.84);
\end{scope}
\begin{scope}
\path[clip] ( 0.00, 0.00) rectangle (469.75,352.32);
\definecolor{drawColor}{RGB}{0,0,0}
\node[text=drawColor,anchor=base west,inner sep=0pt, outer sep=0pt, scale= 0.92] at (184.33, 16.52) {drv};
\end{scope}
\begin{scope}
\path[clip] ( 0.00, 0.00) rectangle (469.75,352.32);
\definecolor{fillColor}{gray}{0.95}
\path[fill=fillColor] (203.35, 11.00) rectangle (220.70, 28.34);
\end{scope}
\begin{scope}
\path[clip] ( 0.00, 0.00) rectangle (469.75,352.32);
\definecolor{fillColor}{RGB}{248,118,109}
\path[fill=fillColor] (204.06, 11.71) rectangle (219.99, 27.63);
\end{scope}
\begin{scope}
\path[clip] ( 0.00, 0.00) rectangle (469.75,352.32);
\definecolor{fillColor}{gray}{0.95}
\path[fill=fillColor] (283.04, 11.00) rectangle (300.38, 28.34);
\end{scope}
\begin{scope}
\path[clip] ( 0.00, 0.00) rectangle (469.75,352.32);
\definecolor{fillColor}{RGB}{0,186,56}
\path[fill=fillColor] (283.75, 11.71) rectangle (299.67, 27.63);
\end{scope}
\begin{scope}
\path[clip] ( 0.00, 0.00) rectangle (469.75,352.32);
\definecolor{fillColor}{gray}{0.95}
\path[fill=fillColor] (313.62, 11.00) rectangle (330.97, 28.34);
\end{scope}
\begin{scope}
\path[clip] ( 0.00, 0.00) rectangle (469.75,352.32);
\definecolor{fillColor}{RGB}{97,156,255}
\path[fill=fillColor] (314.33, 11.71) rectangle (330.26, 27.63);
\end{scope}
\begin{scope}
\path[clip] ( 0.00, 0.00) rectangle (469.75,352.32);
\definecolor{drawColor}{RGB}{0,0,0}
\node[text=drawColor,anchor=base west,inner sep=0pt, outer sep=0pt, scale= 0.73] at (226.20, 17.15) {really long label};
\end{scope}
\begin{scope}
\path[clip] ( 0.00, 0.00) rectangle (469.75,352.32);
\definecolor{drawColor}{RGB}{0,0,0}
\node[text=drawColor,anchor=base west,inner sep=0pt, outer sep=0pt, scale= 0.73] at (305.88, 17.15) {f};
\end{scope}
\begin{scope}
\path[clip] ( 0.00, 0.00) rectangle (469.75,352.32);
\definecolor{drawColor}{RGB}{0,0,0}
\node[text=drawColor,anchor=base west,inner sep=0pt, outer sep=0pt, scale= 0.73] at (336.47, 17.15) {r};
\end{scope}
\end{tikzpicture}
}
\caption{Some caption. The problem is in the "really long label".}
\label{fig:someplotlabel}
\end{figure}
\end{document}
The problem with the figure is in the large label. And I do not want to alter the tikz code, please.
I am not sure, what I could do for a reproducible example, any tips are welcome.
Is there any way to solve my problem with the 12 pt font? - e.g., by defining all figures should be 10 pt?

ggplotand then show a minimal document which includes the generated TeX code that your\inputcommand would input. – Alan Munn Jul 20 '23 at 22:40tizkdevice like here or here but centered in your issue and the simplicity, i.e., if you can show your font issue with a standardplot()andc(1,2,3)as plot data, do not useggplot2nor your large data frame were you are working on. – Fran Jul 21 '23 at 09:14