Create data partition figure - plot

I would like to create a figure in Latex similar to this (but then also with percentages in the blocks):
And I managed to get this far (MWE):
\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{arrows,calc,positioning}
\usepackage{xcolor}
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%
\begin{document}
\tikzset{
train/.style={
text=black,
draw,
minimum height=1cm,
minimum width=7cm,
left color=orange, right color=orange!30!white,shading angle=90},
val/.style={
draw,
text=black,
minimum height=1cm,
minimum width=2cm,
left color=orange!30!white, right color=green!30!white,shading angle=90},
test/.style={
draw,
text=black,
fill=cyan,
minimum height=1cm,
minimum width=1cm}}
\begin{tikzpicture}[thin,black]
\path
(0,0) node[train] (N) {70\%}
++(0:4.5) node[val] (C) {20\%}
+(0:2) node[test] (O) {10\%};
\end{tikzpicture}
\end{document}
Which results in:
Clearly, the arrows with train, validation, and test descriptions are still missing and I haven't been able to find out how to create this.
How would I go about this?
Thanks!

Normally I would recommend the decorations.pathreplacing library to add large curly braces to tikz pictures, but in this particular siltation, it might be easier to draw a few paths with rounded corners:
\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{arrows,calc,positioning}
\usepackage{xcolor}
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%
\begin{document}
\tikzset{
train/.style={
text=black,
draw,
minimum height=1cm,
minimum width=7cm,
left color=orange, right color=orange!30!white,shading angle=90},
val/.style={
draw,
text=black,
minimum height=1cm,
minimum width=2cm,
left color=orange!30!white, right color=green!30!white,shading angle=90},
test/.style={
draw,
text=black,
fill=cyan,
minimum height=1cm,
minimum width=1cm}}
\begin{tikzpicture}[thin,black]
\path
(0,0) node[train] (N) {70\%}
++(0:4.5) node[val] (C) {20\%}
+(0:2) node[test] (O) {10\%};
\draw[->,rounded corners=1mm] (-3.5,0.6) |- (0,1) -- ++(0,0.5);
\draw[->,rounded corners=1mm] (3.5,0.6) |- (0,1) -- ++(0,0.5);
\draw[->,rounded corners=1mm] (3.5,0.6) |- (4.5,1) -- ++(0,0.5);
\draw[->,rounded corners=1mm] (5.5,0.6) |- (4.5,1) -- ++(0,0.5);
\draw[->,rounded corners=1mm] (6,0.6) |- (6.5,1) -- ++(0,0.5);
\draw[->,rounded corners=1mm] (7,0.6) |- (6.5,1) -- ++(0,0.5);
\node at (0,1.7) {Train};
\node at (4.5,1.7) {Validation};
\node at (6.5,1.7) {Test};
\end{tikzpicture}
\end{document}

Related

TikZ: shaded area enveloping a graph

I need to draw a shaded area enveloping a graph like in the example below. I am looking for an automated procedure and a more satisfying result. Compared to the example, I need smoother edges and more regular distances between vertices (circles) and the boundaries of the area.
Drawing arbitrary shapes in TikZ are time consuming, I suspect that a smart trick might do the job, like creating large overlapping areas centered at each vertex. Any suggestions?
And here is a minimal working LaTeX code to generate the graph which has to be enveloped:
\documentclass[tikz]{standalone}
\usepackage{tikz}
\begin{document}
\tikzstyle{vertex}=[circle,draw=black,thick]
\begin{tikzpicture}
\foreach \pos/\name in {{(0,1)/1}, {(1,1)/2}, {(0,0)/3}, {(1,0)/4}, {(2,0)/5}, {(1,-.7)/6}}
\node[vertex] (\name) at \pos {};
\foreach \one/\two in {{1/2}, {1/3}, {1/4}, {1/6}, {2/3}, {2/4}, {2/5}, {3/6}, {4/6}, {4/5}}
\draw[thick] (\one) -- (\two);
\end{tikzpicture}
\end{document}
You could connect your nodes with a polygon with a tick stroke:
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\begin{document}
\tikzset{vertex/.style={circle,draw=black,thick}}
\begin{tikzpicture}
\foreach \pos/\name in {{(0,1)/1}, {(1,1)/2}, {(0,0)/3}, {(1,0)/4}, {(2,0)/5}, {(1,-.7)/6}}
\node[vertex] (\name) at \pos {};
\foreach \one/\two in {{1/2}, {1/3}, {1/4}, {1/6}, {2/3}, {2/4}, {2/5}, {3/6}, {4/6}, {4/5}}
\draw[thick] (\one) -- (\two);
\begin{scope}[on background layer]
\filldraw[lightgray,line width=20pt,rounded corners=5pt] (1.center) -- (2.center) -- (5.center) -- (4.center) -- (6.center) -- (3.center) -- cycle;
\end{scope}
\end{tikzpicture}
\end{document}

Figures with boxes, arrows etc. in Markdown

