3

I've shifted through several similar questions (such as this and this), but couldn't find an answer.

I'm trying to rescale a plot produced in R, using the tikzDevice library, but I'm having trouble with the legend. I understand that the scalebox and resizebox commands from the graphicx package don't work on text within a TikZ picture, but don't know how to sort that (note: I'd like that my R and tex scripts would run smoothly, without any further changes on the file generated through the tikzDevice command).

.tex file

\documentclass[12pt,letterpaper]{article}
\usepackage{graphicx}
\usepackage{tikz}

\begin{document}

\begin{figure}[!htbp]
  \caption{Example}
  \centering
  \resizebox{1\textwidth}{!}{\input{counts}}
\end{figure}

\end{document}

R script

set.seed(62442)
# Generating example data
mat = matrix(round(runif(32, 0, 30)), nr = 4)

# Vector of colors 
cols = gray(c(0, 1 / 6, 1 / 6 * 2, 1 / 6 * 3))

# Calling tikz to export to latex
library(tikzDevice)

# Plotting
tikz("counts.tex")
barplot(mat, beside = T, col = cols, ylim = c(0, 40), xlab = "", axes = F, width = 1, space = c(.2, 5))
box()   
legend("topright", c("First var", "Second var", "Third var", "Fourth var"), lty = rep(1, 4), lwd = rep(10, 4), col = cols)
axis(2, las = 1)
title(main = "", xlab = "Counts", ylab = "Observations", outer = T)
dev.off()

TikZ figure

% Created by tikzDevice version 0.10.1 on 2017-10-01 16:26:13
% !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 (505.89,505.89);
\begin{scope}
\path[clip] (  0.00,  0.00) rectangle (505.89,505.89);
\definecolor{drawColor}{RGB}{0,0,0}
\definecolor{fillColor}{RGB}{0,0,0}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] ( 65.18, 61.20) rectangle ( 70.75,130.41);
\definecolor{fillColor}{gray}{0.17}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] ( 71.86, 61.20) rectangle ( 77.42,199.62);
\definecolor{fillColor}{RGB}{85,85,85}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] ( 78.54, 61.20) rectangle ( 84.10,179.85);
\definecolor{fillColor}{RGB}{128,128,128}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] ( 85.21, 61.20) rectangle ( 90.78,169.96);
\definecolor{fillColor}{RGB}{0,0,0}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] (118.60, 61.20) rectangle (124.16,150.19);
\definecolor{fillColor}{gray}{0.17}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] (125.28, 61.20) rectangle (130.84,328.16);
\definecolor{fillColor}{RGB}{85,85,85}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] (131.95, 61.20) rectangle (137.52,219.40);
\definecolor{fillColor}{RGB}{128,128,128}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] (138.63, 61.20) rectangle (144.20, 61.20);
\definecolor{fillColor}{RGB}{0,0,0}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] (172.02, 61.20) rectangle (177.58,278.72);
\definecolor{fillColor}{gray}{0.17}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] (178.70, 61.20) rectangle (184.26,100.75);
\definecolor{fillColor}{RGB}{85,85,85}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] (185.37, 61.20) rectangle (190.94,140.30);
\definecolor{fillColor}{RGB}{128,128,128}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] (192.05, 61.20) rectangle (197.62, 90.86);
\definecolor{fillColor}{RGB}{0,0,0}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] (225.44, 61.20) rectangle (231.00,120.52);
\definecolor{fillColor}{gray}{0.17}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] (232.11, 61.20) rectangle (237.68, 80.97);
\definecolor{fillColor}{RGB}{85,85,85}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] (238.79, 61.20) rectangle (244.36,258.94);
\definecolor{fillColor}{RGB}{128,128,128}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] (245.47, 61.20) rectangle (251.03,110.64);
\definecolor{fillColor}{RGB}{0,0,0}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] (278.86, 61.20) rectangle (284.42, 90.86);
\definecolor{fillColor}{gray}{0.17}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] (285.53, 61.20) rectangle (291.10, 80.97);
\definecolor{fillColor}{RGB}{85,85,85}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] (292.21, 61.20) rectangle (297.78,229.28);
\definecolor{fillColor}{RGB}{128,128,128}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] (298.89, 61.20) rectangle (304.45,169.96);
\definecolor{fillColor}{RGB}{0,0,0}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] (332.27, 61.20) rectangle (337.84,160.07);
\definecolor{fillColor}{gray}{0.17}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] (338.95, 61.20) rectangle (344.52,189.73);
\definecolor{fillColor}{RGB}{85,85,85}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] (345.63, 61.20) rectangle (351.19,130.41);
\definecolor{fillColor}{RGB}{128,128,128}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] (352.31, 61.20) rectangle (357.87,328.16);
\definecolor{fillColor}{RGB}{0,0,0}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] (385.69, 61.20) rectangle (391.26,209.51);
\definecolor{fillColor}{gray}{0.17}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] (392.37, 61.20) rectangle (397.94,189.73);
\definecolor{fillColor}{RGB}{85,85,85}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] (399.05, 61.20) rectangle (404.61,308.38);
\definecolor{fillColor}{RGB}{128,128,128}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] (405.73, 61.20) rectangle (411.29,110.64);
\definecolor{fillColor}{RGB}{0,0,0}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] (439.11, 61.20) rectangle (444.68,278.72);
\definecolor{fillColor}{gray}{0.17}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] (445.79, 61.20) rectangle (451.35,130.41);
\definecolor{fillColor}{RGB}{85,85,85}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] (452.47, 61.20) rectangle (458.03,278.72);
\definecolor{fillColor}{RGB}{128,128,128}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] (459.14, 61.20) rectangle (464.71,328.16);
\end{scope}
\begin{scope}
\path[clip] (  0.00,  0.00) rectangle (505.89,505.89);
\definecolor{drawColor}{RGB}{0,0,0}

