From ffb47975500673710c6f62e39c94695f7602e716 Mon Sep 17 00:00:00 2001
From: Christian Krause <christian.krause@idiv.de>
Date: Mon, 3 Jun 2019 14:27:37 +0200
Subject: [PATCH] adds plots with R

- new image sub-directory layout
- updates README instructions
---
 .gitignore                              |  3 ++-
 .spelling                               |  4 +++
 README.md                               | 35 ++++++++++++++++++-------
 chapter-3-results.md                    |  2 +-
 chapter-4-discussion.md                 |  9 ++++++-
 img/Makefile                            | 24 ++++++++++-------
 img/graph/Makefile                      | 20 ++++++++++++++
 img/{ => graph}/git-remote-commands.dot |  0
 img/plot/Makefile                       | 20 ++++++++++++++
 img/plot/results.R                      |  9 +++++++
 img/plot/results.csv                    |  6 +++++
 results.csv                             |  3 ---
 12 files changed, 109 insertions(+), 26 deletions(-)
 create mode 100644 img/graph/Makefile
 rename img/{ => graph}/git-remote-commands.dot (100%)
 create mode 100644 img/plot/Makefile
 create mode 100644 img/plot/results.R
 create mode 100644 img/plot/results.csv
 delete mode 100644 results.csv

diff --git a/.gitignore b/.gitignore
index ff2f188..51dd931 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
 *.pdf