I am interested in displaying a causal mechanism visually with some plain boxes/circles connected by arrows in my paper written in RMarkdown. Something like this:
How is this done in RMarkdown? Would you recommend me to create a picture in a another app and import to RMarkdown like a picture?
I think the best way to do this is to use Tikz to draw the graphics. There's an example at https://github.com/yihui/knitr-examples/blob/master/058-engine-tikz.Rmd, where the code chunk includes
```{tikz, tikz-ex, fig.cap = "Funky tikz", fig.ext = 'png', cache=TRUE}
\usetikzlibrary{arrows}
\begin{tikzpicture}[node distance=2cm, auto,>=latex', thick, scale = 0.5]
\node (P) {$P$};
\node (B) [right of=P] {$B$};
\node (A) [below of=P] {$A$};
\node (C) [below of=B] {$C$};
\node (P1) [node distance=1.4cm, left of=P, above of=P] {$\hat{P}$};
\draw[->] (P) to node {$f$} (B);
\draw[->] (P) to node [swap] {$g$} (A);
\draw[->] (A) to node [swap] {$f$} (C);
\draw[->] (B) to node {$g$} (C);
\draw[->, bend right] (P1) to node [swap] {$\hat{g}$} (A);
\draw[->, bend left] (P1) to node {$\hat{f}$} (B);
\draw[->, dashed] (P1) to node {$k$} (P);
\end{tikzpicture}
```
and the output (in HTML or PDF format; this is from PDF) looks like this:

How to make a graph (nodes and edges) on Latex?

I new coding in latex and I want to make high-quality graphs (nodes and edges), in particular, I need to make this kind of plots.
Is it that even possible?
That is not only "even possible", that is always possible. This sort of diagram can be easily created using TikZ and similar packages.
If you already have the coordinates of the nodes, the directions of the arrows etc., you may start from this. This is not your diagram, but is a good starting point.
\documentclass[tikz,margin=3]{standalone}
\usetikzlibrary{shadows} % Shadows for nodes
\begin{document}
\begin{tikzpicture}
\tikzset{% This is the style settings for nodes
dep/.style={circle,minimum size=1cm,fill=orange!20,draw=orange,
general shadow={fill=gray!60,shadow xshift=1pt,shadow yshift=-1pt}},
cli/.style={circle,minimum size=1cm,fill=white,draw,
general shadow={fill=gray!60,shadow xshift=1pt,shadow yshift=-1pt}},
spl/.style={cli,append after command={
node[circle,draw,dotted,
minimum size=1.5cm] at (\tikzlastnode.center) {}}},
c1/.style={-stealth,very thick,red!80!black},
v2/.style={-stealth,very thick,yellow!65!black},
v4/.style={-stealth,very thick,purple!70!black}}
\node[dep] (0) at (0,0) {0};
\node[cli] (7) at (-1,-2) {7};
\node[spl] (8) at (2,-2) {8};
\draw[c1] (0) to[bend right] (7);
\draw[v2] (7) -- (8);
\draw[v4] (8) -- (2,-1) -- (0);
\end{tikzpicture}
\end{document}
You can use a matrix or whatever you want to add a legend
\documentclass[tikz,margin=3]{standalone}
\usetikzlibrary{shadows,matrix} % Shadows for nodes
\begin{document}
\begin{tikzpicture}
\tikzset{% This is the style settings for nodes
dep/.style={circle,minimum size=#1,fill=orange!20,draw=orange,
general shadow={fill=gray!60,shadow xshift=1pt,shadow yshift=-1pt}},
dep/.default=1cm,
cli/.style={circle,minimum size=#1,fill=white,draw,
general shadow={fill=gray!60,shadow xshift=1pt,shadow yshift=-1pt}},
cli/.default=1cm,
spl/.style={cli=#1,append after command={
node[circle,draw,dotted,
minimum size=1.5cm] at (\tikzlastnode.center) {}}},
spl/.default=1cm,
c1/.style={-stealth,very thick,red!80!black},
v2/.style={-stealth,very thick,yellow!65!black},
v4/.style={-stealth,very thick,purple!70!black}}
\begin{scope}[local bounding box=graph]
\node[dep] (0) at (0,0) {0};
\node[cli] (7) at (-1,-2) {7};
\node[spl] (8) at (2,-2) {8};
\draw[c1] (0) to[bend right] (7);
\draw[v2] (7) -- (8);
\draw[v4] (8) -- (2,-1) -- (0);
\end{scope}
\matrix[draw,anchor=north west,xshift=1ex,matrix of nodes,row sep=.5ex,
column 2/.style={text width=2.5cm,align=left,anchor=center}]
at (graph.north east) {
\draw[c1] (-.5,0) -- (.5,0); & $C_1$\\
\draw[v2] (-.5,0) -- (.5,0); & $V_2$\\
\draw[v4] (-.5,0) -- (.5,0); & $V_4$\\
\draw[dotted] circle (.25); & Split Delivery\\
\node[cli=.5cm] {}; & Cliente\\
\node[dep=.5cm] {}; & Deposito\\
};
\end{tikzpicture}
\end{document}
It seems that you haven't known much about TikZ yet, then this question on TeX.SE may be useful to you.