\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale=  1.00] at ( 77.98, 39.60) {0};

\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale=  1.00] at (131.40, 39.60) {1};

\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale=  1.00] at (184.82, 39.60) {2};

\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale=  1.00] at (238.24, 39.60) {3};

\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale=  1.00] at (291.65, 39.60) {4};

\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale=  1.00] at (345.07, 39.60) {5};

\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale=  1.00] at (398.49, 39.60) {6};

\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale=  1.00] at (451.91, 39.60) {7};
\end{scope}
\begin{scope}
\path[clip] (  0.00,  0.00) rectangle (505.89,505.89);
\definecolor{drawColor}{RGB}{0,0,0}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 49.20, 61.20) --
    (480.69, 61.20) --
    (480.69,456.69) --
    ( 49.20,456.69) --
    ( 49.20, 61.20);
\end{scope}
\begin{scope}
\path[clip] ( 49.20, 61.20) rectangle (480.69,456.69);
\definecolor{drawColor}{RGB}{0,0,0}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] (392.67,456.69) rectangle (480.69,396.69);

\path[draw=drawColor,line width= 4.0pt,line join=round,line cap=round] (401.67,444.69) -- (419.67,444.69);
\definecolor{drawColor}{gray}{0.17}

\path[draw=drawColor,line width= 4.0pt,line join=round,line cap=round] (401.67,432.69) -- (419.67,432.69);
\definecolor{drawColor}{RGB}{85,85,85}

\path[draw=drawColor,line width= 4.0pt,line join=round,line cap=round] (401.67,420.69) -- (419.67,420.69);
\definecolor{drawColor}{RGB}{128,128,128}

\path[draw=drawColor,line width= 4.0pt,line join=round,line cap=round] (401.67,408.69) -- (419.67,408.69);
\definecolor{drawColor}{RGB}{0,0,0}

\node[text=drawColor,anchor=base west,inner sep=0pt, outer sep=0pt, scale=  1.00] at (428.67,441.25) {First var};

\node[text=drawColor,anchor=base west,inner sep=0pt, outer sep=0pt, scale=  1.00] at (428.67,429.25) {Second var};

\node[text=drawColor,anchor=base west,inner sep=0pt, outer sep=0pt, scale=  1.00] at (428.67,417.25) {Third var};

\node[text=drawColor,anchor=base west,inner sep=0pt, outer sep=0pt, scale=  1.00] at (428.67,405.25) {Fourth var};
\end{scope}
\begin{scope}
\path[clip] (  0.00,  0.00) rectangle (505.89,505.89);
\definecolor{drawColor}{RGB}{0,0,0}

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 49.20, 61.20) -- ( 49.20,456.69);

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 49.20, 61.20) -- ( 43.20, 61.20);

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 49.20,160.07) -- ( 43.20,160.07);

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 49.20,258.94) -- ( 43.20,258.94);

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 49.20,357.82) -- ( 43.20,357.82);

