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:
Related
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}
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}
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.
Long time reader, first time poster.
I am attempting to write mechner notation using DiagrammeR (through R Studio; everything is up to date; MacOS). Long term intention is to embed the code within a latex doc, but I'm taking this one step at a time. Currently, I cannot find the code necessary to force a subscript and superscript to be in-line with each other. So far, I have found this:
library(DiagrammeR)
grViz("
digraph dot {
graph [layout = dot, rankdir = LR]
node [fontname = Helvetica, shape = plaintext]
a [label = 'aA']
b [label = 'bA#_{2}#^{p}']
c [label = 'abC']
a -> b -> c
}")
Produces this.
But I need this.
I suppose the real question is how to properly format within those pesky quotations. I have yet to find a walkthrough on how to properly format (e.g., bold, italics, superscript) within the label field quotations. Suggestions?
I figured this out and forgot to post an answer! Here is the code that worked for me (Mac OSx; R Studio Sweave File, w/ pdfLaTeX enabled):
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\newcommand{\1}{\textbf} %to save space
\begin{document}
\SweaveOpts{concordance=TRUE}
%example 1
\par{\centering
\begin{tikzpicture}
\matrix (m) [matrix of math nodes, nodes in empty cells, row
sep=1em,column sep=2em,minimum width=4em,nodes={anchor=center}]
{
a\1{A} & b\1{A}_{2}^{p} & ab\1{C} & \\
};
\draw [->] (m-1-1) -- (m-1-2);
\draw [->] (m-1-2) -- (m-1-3);
\end{tikzpicture}
\par}
%example 2
\par{\centering
\begin{tikzpicture}
\matrix (m) [matrix of math nodes, nodes in empty cells, row
sep=1em,column sep=2em,minimum width=4em,nodes={anchor=center}]
{
t\1{S}_{A}^{D} & s\1{R}_{CR} & t\1{S}_{CR} & \\
};
\draw [->] (m-1-1) -- (m-1-2);
\draw [->] (m-1-2) -- (m-1-3);
\end{tikzpicture}
\par}
\end{document}
Here is the output
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 :).