With knitr and LateX, how to avoid path above .png; and how to place two plots side-by-side in table

New to LateX, I have two problems with it or knitr. Running the code that follows the image of what is produced in PDF, there continue to appear three repetitions of the the path above the .png figure.
Second, the side-by-side table with a .png image in each is not appearing at all.
I would appreciate being corrected and educated.
\documentclass[11pt]{article}
\usepackage{fullpage}
\usepackage[margin=1in]{geometry}
\usepackage{color}
% \usepackage{framed}
\usepackage{graphicx} % to import .png files
\usepackage{tabularx} % note that there is no tabular package
\usepackage{fancyhdr}
\lhead{Survey Graphics}
\rhead{Pg. \thepage}
\cfoot{$\copyright$ lawyeR}
<<echo=FALSE, results="hide", warning=FALSE, message=FALSE>>=
lm_blue <- "#003767"
#
% \renewcommand
% \headrule{
% \color{lm_blue}
% \begin{minipage}{1\textwidth}
% \hrule width \hsize height 5pt
% \end{minipage}}%
%
% \newcommand{\HRule}[1]{\hfill \rule{0.2\linewidth}{#1}} % Horiz rule at bottom of page, adjust width here
%
% \renewcommand{\headrulewidth}{0.4pt}
% \renewcommand{\footrulewidth}{0.1pt}
% \addtolength{\headheight}{0.5pt}
%% end preamble; begin document
\begin{document}
\graphicspath{ {C:/Users/blank/Documents/Marketing and Blog/ReproData Book Survey/Plots and images/} }
\begin{figure}[h]
\begin{center}
\includegraphics[width = 90mm, height = 2in]{C:/Users/blank/Documents/Marketing and Blog/ReproData Book Survey/Plots and images/Above the Law 2013 comment in plot.PNG}
\end{center}
\end{figure}
% lay out figures side-by-side using table
\begin{table}[h]
\begin{tabular}{cc}
PNG One & PNG Two \\
\includegraphics[width = 20mm]{C:/Users/blank/Documents/Marketing and Blog/ReproData Book Survey/Plots and images/MLA Comp 2014 bars separated.PNG} &
\includegraphics[width = 20mm]{C:/Users/blank/Documents/Marketing and Blog/ReproData Book Survey/Plots and images/Winstonpg19noredundantdata2.PNG}
\end{tabular}
\end{table}
\end{document}
With spaces in your filenames, you need to add
\usepackage{grffile}
to your preamble. Additionally, once you've specified an appropriate set in \graphicspath{..}, you can remove all such path references in the \includegraphics commands.

How can I include a .eps figure within a Tikz simple flow chart?

I would like to create a simple flow chart in latex with the TikZ package similar to the following example
http://www.texample.net/tikz/examples/simple-flow-chart/
However I would like to include figures (a time series plot created in R, as eps or something else) within the flowchart (e.g. for example within a {block}?
\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\begin{document}
\pagestyle{empty}
% Define block styles
\tikzstyle{decision} = [diamond, draw, fill=blue!20,
text width=4.5em, text badly centered, node distance=3cm, inner sep=0pt]
\tikzstyle{block} = [rectangle, draw, fill=blue!20,
text width=5em, text centered, rounded corners, minimum height=4em]
\tikzstyle{line} = [draw, -latex']
\tikzstyle{cloud} = [draw, ellipse,fill=red!20, node distance=3cm,
minimum height=2em]
\begin{tikzpicture}[node distance = 2cm, auto]
% Place nodes
\node [block] (init) {initialize model};
\node [cloud, left of=init] (expert) {expert};
\node [cloud, right of=init] (system) {system};
\node [block, below of=init] (identify) {identify candidate models};
\node [block, below of=identify] (evaluate) {evaluate candidate models};
\node [block, left of=evaluate, node distance=3cm] (update) {update model};
\node [decision, below of=evaluate] (decide) {is best candidate better?};
\node [block, below of=decide, node distance=3cm] (stop) {stop};
% Draw edges
\path [line] (init) -- (identify);
\path [line] (identify) -- (evaluate);
\path [line] (evaluate) -- (decide);
\path [line] (decide) -| node [near start] {yes} (update);
\path [line] (update) |- (identify);
\path [line] (decide) -- node {no}(stop);
\path [line,dashed] (expert) -- (init);
\path [line,dashed] (system) -- (init);
\path [line,dashed] (system) |- (evaluate);
\end{tikzpicture}
\end{document}
Thanks,
Jan
As another example shows, it is possible to use \includegraphics in a tikzpicture environment. Have you tried this?
And of course there is the obligatory mentioning of pgfplots. Time series or other data visualization can be performed using this package instead of including graphics :).

Resources