\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 49.20,456.69) -- ( 43.20,456.69);

\node[text=drawColor,anchor=base east,inner sep=0pt, outer sep=0pt, scale=  1.00] at ( 37.20, 57.76) {0};

\node[text=drawColor,anchor=base east,inner sep=0pt, outer sep=0pt, scale=  1.00] at ( 37.20,156.63) {10};

\node[text=drawColor,anchor=base east,inner sep=0pt, outer sep=0pt, scale=  1.00] at ( 37.20,255.50) {20};

\node[text=drawColor,anchor=base east,inner sep=0pt, outer sep=0pt, scale=  1.00] at ( 37.20,354.37) {30};

\node[text=drawColor,anchor=base east,inner sep=0pt, outer sep=0pt, scale=  1.00] at ( 37.20,453.25) {40};
\end{scope}
\end{tikzpicture}

1 Answers1

0

I am not sure of how should be the final image that you want (rscale what and how?), but I am quite sure that your are doing in the hard way.

Consider this yourbarplot.Rnw where the image is scaled to the 90% of the text width:

\documentclass{article}
\usepackage{lipsum}
\begin{document}
\lipsum[2]
<<Example,echo=F,dev='tikz', fig.cap="Use R with \\LaTeX{}", fig.align='center', out.width='.9\\linewidth'>>=
set.seed(62442)
mat = matrix(round(runif(32, 0, 30)), nr = 4)
cols = gray(c(0, 1 / 6, 1 / 6 * 2, 1 / 6 * 3))
library(tikzDevice)
par(mar=c(5,6,.5,4)) # adjust as needed
barplot(mat, beside = T, las = 1, col = cols, ylim = c(0, 40), 
    xlab = "Counts",  ylab = "Observations", width = 1, space = c(.2, 5))
legend("topright", c("First var", "Second var", "Third var", "Fourth var"), lty = rep(1, 4), lwd = rep(10, 4), col = cols)
@
\lipsum[3]
\end{document}

This is is compilable directly without any other user action that open in Rstudio and push the "Compile" button (unless you have change the default "knitr" by "Sweave" in the preferences"). The result:

mwe

Fran
  • 80,769
  • Seems rather cumbersome to do this for all the plots in the paper, especially when I need to make R scripts available for reproducibility. – Lucas De Abreu Maia Oct 02 '17 at 22:39
  • @LucasDeAbreuMaia Cumbersome? Just the contrary! The greatest strength of knitr is allow real reproducible research and literate programming. No need to translate (correctly?) every R output to the LaTeX document. As R code change, so will the final PDF, no possibility of outdated/wrong results in the PDF. Are you 110% sure with your actual routine? All is perfectly traceable in a single file, not only the R raw results, but also how they are "digested" and interpreted exactly. (...and even the R could be a bit simpler). – Fran Oct 03 '17 at 00:20
  • @LucasDeAbreuMaia Moreover, with knitr::purl("yourbarplot.Rnw") you can extract all the R code chunks of the document, without LateX text, in yourbarplot.R. The chunk options are passed as R comments, so you can still see how the R code was splitted in the .Rnw file. – Fran Oct 03 '17 at 00:43
  • I understand the advantage of using knitr, but I have good reason not to use RStudio: I'm blind and RStudio is inaccessible to my screen reader. Can I use your solution with R GUI? – Lucas De Abreu Maia Oct 03 '17 at 17:13
  • @LucasDeAbreuMaia Then knitr is a must for you. The .Rnw to .tex export is done by R commands. Of course that it can done be in the R prompt: just library(knitr); knit('yourbarblot.Rnw') or without the R console, just run Rscript -e "library(knitr); knit('my_sweave_file.Rnw')" in the command line prompt. Then the .tex to .pdf will be a job exclusive for the LaTeX engines as usual. Read https://tex.stackexchange.com/a/132724/11604. My suggestion is make a executable script for these steps but beside this, search about Rmarkdown !! – Fran Oct 03 '17 at 18:09