调整在 R 中创建的包含图例的 TikZ 图形的大小

调整在 R 中创建的包含图例的 TikZ 图形的大小

我已经浏览过几个类似的问题(例如),但找不到答案。

我正在尝试使用 tikzDevice 库重新缩放在 R 中生成的图,但我遇到了图例问题。我知道 graphicx 包中的 scalebox 和 resizebox 命令不适用于 TikZ 图片中的文本,但不知道如何对其进行排序(注意:我希望我的 R 和 tex 脚本能够顺利运行,而无需对通过 tikzDevice 命令生成的文件进行任何进一步的更改)。

.tex 文件

\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 脚本

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 人物

% 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

我不确定您想要的最终图像应该是什么样的(rscale 什么以及如何?),但我很确定您正在以困难的方式做。

考虑yourbarplot.Rnw将图像缩放到文本宽度的 90% 的情况:

\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}

这是可直接编译的,无需任何其他用户操作,只需在 Rstudio 中打开并按下“编译”按钮即可(除非您在首选项中将默认的“knitr”更改为“Sweave”)。结果:

姆韦

相关内容