-img/*.svg
+img/graph/*.svg
+img/plot/*.svg
diff --git a/.spelling b/.spelling
index 514f348..ed0aad0 100644
--- a/.spelling
+++ b/.spelling
@@ -1,2 +1,6 @@
+csvautobooktabular
+Graphviz
+img
 pandoc
+results.csv
 WatsonCrick1953
diff --git a/README.md b/README.md
index 8761bec..bf6ad05 100644
--- a/README.md
+++ b/README.md
@@ -4,22 +4,37 @@ Change to Document Title Here
 Building
 --------
 
-Build the PDF:
+To build the PDF, just type `make`. Make already does incremental compilation,
+i.e. it re-compiles only those targets whose sources changed. If you want to
+watch for changes and automatically run `make` when something changes, use
+[watchexec][]:
 
 ```
-make title.pdf
+watchexec make
 ```
 
+The `watchexec` tool honors `.gitignore`.
+
 ### Requirements
 
-Required:
+These requirements are all more or less optional or replaceable. If you want to
+build the template PDF, you need these dependencies:
+
+- [git][]
+- [pandoc and pandoc-citeproc][pandoc]
+- LaTeX, either through **texlive** or **MiKTeX**
+- GNU **make**
+- [graphviz][]
+- R including the packages **data.table**, **tidyverse** and **svglite**
+- [watchexec][]
+- [markdown-spellcheck][]
 
-- [git](https://git-scm.com/downloads)
-- [pandoc, pandoc-citeproc](https://pandoc.org/installing.html)
-- LaTeX, either texlive or MiKTeX
-- make
+**Note:** On MS Windows, it might be helpful using [chocolatey][].
 
-Optional:
 
-- [graphviz](https://graphviz.gitlab.io/download/)
-- [watchexec](https://github.com/watchexec/watchexec/releases)
+[chocolatey]: https://chocolatey.org/
+[git]: https://git-scm.com/downloads
+[graphviz]: https://graphviz.gitlab.io/download/
+[markdown-spellcheck]: https://www.npmjs.com/package/markdown-spellcheck
+[pandoc]: https://pandoc.org/installing.html
+[watchexec]: https://github.com/watchexec/watchexec/releases
diff --git a/chapter-3-results.md b/chapter-3-results.md
index b1efb97..734a639 100644
--- a/chapter-3-results.md
+++ b/chapter-3-results.md
@@ -14,6 +14,6 @@ You can automatically insert tables from your results if you store them in CSV.
 
 \begin{table}
   \centering
-  \csvautobooktabular{results.csv}
+  \csvautobooktabular{img/plot/results.csv}
   \caption{These are my super significant results.}
 \end{table}
diff --git a/chapter-4-discussion.md b/chapter-4-discussion.md
index 2e06c71..ae3a79e 100644
--- a/chapter-4-discussion.md
+++ b/chapter-4-discussion.md
@@ -1,5 +1,12 @@
 Discussion
 ==========
 
+You can add flow charts produced with Graphviz as in figure \ref{flowchart}.
+Note how you inline the \LaTeX\ references.
+
 ![This is the image caption. This image shows how git remotes
-work.](img/git-remote-commands.svg)
+work.\label{flowchart}](img/graph/git-remote-commands.svg)
+
+See these awesome, auto-generated plot in figure \ref{plot}.
+
+![The plot from the results data.\label{plot}](img/plot/results.svg)
diff --git a/img/Makefile b/img/Makefile
index dc8bfbf..27fce70 100644
--- a/img/Makefile
+++ b/img/Makefile
@@ -1,20 +1,24 @@
-SOURCES_DOT = $(wildcard *.dot)
+default: all
 
-OBJECTS_DOT_SVG = $(SOURCES_DOT:.dot=.svg)
+all: subdirs
 
-OBJECTS = \
-	$(OBJECTS_DOT_SVG) \
+SUBDIRS = \
+	graph \
+	plot \
 
-all: $(OBJECTS)
+$(SUBDIRS):
+	$(MAKE) -C $@
 
-$(OBJECTS):
-
-%.svg: %.dot
-	dot -Tsvg -o $@ $<
+subdirs: $(SUBDIRS)
 
 clean:
-	rm -f $(OBJECTS)
+	@for dir in $(SUBDIRS); do \
+	  $(MAKE) -C $$dir $@; \
+	done
 
 .PHONY: \
 	all \
 	clean \
+	default \
+	subdirs \
+	$(SUBDIRS) \
diff --git a/img/graph/Makefile b/img/graph/Makefile
new file mode 100644
index 0000000..dc8bfbf
--- /dev/null
+++ b/img/graph/Makefile
@@ -0,0 +1,20 @@
+SOURCES_DOT = $(wildcard *.dot)
+
+OBJECTS_DOT_SVG = $(SOURCES_DOT:.dot=.svg)
+
+OBJECTS = \
+	$(OBJECTS_DOT_SVG) \
+
+all: $(OBJECTS)
+
+$(OBJECTS):
+
+%.svg: %.dot
+	dot -Tsvg -o $@ $<
+
+clean:
+	rm -f $(OBJECTS)
+
+.PHONY: \
+	all \
+	clean \
diff --git a/img/git-remote-commands.dot b/img/graph/git-remote-commands.dot
similarity index 100%
rename from img/git-remote-commands.dot
rename to img/graph/git-remote-commands.dot
diff --git a/img/plot/Makefile b/img/plot/Makefile
new file mode 100644
index 0000000..12005f2
--- /dev/null
+++ b/img/plot/Makefile
@@ -0,0 +1,20 @@
+SOURCES_CSV = $(wildcard *.csv)
+
+OBJECTS_CSV_SVG = $(SOURCES_CSV:.csv=.svg)
+
+OBJECTS = \
+	$(OBJECTS_CSV_SVG) \
+
+all: $(OBJECTS)
+
+$(OBJECTS):
+
+%.svg: %.csv
+	Rscript --vanilla $*.R
+
+clean:
+	rm -f $(OBJECTS)
+
+.PHONY: \
+	all \
+	clean \
diff --git a/img/plot/results.R b/img/plot/results.R
new file mode 100644
index 0000000..14acca4
--- /dev/null
+++ b/img/plot/results.R
@@ -0,0 +1,9 @@
+library(data.table)
+library(ggplot2)
+
+data <- fread("results.csv")
+
+plot <- ggplot(data, aes(x, y)) +
+    geom_point(aes(col = cat))
+
+ggsave("results.svg", plot)
diff --git a/img/plot/results.csv b/img/plot/results.csv
new file mode 100644
index 0000000..6a9061c
--- /dev/null
+++ b/img/plot/results.csv
@@ -0,0 +1,6 @@
+cat,x,y,foo
+a,1,2,8
+b,6,3,7
+c,7,4,5
+d,2,5,1
+e,1,8,7
diff --git a/results.csv b/results.csv
deleted file mode 100644
index 88700c7..0000000
--- a/results.csv
+++ /dev/null
@@ -1,3 +0,0 @@
-a,b,c
-1,2,3
-4,5,6
-- 
GitLab