The code adds some completely useless invisible (or rather white) stuff. The lines
\clip(0pt,403pt) -- (389.957pt,403pt) -- (389.957pt,99.6166pt) -- (0pt,99.6166pt) -- (0pt,403pt);
\color[rgb]{1,1,1}
\fill(3.76406pt,399.236pt) -- (380.923pt,399.236pt) -- (380.923pt,253.19pt) -- (3.76406pt,253.19pt) -- (3.76406pt,399.236pt);
\fill(53.4497pt,394.719pt) -- (374.901pt,394.719pt) -- (374.901pt,289.325pt) -- (53.4497pt,289.325pt) -- (53.4497pt,394.719pt);
draw a white background that is larger than the actual picture. TikZ sees that and thinks it is part of the picture. Simply removing/uncommenting these lines removes most of the whitespace.
Near the end of the first scope,
\color[rgb]{1,1,1}
\fill(3.76406pt,249.426pt) -- (386.193pt,249.426pt) -- (386.193pt,103.381pt) -- (3.76406pt,103.381pt) -- (3.76406pt,249.426pt);
does the same.
Additionally (near the end of the second scope
),
\pgftext[center, base, at={\pgfpoint{220.95pt}{106.392pt}}]{\sffamily\fontsize{9}{0}\selectfont{\textbf{ }}}
adds a blank node below the picture, again enlarging the bounding box.
Removing all those lines gives a tight bounding box.
As far as I know, TikZ cannot do the cropping for you, as it can't know whether the white stuff is intentional or not (there might for example be a dark background behind the image so that white is visible).
Are you sure tikzDevice doesn't have an option to turn that off? TikZ calculates the bounding box automatically so this is kind of an artifact left from rather old PSTricks habits. And I think you would be better off having the data from R and using directly pgfplots instead of this.
Here is a very stupid way of doing it. It should only be used on these type of pictures but not others. One can also take the long way and consider if \path is given an option that starts with u
or c
. Again I would skip this way of operating completely and use pgfplots
\documentclass[11pt]{standalone}
\usepackage{tikz}
\makeatletter
\tikzset{dangerous style/.code={
\tikzoption{clip}[]{\pgf@relevantforpicturesizefalse}
\tikzoption{use as bounding box}[]{\pgf@relevantforpicturesizefalse}
}
}
\makeatother
\begin{document}
\begin{tikzpicture}[x=1pt,y=1pt,dangerous style]
\definecolor[named]{fillColor}{rgb}{1.00,1.00,0.00}
\path[use as bounding box] (0,0) rectangle (361.35,361.35);% DISABLE THIS LINE
\begin{scope}
\path[clip] ( 0.00, 0.00) rectangle (361.35,361.35);% DISABLE THIS LINE
\definecolor[named]{drawColor}{rgb}{0.00,0.00,0.00}
\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale= 1.00] at (195.07, 18.72) {$n\cdot\lambda\cdot d$};
\node[text=drawColor,rotate= 90.00,anchor=base,inner sep=0pt, outer sep=0pt, scale= 1.00] at ( 12.96,187.87) {$x_n$};
\end{scope}
\begin{scope}
\path[clip] ( 0.00, 0.00) rectangle (361.35,361.35);% DISABLE THIS LINE
\definecolor[named]{drawColor}{rgb}{0.00,0.00,0.00}
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 60.03, 73.44) -- (307.81, 73.44);
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 60.03, 73.44) -- ( 60.03, 66.24);
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] (101.33, 73.44) -- (101.33, 66.24);
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] (142.62, 73.44) -- (142.62, 66.24);
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] (183.92, 73.44) -- (183.92, 66.24);
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] (225.22, 73.44) -- (225.22, 66.24);
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] (266.51, 73.44) -- (266.51, 66.24);
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] (307.81, 73.44) -- (307.81, 66.24);
\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale= 1.00] at ( 60.03, 47.52) {1e-06};
\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale= 1.00] at (101.33, 47.52) {2e-06};
\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale= 1.00] at (142.62, 47.52) {3e-06};
\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale= 1.00] at (183.92, 47.52) {4e-06};
\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale= 1.00] at (225.22, 47.52) {5e-06};
\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale= 1.00] at (266.51, 47.52) {6e-06};
\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale= 1.00] at (307.81, 47.52) {7e-06};
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 59.04, 90.96) -- ( 59.04,291.82);
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 59.04, 90.96) -- ( 51.84, 90.96);
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 59.04,141.17) -- ( 51.84,141.17);
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 59.04,191.39) -- ( 51.84,191.39);
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 59.04,241.61) -- ( 51.84,241.61);
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 59.04,291.82) -- ( 51.84,291.82);
\node[text=drawColor,anchor=base east,inner sep=0pt, outer sep=0pt, scale= 1.00] at ( 44.64, 86.80) {5};
\node[text=drawColor,anchor=base east,inner sep=0pt, outer sep=0pt, scale= 1.00] at ( 44.64,137.02) {10};
\node[text=drawColor,anchor=base east,inner sep=0pt, outer sep=0pt, scale= 1.00] at ( 44.64,187.23) {15};
\node[text=drawColor,anchor=base east,inner sep=0pt, outer sep=0pt, scale= 1.00] at ( 44.64,237.45) {20};
\node[text=drawColor,anchor=base east,inner sep=0pt, outer sep=0pt, scale= 1.00] at ( 44.64,287.67) {25};
\end{scope}
\begin{scope}
\path[clip] ( 59.04, 73.44) rectangle (331.11,302.31);% DISABLE THIS LINE
\definecolor[named]{drawColor}{rgb}{0.47,0.00,0.05}
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 69.12, 81.92) circle ( 2.70);
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] (119.50,121.09) circle ( 2.70);
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] (169.88,169.29) circle ( 2.70);
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] (220.27,210.47) circle ( 2.70);
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] (270.65,250.65) circle ( 2.70);
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] (321.03,293.83) circle ( 2.70);
\path[draw=drawColor,line width= 0.4pt,dash pattern=on 1pt off 3pt ,line join=round,line cap=round] ( 59.04, 72.98) -- (331.11,302.77);
\end{scope}
\end{tikzpicture}
\end{document}
Best Answer
bb
is really only intended for correcting EPS files with incorrect bounding boxes (or PS files that you want to treat as EPS by supplying a missing bounding box)If you want to trim an existing figure use the viewport, trim or clip options (which just differ in the way in which the coordinates of the region are specified)