diff --git a/appendixes.prj b/appendixes.prj index 7f89936f..8f6c1509 100644 --- a/appendixes.prj +++ b/appendixes.prj @@ -13,7 +13,7 @@ TeX:RNW:UTF-8 2 0 189 20 189 25 6074 -1 6522 208 1 1 458 435 1 906 254 255 -1 0 0 33 1 0 25 189 0 -1 0 learn-r-2ed-crc.tex TeX -403713275 0 -1 777601 -1 777604 152 152 1581 1098 1 1 1186 -1508 -1 -1 0 0 49 -1 -1 49 1 0 777604 -1 0 -1 0 +403713275 0 -1 777601 -1 777604 152 152 1581 1098 1 1 1186 667 -1 -1 0 0 49 -1 -1 49 1 0 777604 -1 0 -1 0 R.plotting.Rnw TeX:RNW:UNIX 17936379 0 -1 3770 -1 3775 380 380 1366 1438 1 1 445 957 -1 -1 0 0 115133 -1 -1 115133 1 0 3775 -1 0 -1 0 diff --git a/chapter-3--learn-R-crc-2ed.pdf b/chapter-3--learn-R-crc-2ed.pdf deleted file mode 100644 index 98b4afa6..00000000 Binary files a/chapter-3--learn-R-crc-2ed.pdf and /dev/null differ diff --git a/figure.old/pos-annotate-01-1.pdf b/figure.old/pos-annotate-01-1.pdf deleted file mode 100644 index 65103d43..00000000 Binary files a/figure.old/pos-annotate-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-annotate-03-1.pdf b/figure.old/pos-annotate-03-1.pdf deleted file mode 100644 index b860297b..00000000 Binary files a/figure.old/pos-annotate-03-1.pdf and /dev/null differ diff --git a/figure.old/pos-area-plot-01-1.pdf b/figure.old/pos-area-plot-01-1.pdf deleted file mode 100644 index f4acb8c5..00000000 Binary files a/figure.old/pos-area-plot-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-area-plot-01-2.pdf b/figure.old/pos-area-plot-01-2.pdf deleted file mode 100644 index 38c98382..00000000 Binary files a/figure.old/pos-area-plot-01-2.pdf and /dev/null differ diff --git a/figure.old/pos-area-plot-02-1.pdf b/figure.old/pos-area-plot-02-1.pdf deleted file mode 100644 index ff726878..00000000 Binary files a/figure.old/pos-area-plot-02-1.pdf and /dev/null differ diff --git a/figure.old/pos-axis-labels-01-1.pdf b/figure.old/pos-axis-labels-01-1.pdf deleted file mode 100644 index ac350e50..00000000 Binary files a/figure.old/pos-axis-labels-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-axis-labels-02-1.pdf b/figure.old/pos-axis-labels-02-1.pdf deleted file mode 100644 index 382b5f62..00000000 Binary files a/figure.old/pos-axis-labels-02-1.pdf and /dev/null differ diff --git a/figure.old/pos-axis-labels-03-1.pdf b/figure.old/pos-axis-labels-03-1.pdf deleted file mode 100644 index 4c741887..00000000 Binary files a/figure.old/pos-axis-labels-03-1.pdf and /dev/null differ diff --git a/figure.old/pos-axis-position-01-1.pdf b/figure.old/pos-axis-position-01-1.pdf deleted file mode 100644 index 9e71a3a7..00000000 Binary files a/figure.old/pos-axis-position-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-axis-secondary-01-1.pdf b/figure.old/pos-axis-secondary-01-1.pdf deleted file mode 100644 index 90da9244..00000000 Binary files a/figure.old/pos-axis-secondary-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-axis-secondary-02-1.pdf b/figure.old/pos-axis-secondary-02-1.pdf deleted file mode 100644 index 578f3a56..00000000 Binary files a/figure.old/pos-axis-secondary-02-1.pdf and /dev/null differ diff --git a/figure.old/pos-bench-diff-01-1.pdf b/figure.old/pos-bench-diff-01-1.pdf deleted file mode 100644 index d7672804..00000000 Binary files a/figure.old/pos-bench-diff-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-bench-diff-01__opts_fig_very_wide_square-1.pdf b/figure.old/pos-bench-diff-01__opts_fig_very_wide_square-1.pdf deleted file mode 100644 index cc4626c6..00000000 Binary files a/figure.old/pos-bench-diff-01__opts_fig_very_wide_square-1.pdf and /dev/null differ diff --git a/figure.old/pos-bench-rowsums-01-1.pdf b/figure.old/pos-bench-rowsums-01-1.pdf deleted file mode 100644 index 2b271df6..00000000 Binary files a/figure.old/pos-bench-rowsums-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-bin2d-plot-01-1.pdf b/figure.old/pos-bin2d-plot-01-1.pdf deleted file mode 100644 index e3c85dea..00000000 Binary files a/figure.old/pos-bin2d-plot-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-binned-scales-01-1.pdf b/figure.old/pos-binned-scales-01-1.pdf deleted file mode 100644 index 5637febf..00000000 Binary files a/figure.old/pos-binned-scales-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-bw-plot-01-1.pdf b/figure.old/pos-bw-plot-01-1.pdf deleted file mode 100644 index b1017a63..00000000 Binary files a/figure.old/pos-bw-plot-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-bw-plot-02-1.pdf b/figure.old/pos-bw-plot-02-1.pdf deleted file mode 100644 index 5c00038a..00000000 Binary files a/figure.old/pos-bw-plot-02-1.pdf and /dev/null differ diff --git a/figure.old/pos-bw-plot-03-1.pdf b/figure.old/pos-bw-plot-03-1.pdf deleted file mode 100644 index da85da65..00000000 Binary files a/figure.old/pos-bw-plot-03-1.pdf and /dev/null differ diff --git a/figure.old/pos-cluster-02-1.pdf b/figure.old/pos-cluster-02-1.pdf deleted file mode 100644 index 860c697e..00000000 Binary files a/figure.old/pos-cluster-02-1.pdf and /dev/null differ diff --git a/figure.old/pos-col-plot-02-1.pdf b/figure.old/pos-col-plot-02-1.pdf deleted file mode 100644 index 683becc6..00000000 Binary files a/figure.old/pos-col-plot-02-1.pdf and /dev/null differ diff --git a/figure.old/pos-col-plot-03-1.pdf b/figure.old/pos-col-plot-03-1.pdf deleted file mode 100644 index a5016ff6..00000000 Binary files a/figure.old/pos-col-plot-03-1.pdf and /dev/null differ diff --git a/figure.old/pos-col-plot-04-1.pdf b/figure.old/pos-col-plot-04-1.pdf deleted file mode 100644 index 3704ea30..00000000 Binary files a/figure.old/pos-col-plot-04-1.pdf and /dev/null differ diff --git a/figure.old/pos-col-plot-04a-1.pdf b/figure.old/pos-col-plot-04a-1.pdf deleted file mode 100644 index cf7b79ef..00000000 Binary files a/figure.old/pos-col-plot-04a-1.pdf and /dev/null differ diff --git a/figure.old/pos-cor-00-1.pdf b/figure.old/pos-cor-00-1.pdf deleted file mode 100644 index 8f585b03..00000000 Binary files a/figure.old/pos-cor-00-1.pdf and /dev/null differ diff --git a/figure.old/pos-density-01-1.pdf b/figure.old/pos-density-01-1.pdf deleted file mode 100644 index e0950638..00000000 Binary files a/figure.old/pos-density-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-density-plot-01-1.pdf b/figure.old/pos-density-plot-01-1.pdf deleted file mode 100644 index ccbd9a5a..00000000 Binary files a/figure.old/pos-density-plot-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-density-plot-03-1.pdf b/figure.old/pos-density-plot-03-1.pdf deleted file mode 100644 index 8fa24b4e..00000000 Binary files a/figure.old/pos-density-plot-03-1.pdf and /dev/null differ diff --git a/figure.old/pos-density-plot-10-1.pdf b/figure.old/pos-density-plot-10-1.pdf deleted file mode 100644 index f35aefc9..00000000 Binary files a/figure.old/pos-density-plot-10-1.pdf and /dev/null differ diff --git a/figure.old/pos-density-plot-12-1.pdf b/figure.old/pos-density-plot-12-1.pdf deleted file mode 100644 index 9c62a8a6..00000000 Binary files a/figure.old/pos-density-plot-12-1.pdf and /dev/null differ diff --git a/figure.old/pos-desnity-04-1.pdf b/figure.old/pos-desnity-04-1.pdf deleted file mode 100644 index 0835de48..00000000 Binary files a/figure.old/pos-desnity-04-1.pdf and /dev/null differ diff --git a/figure.old/pos-distrib-01a-1.pdf b/figure.old/pos-distrib-01a-1.pdf deleted file mode 100644 index bf10678c..00000000 Binary files a/figure.old/pos-distrib-01a-1.pdf and /dev/null differ diff --git a/figure.old/pos-distrib-01b-1.pdf b/figure.old/pos-distrib-01b-1.pdf deleted file mode 100644 index 47bd4c38..00000000 Binary files a/figure.old/pos-distrib-01b-1.pdf and /dev/null differ diff --git a/figure.old/pos-expr-bquote-01-1.pdf b/figure.old/pos-expr-bquote-01-1.pdf deleted file mode 100644 index 7c94413d..00000000 Binary files a/figure.old/pos-expr-bquote-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-expr-parse-box-01-1.pdf b/figure.old/pos-expr-parse-box-01-1.pdf deleted file mode 100644 index 9070ba23..00000000 Binary files a/figure.old/pos-expr-parse-box-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-expr-parse-box-02-1.pdf b/figure.old/pos-expr-parse-box-02-1.pdf deleted file mode 100644 index 9070ba23..00000000 Binary files a/figure.old/pos-expr-parse-box-02-1.pdf and /dev/null differ diff --git a/figure.old/pos-expr-parse-box-03-1.pdf b/figure.old/pos-expr-parse-box-03-1.pdf deleted file mode 100644 index 27088da0..00000000 Binary files a/figure.old/pos-expr-parse-box-03-1.pdf and /dev/null differ diff --git a/figure.old/pos-expr-parse-box-06-1.pdf b/figure.old/pos-expr-parse-box-06-1.pdf deleted file mode 100644 index 5e56b087..00000000 Binary files a/figure.old/pos-expr-parse-box-06-1.pdf and /dev/null differ diff --git a/figure.old/pos-expr-parse-box-07-1.pdf b/figure.old/pos-expr-parse-box-07-1.pdf deleted file mode 100644 index 842480f8..00000000 Binary files a/figure.old/pos-expr-parse-box-07-1.pdf and /dev/null differ diff --git a/figure.old/pos-expr-parse-box-08-1.pdf b/figure.old/pos-expr-parse-box-08-1.pdf deleted file mode 100644 index e8daff31..00000000 Binary files a/figure.old/pos-expr-parse-box-08-1.pdf and /dev/null differ diff --git a/figure.old/pos-expr-parse-box-09-1.pdf b/figure.old/pos-expr-parse-box-09-1.pdf deleted file mode 100644 index 34f8eeca..00000000 Binary files a/figure.old/pos-expr-parse-box-09-1.pdf and /dev/null differ diff --git a/figure.old/pos-expr-substitute-01-1.pdf b/figure.old/pos-expr-substitute-01-1.pdf deleted file mode 100644 index 7c94413d..00000000 Binary files a/figure.old/pos-expr-substitute-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-facets-00-1.pdf b/figure.old/pos-facets-00-1.pdf deleted file mode 100644 index 33b5a30a..00000000 Binary files a/figure.old/pos-facets-00-1.pdf and /dev/null differ diff --git a/figure.old/pos-facets-01-1.pdf b/figure.old/pos-facets-01-1.pdf deleted file mode 100644 index 5d25989d..00000000 Binary files a/figure.old/pos-facets-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-facets-01a-1.pdf b/figure.old/pos-facets-01a-1.pdf deleted file mode 100644 index 3622886d..00000000 Binary files a/figure.old/pos-facets-01a-1.pdf and /dev/null differ diff --git a/figure.old/pos-facets-02a-1.pdf b/figure.old/pos-facets-02a-1.pdf deleted file mode 100644 index fc5e8587..00000000 Binary files a/figure.old/pos-facets-02a-1.pdf and /dev/null differ diff --git a/figure.old/pos-facets-02b-1.pdf b/figure.old/pos-facets-02b-1.pdf deleted file mode 100644 index 96a1faf0..00000000 Binary files a/figure.old/pos-facets-02b-1.pdf and /dev/null differ diff --git a/figure.old/pos-facets-05-1.pdf b/figure.old/pos-facets-05-1.pdf deleted file mode 100644 index ea94475a..00000000 Binary files a/figure.old/pos-facets-05-1.pdf and /dev/null differ diff --git a/figure.old/pos-facets-06-1.pdf b/figure.old/pos-facets-06-1.pdf deleted file mode 100644 index 68f576aa..00000000 Binary files a/figure.old/pos-facets-06-1.pdf and /dev/null differ diff --git a/figure.old/pos-facets-07-1.pdf b/figure.old/pos-facets-07-1.pdf deleted file mode 100644 index 31768471..00000000 Binary files a/figure.old/pos-facets-07-1.pdf and /dev/null differ diff --git a/figure.old/pos-facets-11-1.pdf b/figure.old/pos-facets-11-1.pdf deleted file mode 100644 index d955326a..00000000 Binary files a/figure.old/pos-facets-11-1.pdf and /dev/null differ diff --git a/figure.old/pos-facets-12-1.pdf b/figure.old/pos-facets-12-1.pdf deleted file mode 100644 index 54b63142..00000000 Binary files a/figure.old/pos-facets-12-1.pdf and /dev/null differ diff --git a/figure.old/pos-facets-13-1.pdf b/figure.old/pos-facets-13-1.pdf deleted file mode 100644 index d103150c..00000000 Binary files a/figure.old/pos-facets-13-1.pdf and /dev/null differ diff --git a/figure.old/pos-flipping-01-02-ggplot-1.pdf b/figure.old/pos-flipping-01-02-ggplot-1.pdf deleted file mode 100644 index 18a7c4f8..00000000 Binary files a/figure.old/pos-flipping-01-02-ggplot-1.pdf and /dev/null differ diff --git a/figure.old/pos-flipping-01-ggplot-1.pdf b/figure.old/pos-flipping-01-ggplot-1.pdf deleted file mode 100644 index 04847bc3..00000000 Binary files a/figure.old/pos-flipping-01-ggplot-1.pdf and /dev/null differ diff --git a/figure.old/pos-flipping-02-ggplot-1.pdf b/figure.old/pos-flipping-02-ggplot-1.pdf deleted file mode 100644 index b4706068..00000000 Binary files a/figure.old/pos-flipping-02-ggplot-1.pdf and /dev/null differ diff --git a/figure.old/pos-flipping-03-04-ggplot-1.pdf b/figure.old/pos-flipping-03-04-ggplot-1.pdf deleted file mode 100644 index 650b90c2..00000000 Binary files a/figure.old/pos-flipping-03-04-ggplot-1.pdf and /dev/null differ diff --git a/figure.old/pos-flipping-03-ggplot-1.pdf b/figure.old/pos-flipping-03-ggplot-1.pdf deleted file mode 100644 index c201951f..00000000 Binary files a/figure.old/pos-flipping-03-ggplot-1.pdf and /dev/null differ diff --git a/figure.old/pos-flipping-04-ggplot-1.pdf b/figure.old/pos-flipping-04-ggplot-1.pdf deleted file mode 100644 index 0effe543..00000000 Binary files a/figure.old/pos-flipping-04-ggplot-1.pdf and /dev/null differ diff --git a/figure.old/pos-flipping-05-ggplot-1.pdf b/figure.old/pos-flipping-05-ggplot-1.pdf deleted file mode 100644 index 526f500d..00000000 Binary files a/figure.old/pos-flipping-05-ggplot-1.pdf and /dev/null differ diff --git a/figure.old/pos-flipping-06-ggplot-1.pdf b/figure.old/pos-flipping-06-ggplot-1.pdf deleted file mode 100644 index d4a24721..00000000 Binary files a/figure.old/pos-flipping-06-ggplot-1.pdf and /dev/null differ diff --git a/figure.old/pos-flipping-06a-ggplot-1.pdf b/figure.old/pos-flipping-06a-ggplot-1.pdf deleted file mode 100644 index 4c97e04a..00000000 Binary files a/figure.old/pos-flipping-06a-ggplot-1.pdf and /dev/null differ diff --git a/figure.old/pos-flipping-07-ggpmisc-1.pdf b/figure.old/pos-flipping-07-ggpmisc-1.pdf deleted file mode 100644 index edccff42..00000000 Binary files a/figure.old/pos-flipping-07-ggpmisc-1.pdf and /dev/null differ diff --git a/figure.old/pos-flipping-08-ggpmisc-1.pdf b/figure.old/pos-flipping-08-ggpmisc-1.pdf deleted file mode 100644 index 70fc058c..00000000 Binary files a/figure.old/pos-flipping-08-ggpmisc-1.pdf and /dev/null differ diff --git a/figure.old/pos-flipping-09-ggpmisc-1.pdf b/figure.old/pos-flipping-09-ggpmisc-1.pdf deleted file mode 100644 index 2588cb9e..00000000 Binary files a/figure.old/pos-flipping-09-ggpmisc-1.pdf and /dev/null differ diff --git a/figure.old/pos-flipping-10-ggpmisc-1.pdf b/figure.old/pos-flipping-10-ggpmisc-1.pdf deleted file mode 100644 index c86759d4..00000000 Binary files a/figure.old/pos-flipping-10-ggpmisc-1.pdf and /dev/null differ diff --git a/figure.old/pos-flipping_box-01-ggplot-1.pdf b/figure.old/pos-flipping_box-01-ggplot-1.pdf deleted file mode 100644 index 49a5dbb8..00000000 Binary files a/figure.old/pos-flipping_box-01-ggplot-1.pdf and /dev/null differ diff --git a/figure.old/pos-flipping_box-01-ggplot-2.pdf b/figure.old/pos-flipping_box-01-ggplot-2.pdf deleted file mode 100644 index 2acb45ae..00000000 Binary files a/figure.old/pos-flipping_box-01-ggplot-2.pdf and /dev/null differ diff --git a/figure.old/pos-flipping_box-01a-ggplot-1.pdf b/figure.old/pos-flipping_box-01a-ggplot-1.pdf deleted file mode 100644 index 7a368179..00000000 Binary files a/figure.old/pos-flipping_box-01a-ggplot-1.pdf and /dev/null differ diff --git a/figure.old/pos-flipping_box-01b-ggplot-1.pdf b/figure.old/pos-flipping_box-01b-ggplot-1.pdf deleted file mode 100644 index 3da66b4f..00000000 Binary files a/figure.old/pos-flipping_box-01b-ggplot-1.pdf and /dev/null differ diff --git a/figure.old/pos-flipping_box-01c-ggplot-1.pdf b/figure.old/pos-flipping_box-01c-ggplot-1.pdf deleted file mode 100644 index 7b0095eb..00000000 Binary files a/figure.old/pos-flipping_box-01c-ggplot-1.pdf and /dev/null differ diff --git a/figure.old/pos-function-plot-01-1.pdf b/figure.old/pos-function-plot-01-1.pdf deleted file mode 100644 index f516a929..00000000 Binary files a/figure.old/pos-function-plot-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-function-plot-02-1.pdf b/figure.old/pos-function-plot-02-1.pdf deleted file mode 100644 index bae3c65d..00000000 Binary files a/figure.old/pos-function-plot-02-1.pdf and /dev/null differ diff --git a/figure.old/pos-function-plot-03-1.pdf b/figure.old/pos-function-plot-03-1.pdf deleted file mode 100644 index 26d26827..00000000 Binary files a/figure.old/pos-function-plot-03-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggbeeswarm-plot-01-1.pdf b/figure.old/pos-ggbeeswarm-plot-01-1.pdf deleted file mode 100644 index 395026af..00000000 Binary files a/figure.old/pos-ggbeeswarm-plot-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggbeeswarm-plot-012-1.pdf b/figure.old/pos-ggbeeswarm-plot-012-1.pdf deleted file mode 100644 index 8410e838..00000000 Binary files a/figure.old/pos-ggbeeswarm-plot-012-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggplot-basics-01-1.pdf b/figure.old/pos-ggplot-basics-01-1.pdf deleted file mode 100644 index fbc22f4b..00000000 Binary files a/figure.old/pos-ggplot-basics-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggplot-basics-02-1.pdf b/figure.old/pos-ggplot-basics-02-1.pdf deleted file mode 100644 index be1945d2..00000000 Binary files a/figure.old/pos-ggplot-basics-02-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggplot-basics-03-1.pdf b/figure.old/pos-ggplot-basics-03-1.pdf deleted file mode 100644 index 609fd6f5..00000000 Binary files a/figure.old/pos-ggplot-basics-03-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggplot-basics-04-1.pdf b/figure.old/pos-ggplot-basics-04-1.pdf deleted file mode 100644 index f6294ab8..00000000 Binary files a/figure.old/pos-ggplot-basics-04-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggplot-basics-04-wb2-1.pdf b/figure.old/pos-ggplot-basics-04-wb2-1.pdf deleted file mode 100644 index a767538e..00000000 Binary files a/figure.old/pos-ggplot-basics-04-wb2-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggplot-basics-04a-1.pdf b/figure.old/pos-ggplot-basics-04a-1.pdf deleted file mode 100644 index 0d165480..00000000 Binary files a/figure.old/pos-ggplot-basics-04a-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggplot-basics-04b-1.pdf b/figure.old/pos-ggplot-basics-04b-1.pdf deleted file mode 100644 index f4f14487..00000000 Binary files a/figure.old/pos-ggplot-basics-04b-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggplot-basics-05-1.pdf b/figure.old/pos-ggplot-basics-05-1.pdf deleted file mode 100644 index d0eb2019..00000000 Binary files a/figure.old/pos-ggplot-basics-05-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggplot-basics-06-1.pdf b/figure.old/pos-ggplot-basics-06-1.pdf deleted file mode 100644 index 8939fd9c..00000000 Binary files a/figure.old/pos-ggplot-basics-06-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggplot-basics-07-1.pdf b/figure.old/pos-ggplot-basics-07-1.pdf deleted file mode 100644 index 7ad61eaa..00000000 Binary files a/figure.old/pos-ggplot-basics-07-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggplot-basics-08-1.pdf b/figure.old/pos-ggplot-basics-08-1.pdf deleted file mode 100644 index 0f976f78..00000000 Binary files a/figure.old/pos-ggplot-basics-08-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggplot-basics-09-1.pdf b/figure.old/pos-ggplot-basics-09-1.pdf deleted file mode 100644 index e5c765d1..00000000 Binary files a/figure.old/pos-ggplot-basics-09-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggplot-basics-10-1.pdf b/figure.old/pos-ggplot-basics-10-1.pdf deleted file mode 100644 index 6ac9a6d4..00000000 Binary files a/figure.old/pos-ggplot-basics-10-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggplot-basics-11-1.pdf b/figure.old/pos-ggplot-basics-11-1.pdf deleted file mode 100644 index 0cc36d8e..00000000 Binary files a/figure.old/pos-ggplot-basics-11-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggplot-basics-12a-1.pdf b/figure.old/pos-ggplot-basics-12a-1.pdf deleted file mode 100644 index 4653db53..00000000 Binary files a/figure.old/pos-ggplot-basics-12a-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggplot-basics-12b-1.pdf b/figure.old/pos-ggplot-basics-12b-1.pdf deleted file mode 100644 index 3004dab3..00000000 Binary files a/figure.old/pos-ggplot-basics-12b-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggplot-basics-12c-1.pdf b/figure.old/pos-ggplot-basics-12c-1.pdf deleted file mode 100644 index a558915b..00000000 Binary files a/figure.old/pos-ggplot-basics-12c-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggplot-basics-12d-1.pdf b/figure.old/pos-ggplot-basics-12d-1.pdf deleted file mode 100644 index 5fd3bbdc..00000000 Binary files a/figure.old/pos-ggplot-basics-12d-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggplot-basics-13-1.pdf b/figure.old/pos-ggplot-basics-13-1.pdf deleted file mode 100644 index c6c628c2..00000000 Binary files a/figure.old/pos-ggplot-basics-13-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggplot-basics-14-1.pdf b/figure.old/pos-ggplot-basics-14-1.pdf deleted file mode 100644 index 820e79d6..00000000 Binary files a/figure.old/pos-ggplot-basics-14-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggplot-basics-15-1.pdf b/figure.old/pos-ggplot-basics-15-1.pdf deleted file mode 100644 index 820e79d6..00000000 Binary files a/figure.old/pos-ggplot-basics-15-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggplot-basics-15a-1.pdf b/figure.old/pos-ggplot-basics-15a-1.pdf deleted file mode 100644 index 60989a11..00000000 Binary files a/figure.old/pos-ggplot-basics-15a-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggplot-basics-15b-1.pdf b/figure.old/pos-ggplot-basics-15b-1.pdf deleted file mode 100644 index 56089237..00000000 Binary files a/figure.old/pos-ggplot-basics-15b-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggplot-basics-16-1.pdf b/figure.old/pos-ggplot-basics-16-1.pdf deleted file mode 100644 index 3be8e66e..00000000 Binary files a/figure.old/pos-ggplot-basics-16-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggplot-basics-17-1.pdf b/figure.old/pos-ggplot-basics-17-1.pdf deleted file mode 100644 index 18ecfe1b..00000000 Binary files a/figure.old/pos-ggplot-basics-17-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggplot-basics-18-1.pdf b/figure.old/pos-ggplot-basics-18-1.pdf deleted file mode 100644 index 18ecfe1b..00000000 Binary files a/figure.old/pos-ggplot-basics-18-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggplot-basics-info-01-1.pdf b/figure.old/pos-ggplot-basics-info-01-1.pdf deleted file mode 100644 index daad103e..00000000 Binary files a/figure.old/pos-ggplot-basics-info-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggplot-debug-01-1.pdf b/figure.old/pos-ggplot-debug-01-1.pdf deleted file mode 100644 index 17460404..00000000 Binary files a/figure.old/pos-ggplot-debug-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggplot-debug-02-1.pdf b/figure.old/pos-ggplot-debug-02-1.pdf deleted file mode 100644 index 11ad59d6..00000000 Binary files a/figure.old/pos-ggplot-debug-02-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggplot-objects-02-1.pdf b/figure.old/pos-ggplot-objects-02-1.pdf deleted file mode 100644 index c59b94bd..00000000 Binary files a/figure.old/pos-ggplot-objects-02-1.pdf and /dev/null differ diff --git a/figure.old/pos-ggplot-objects-info-02-1.pdf b/figure.old/pos-ggplot-objects-info-02-1.pdf deleted file mode 100644 index 4a700e6e..00000000 Binary files a/figure.old/pos-ggplot-objects-info-02-1.pdf and /dev/null differ diff --git a/figure.old/pos-hex-bin2d-plot-01-1.pdf b/figure.old/pos-hex-bin2d-plot-01-1.pdf deleted file mode 100644 index f2bed275..00000000 Binary files a/figure.old/pos-hex-bin2d-plot-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-hex-plot-01-1.pdf b/figure.old/pos-hex-plot-01-1.pdf deleted file mode 100644 index 403b8f81..00000000 Binary files a/figure.old/pos-hex-plot-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-histogram-plot-01-1.pdf b/figure.old/pos-histogram-plot-01-1.pdf deleted file mode 100644 index e8dcee35..00000000 Binary files a/figure.old/pos-histogram-plot-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-histogram-plot-03-1.pdf b/figure.old/pos-histogram-plot-03-1.pdf deleted file mode 100644 index e6f7ff20..00000000 Binary files a/figure.old/pos-histogram-plot-03-1.pdf and /dev/null differ diff --git a/figure.old/pos-histogram-plot-03a-1.pdf b/figure.old/pos-histogram-plot-03a-1.pdf deleted file mode 100644 index e1fee39a..00000000 Binary files a/figure.old/pos-histogram-plot-03a-1.pdf and /dev/null differ diff --git a/figure.old/pos-histogram-plot-04-1.pdf b/figure.old/pos-histogram-plot-04-1.pdf deleted file mode 100644 index eeed3ecf..00000000 Binary files a/figure.old/pos-histogram-plot-04-1.pdf and /dev/null differ diff --git a/figure.old/pos-inset-01-1.pdf b/figure.old/pos-inset-01-1.pdf deleted file mode 100644 index e666151b..00000000 Binary files a/figure.old/pos-inset-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-label-plot-01-1.pdf b/figure.old/pos-label-plot-01-1.pdf deleted file mode 100644 index f9353193..00000000 Binary files a/figure.old/pos-label-plot-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-line-plot-01-1.pdf b/figure.old/pos-line-plot-01-1.pdf deleted file mode 100644 index 937492f5..00000000 Binary files a/figure.old/pos-line-plot-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-102-1.pdf b/figure.old/pos-main-chunk-102-1.pdf deleted file mode 100644 index 61b85c64..00000000 Binary files a/figure.old/pos-main-chunk-102-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-25-1.pdf b/figure.old/pos-main-chunk-25-1.pdf deleted file mode 100644 index fc8bedd3..00000000 Binary files a/figure.old/pos-main-chunk-25-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-27-1.pdf b/figure.old/pos-main-chunk-27-1.pdf deleted file mode 100644 index 58d9526e..00000000 Binary files a/figure.old/pos-main-chunk-27-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-29-1.pdf b/figure.old/pos-main-chunk-29-1.pdf deleted file mode 100644 index 54b64c9d..00000000 Binary files a/figure.old/pos-main-chunk-29-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-31-1.pdf b/figure.old/pos-main-chunk-31-1.pdf deleted file mode 100644 index 0f6b001d..00000000 Binary files a/figure.old/pos-main-chunk-31-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-33-1.pdf b/figure.old/pos-main-chunk-33-1.pdf deleted file mode 100644 index b1d74545..00000000 Binary files a/figure.old/pos-main-chunk-33-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-34-1.pdf b/figure.old/pos-main-chunk-34-1.pdf deleted file mode 100644 index 22d14e15..00000000 Binary files a/figure.old/pos-main-chunk-34-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-36-1.pdf b/figure.old/pos-main-chunk-36-1.pdf deleted file mode 100644 index 4bfec3db..00000000 Binary files a/figure.old/pos-main-chunk-36-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-38-1.pdf b/figure.old/pos-main-chunk-38-1.pdf deleted file mode 100644 index 1d1815e1..00000000 Binary files a/figure.old/pos-main-chunk-38-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-39-1.pdf b/figure.old/pos-main-chunk-39-1.pdf deleted file mode 100644 index 4b4b5540..00000000 Binary files a/figure.old/pos-main-chunk-39-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-40-1.pdf b/figure.old/pos-main-chunk-40-1.pdf deleted file mode 100644 index 56b044a6..00000000 Binary files a/figure.old/pos-main-chunk-40-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-41-1.pdf b/figure.old/pos-main-chunk-41-1.pdf deleted file mode 100644 index 562c2800..00000000 Binary files a/figure.old/pos-main-chunk-41-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-44-1.pdf b/figure.old/pos-main-chunk-44-1.pdf deleted file mode 100644 index d7d53538..00000000 Binary files a/figure.old/pos-main-chunk-44-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-45-1.pdf b/figure.old/pos-main-chunk-45-1.pdf deleted file mode 100644 index 060ea581..00000000 Binary files a/figure.old/pos-main-chunk-45-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-54-1.pdf b/figure.old/pos-main-chunk-54-1.pdf deleted file mode 100644 index 495a285e..00000000 Binary files a/figure.old/pos-main-chunk-54-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-55-1.pdf b/figure.old/pos-main-chunk-55-1.pdf deleted file mode 100644 index eda5cd96..00000000 Binary files a/figure.old/pos-main-chunk-55-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-57-1.pdf b/figure.old/pos-main-chunk-57-1.pdf deleted file mode 100644 index fb650ff3..00000000 Binary files a/figure.old/pos-main-chunk-57-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-59-1.pdf b/figure.old/pos-main-chunk-59-1.pdf deleted file mode 100644 index f2f36643..00000000 Binary files a/figure.old/pos-main-chunk-59-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-62-1.pdf b/figure.old/pos-main-chunk-62-1.pdf deleted file mode 100644 index 34528763..00000000 Binary files a/figure.old/pos-main-chunk-62-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-64-1.pdf b/figure.old/pos-main-chunk-64-1.pdf deleted file mode 100644 index 34100d19..00000000 Binary files a/figure.old/pos-main-chunk-64-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-65-1.pdf b/figure.old/pos-main-chunk-65-1.pdf deleted file mode 100644 index 9a615915..00000000 Binary files a/figure.old/pos-main-chunk-65-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-66-1.pdf b/figure.old/pos-main-chunk-66-1.pdf deleted file mode 100644 index defaa489..00000000 Binary files a/figure.old/pos-main-chunk-66-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-68-1.pdf b/figure.old/pos-main-chunk-68-1.pdf deleted file mode 100644 index a560bdb8..00000000 Binary files a/figure.old/pos-main-chunk-68-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-76-1.pdf b/figure.old/pos-main-chunk-76-1.pdf deleted file mode 100644 index 63413f02..00000000 Binary files a/figure.old/pos-main-chunk-76-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-80-1.pdf b/figure.old/pos-main-chunk-80-1.pdf deleted file mode 100644 index bd18ed66..00000000 Binary files a/figure.old/pos-main-chunk-80-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-82-1.pdf b/figure.old/pos-main-chunk-82-1.pdf deleted file mode 100644 index cf95b1dc..00000000 Binary files a/figure.old/pos-main-chunk-82-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-84-1.pdf b/figure.old/pos-main-chunk-84-1.pdf deleted file mode 100644 index b66af58d..00000000 Binary files a/figure.old/pos-main-chunk-84-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-862-1.pdf b/figure.old/pos-main-chunk-862-1.pdf deleted file mode 100644 index d014da89..00000000 Binary files a/figure.old/pos-main-chunk-862-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-863-1.pdf b/figure.old/pos-main-chunk-863-1.pdf deleted file mode 100644 index 34c4231d..00000000 Binary files a/figure.old/pos-main-chunk-863-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-864-1.pdf b/figure.old/pos-main-chunk-864-1.pdf deleted file mode 100644 index d9fbe240..00000000 Binary files a/figure.old/pos-main-chunk-864-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-865-1.pdf b/figure.old/pos-main-chunk-865-1.pdf deleted file mode 100644 index 8e40183d..00000000 Binary files a/figure.old/pos-main-chunk-865-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-866-1.pdf b/figure.old/pos-main-chunk-866-1.pdf deleted file mode 100644 index ca7a1dfe..00000000 Binary files a/figure.old/pos-main-chunk-866-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-867-1.pdf b/figure.old/pos-main-chunk-867-1.pdf deleted file mode 100644 index 42a3c8fc..00000000 Binary files a/figure.old/pos-main-chunk-867-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-868-1.pdf b/figure.old/pos-main-chunk-868-1.pdf deleted file mode 100644 index 7bc41580..00000000 Binary files a/figure.old/pos-main-chunk-868-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-869-1.pdf b/figure.old/pos-main-chunk-869-1.pdf deleted file mode 100644 index 0ef1f3ad..00000000 Binary files a/figure.old/pos-main-chunk-869-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-870-1.pdf b/figure.old/pos-main-chunk-870-1.pdf deleted file mode 100644 index eae6c568..00000000 Binary files a/figure.old/pos-main-chunk-870-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-88-1.pdf b/figure.old/pos-main-chunk-88-1.pdf deleted file mode 100644 index 8a58adde..00000000 Binary files a/figure.old/pos-main-chunk-88-1.pdf and /dev/null differ diff --git a/figure.old/pos-main-chunk-98-1.pdf b/figure.old/pos-main-chunk-98-1.pdf deleted file mode 100644 index 7d8e6df3..00000000 Binary files a/figure.old/pos-main-chunk-98-1.pdf and /dev/null differ diff --git a/figure.old/pos-mapping-stage-02-1.pdf b/figure.old/pos-mapping-stage-02-1.pdf deleted file mode 100644 index c1f5d210..00000000 Binary files a/figure.old/pos-mapping-stage-02-1.pdf and /dev/null differ diff --git a/figure.old/pos-mapping-stage-03-1.pdf b/figure.old/pos-mapping-stage-03-1.pdf deleted file mode 100644 index fd1af494..00000000 Binary files a/figure.old/pos-mapping-stage-03-1.pdf and /dev/null differ diff --git a/figure.old/pos-mds-03-1.pdf b/figure.old/pos-mds-03-1.pdf deleted file mode 100644 index cd0f1b1a..00000000 Binary files a/figure.old/pos-mds-03-1.pdf and /dev/null differ diff --git a/figure.old/pos-model-11-1.pdf b/figure.old/pos-model-11-1.pdf deleted file mode 100644 index 8c4c97e3..00000000 Binary files a/figure.old/pos-model-11-1.pdf and /dev/null differ diff --git a/figure.old/pos-model-6a-1.pdf b/figure.old/pos-model-6a-1.pdf deleted file mode 100644 index 42b14f14..00000000 Binary files a/figure.old/pos-model-6a-1.pdf and /dev/null differ diff --git a/figure.old/pos-models-1a-1.pdf b/figure.old/pos-models-1a-1.pdf deleted file mode 100644 index 14a88914..00000000 Binary files a/figure.old/pos-models-1a-1.pdf and /dev/null differ diff --git a/figure.old/pos-models-2a-1.pdf b/figure.old/pos-models-2a-1.pdf deleted file mode 100644 index b52e5e50..00000000 Binary files a/figure.old/pos-models-2a-1.pdf and /dev/null differ diff --git a/figure.old/pos-models-3-1.pdf b/figure.old/pos-models-3-1.pdf deleted file mode 100644 index 7b70e148..00000000 Binary files a/figure.old/pos-models-3-1.pdf and /dev/null differ diff --git a/figure.old/pos-patchwork-02-1.pdf b/figure.old/pos-patchwork-02-1.pdf deleted file mode 100644 index eb8f126d..00000000 Binary files a/figure.old/pos-patchwork-02-1.pdf and /dev/null differ diff --git a/figure.old/pos-patchwork-02-2.pdf b/figure.old/pos-patchwork-02-2.pdf deleted file mode 100644 index 9369bcb8..00000000 Binary files a/figure.old/pos-patchwork-02-2.pdf and /dev/null differ diff --git a/figure.old/pos-patchwork-02a-1.pdf b/figure.old/pos-patchwork-02a-1.pdf deleted file mode 100644 index 02a10acd..00000000 Binary files a/figure.old/pos-patchwork-02a-1.pdf and /dev/null differ diff --git a/figure.old/pos-patchwork-03-1.pdf b/figure.old/pos-patchwork-03-1.pdf deleted file mode 100644 index 7af3ed89..00000000 Binary files a/figure.old/pos-patchwork-03-1.pdf and /dev/null differ diff --git a/figure.old/pos-pca-04-1.pdf b/figure.old/pos-pca-04-1.pdf deleted file mode 100644 index e88685b7..00000000 Binary files a/figure.old/pos-pca-04-1.pdf and /dev/null differ diff --git a/figure.old/pos-pca-05-1.pdf b/figure.old/pos-pca-05-1.pdf deleted file mode 100644 index 8e4e0c50..00000000 Binary files a/figure.old/pos-pca-05-1.pdf and /dev/null differ diff --git a/figure.old/pos-plot-1-1.pdf b/figure.old/pos-plot-1-1.pdf deleted file mode 100644 index fe44c262..00000000 Binary files a/figure.old/pos-plot-1-1.pdf and /dev/null differ diff --git a/figure.old/pos-plot-1a-1.pdf b/figure.old/pos-plot-1a-1.pdf deleted file mode 100644 index 89525fbb..00000000 Binary files a/figure.old/pos-plot-1a-1.pdf and /dev/null differ diff --git a/figure.old/pos-plot-1b-1.pdf b/figure.old/pos-plot-1b-1.pdf deleted file mode 100644 index 6ad81274..00000000 Binary files a/figure.old/pos-plot-1b-1.pdf and /dev/null differ diff --git a/figure.old/pos-plot-1c-1.pdf b/figure.old/pos-plot-1c-1.pdf deleted file mode 100644 index 349c641a..00000000 Binary files a/figure.old/pos-plot-1c-1.pdf and /dev/null differ diff --git a/figure.old/pos-plot-2-1.pdf b/figure.old/pos-plot-2-1.pdf deleted file mode 100644 index 1b9d4bfc..00000000 Binary files a/figure.old/pos-plot-2-1.pdf and /dev/null differ diff --git a/figure.old/pos-plot-3-1.pdf b/figure.old/pos-plot-3-1.pdf deleted file mode 100644 index 3d2f5cb3..00000000 Binary files a/figure.old/pos-plot-3-1.pdf and /dev/null differ diff --git a/figure.old/pos-plot-grob-01-1.pdf b/figure.old/pos-plot-grob-01-1.pdf deleted file mode 100644 index de80c630..00000000 Binary files a/figure.old/pos-plot-grob-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-plot-grob-01c-1.pdf b/figure.old/pos-plot-grob-01c-1.pdf deleted file mode 100644 index 2ff0db0f..00000000 Binary files a/figure.old/pos-plot-grob-01c-1.pdf and /dev/null differ diff --git a/figure.old/pos-plot-npc-eb-01-1.pdf b/figure.old/pos-plot-npc-eb-01-1.pdf deleted file mode 100644 index d5744c7e..00000000 Binary files a/figure.old/pos-plot-npc-eb-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-plot-npc-eb-02-1.pdf b/figure.old/pos-plot-npc-eb-02-1.pdf deleted file mode 100644 index 4ebdb139..00000000 Binary files a/figure.old/pos-plot-npc-eb-02-1.pdf and /dev/null differ diff --git a/figure.old/pos-plot-plot-02-1.pdf b/figure.old/pos-plot-plot-02-1.pdf deleted file mode 100644 index 150f16a8..00000000 Binary files a/figure.old/pos-plot-plot-02-1.pdf and /dev/null differ diff --git a/figure.old/pos-plot-plot-03-1.pdf b/figure.old/pos-plot-plot-03-1.pdf deleted file mode 100644 index 0c27fac5..00000000 Binary files a/figure.old/pos-plot-plot-03-1.pdf and /dev/null differ diff --git a/figure.old/pos-plot-plot-03c-1.pdf b/figure.old/pos-plot-plot-03c-1.pdf deleted file mode 100644 index 960e81f9..00000000 Binary files a/figure.old/pos-plot-plot-03c-1.pdf and /dev/null differ diff --git a/figure.old/pos-plot_composition-02-1.pdf b/figure.old/pos-plot_composition-02-1.pdf deleted file mode 100644 index bfcdfae6..00000000 Binary files a/figure.old/pos-plot_composition-02-1.pdf and /dev/null differ diff --git a/figure.old/pos-plot_composition-02-2.pdf b/figure.old/pos-plot_composition-02-2.pdf deleted file mode 100644 index 5237a1c8..00000000 Binary files a/figure.old/pos-plot_composition-02-2.pdf and /dev/null differ diff --git a/figure.old/pos-plot_composition-02-3.pdf b/figure.old/pos-plot_composition-02-3.pdf deleted file mode 100644 index e0354ec9..00000000 Binary files a/figure.old/pos-plot_composition-02-3.pdf and /dev/null differ diff --git a/figure.old/pos-plot_composition-03-1.pdf b/figure.old/pos-plot_composition-03-1.pdf deleted file mode 100644 index b171210f..00000000 Binary files a/figure.old/pos-plot_composition-03-1.pdf and /dev/null differ diff --git a/figure.old/pos-plot_composition-11-1.pdf b/figure.old/pos-plot_composition-11-1.pdf deleted file mode 100644 index 53563f09..00000000 Binary files a/figure.old/pos-plot_composition-11-1.pdf and /dev/null differ diff --git a/figure.old/pos-plot_composition-22-1.pdf b/figure.old/pos-plot_composition-22-1.pdf deleted file mode 100644 index dfecca2f..00000000 Binary files a/figure.old/pos-plot_composition-22-1.pdf and /dev/null differ diff --git a/figure.old/pos-plotmath-00-1.pdf b/figure.old/pos-plotmath-00-1.pdf deleted file mode 100644 index fc30eec0..00000000 Binary files a/figure.old/pos-plotmath-00-1.pdf and /dev/null differ diff --git a/figure.old/pos-plotmath-02-1.pdf b/figure.old/pos-plotmath-02-1.pdf deleted file mode 100644 index 03df4da1..00000000 Binary files a/figure.old/pos-plotmath-02-1.pdf and /dev/null differ diff --git a/figure.old/pos-plotmath-02a-1.pdf b/figure.old/pos-plotmath-02a-1.pdf deleted file mode 100644 index 633f9377..00000000 Binary files a/figure.old/pos-plotmath-02a-1.pdf and /dev/null differ diff --git a/figure.old/pos-position-03-1.pdf b/figure.old/pos-position-03-1.pdf deleted file mode 100644 index 2df4e651..00000000 Binary files a/figure.old/pos-position-03-1.pdf and /dev/null differ diff --git a/figure.old/pos-position-06-1.pdf b/figure.old/pos-position-06-1.pdf deleted file mode 100644 index 627361d2..00000000 Binary files a/figure.old/pos-position-06-1.pdf and /dev/null differ diff --git a/figure.old/pos-repel-plot-01-1.pdf b/figure.old/pos-repel-plot-01-1.pdf deleted file mode 100644 index cb8d2fa7..00000000 Binary files a/figure.old/pos-repel-plot-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-rug-plot-01-1.pdf b/figure.old/pos-rug-plot-01-1.pdf deleted file mode 100644 index fe2d46e3..00000000 Binary files a/figure.old/pos-rug-plot-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-scale-color-10-1.pdf b/figure.old/pos-scale-color-10-1.pdf deleted file mode 100644 index 62d85b20..00000000 Binary files a/figure.old/pos-scale-color-10-1.pdf and /dev/null differ diff --git a/figure.old/pos-scale-colour-10-1.pdf b/figure.old/pos-scale-colour-10-1.pdf deleted file mode 100644 index e59f4b38..00000000 Binary files a/figure.old/pos-scale-colour-10-1.pdf and /dev/null differ diff --git a/figure.old/pos-scale-datetime-01-1.pdf b/figure.old/pos-scale-datetime-01-1.pdf deleted file mode 100644 index 59b34a7b..00000000 Binary files a/figure.old/pos-scale-datetime-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-scale-datetime-02-1.pdf b/figure.old/pos-scale-datetime-02-1.pdf deleted file mode 100644 index 68b64a0b..00000000 Binary files a/figure.old/pos-scale-datetime-02-1.pdf and /dev/null differ diff --git a/figure.old/pos-scale-discrete-10-1.pdf b/figure.old/pos-scale-discrete-10-1.pdf deleted file mode 100644 index fb25a734..00000000 Binary files a/figure.old/pos-scale-discrete-10-1.pdf and /dev/null differ diff --git a/figure.old/pos-scale-discrete-10a-1.pdf b/figure.old/pos-scale-discrete-10a-1.pdf deleted file mode 100644 index a84f6695..00000000 Binary files a/figure.old/pos-scale-discrete-10a-1.pdf and /dev/null differ diff --git a/figure.old/pos-scale-discrete-11-1.pdf b/figure.old/pos-scale-discrete-11-1.pdf deleted file mode 100644 index 788564be..00000000 Binary files a/figure.old/pos-scale-discrete-11-1.pdf and /dev/null differ diff --git a/figure.old/pos-scale-limits-01-1.pdf b/figure.old/pos-scale-limits-01-1.pdf deleted file mode 100644 index c677fad9..00000000 Binary files a/figure.old/pos-scale-limits-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-scale-limits-02-1.pdf b/figure.old/pos-scale-limits-02-1.pdf deleted file mode 100644 index 85e31056..00000000 Binary files a/figure.old/pos-scale-limits-02-1.pdf and /dev/null differ diff --git a/figure.old/pos-scale-limits-02a-1.pdf b/figure.old/pos-scale-limits-02a-1.pdf deleted file mode 100644 index 0a454e34..00000000 Binary files a/figure.old/pos-scale-limits-02a-1.pdf and /dev/null differ diff --git a/figure.old/pos-scale-limits-03-1.pdf b/figure.old/pos-scale-limits-03-1.pdf deleted file mode 100644 index d1fda324..00000000 Binary files a/figure.old/pos-scale-limits-03-1.pdf and /dev/null differ diff --git a/figure.old/pos-scale-limits-04-1.pdf b/figure.old/pos-scale-limits-04-1.pdf deleted file mode 100644 index f1311f76..00000000 Binary files a/figure.old/pos-scale-limits-04-1.pdf and /dev/null differ diff --git a/figure.old/pos-scale-limits-05a-1.pdf b/figure.old/pos-scale-limits-05a-1.pdf deleted file mode 100644 index 733e86e3..00000000 Binary files a/figure.old/pos-scale-limits-05a-1.pdf and /dev/null differ diff --git a/figure.old/pos-scale-limits-07-1.pdf b/figure.old/pos-scale-limits-07-1.pdf deleted file mode 100644 index 3b088d22..00000000 Binary files a/figure.old/pos-scale-limits-07-1.pdf and /dev/null differ diff --git a/figure.old/pos-scale-ticks-01-1.pdf b/figure.old/pos-scale-ticks-01-1.pdf deleted file mode 100644 index d11424a3..00000000 Binary files a/figure.old/pos-scale-ticks-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-scale-ticks-02-1.pdf b/figure.old/pos-scale-ticks-02-1.pdf deleted file mode 100644 index 9030ef17..00000000 Binary files a/figure.old/pos-scale-ticks-02-1.pdf and /dev/null differ diff --git a/figure.old/pos-scale-ticks-02a-1.pdf b/figure.old/pos-scale-ticks-02a-1.pdf deleted file mode 100644 index 90ce8bf6..00000000 Binary files a/figure.old/pos-scale-ticks-02a-1.pdf and /dev/null differ diff --git a/figure.old/pos-scale-ticks-03-1.pdf b/figure.old/pos-scale-ticks-03-1.pdf deleted file mode 100644 index c23f842e..00000000 Binary files a/figure.old/pos-scale-ticks-03-1.pdf and /dev/null differ diff --git a/figure.old/pos-scale-ticks-04-1.pdf b/figure.old/pos-scale-ticks-04-1.pdf deleted file mode 100644 index f3417dab..00000000 Binary files a/figure.old/pos-scale-ticks-04-1.pdf and /dev/null differ diff --git a/figure.old/pos-scale-ticks-05-1.pdf b/figure.old/pos-scale-ticks-05-1.pdf deleted file mode 100644 index 0a7fe877..00000000 Binary files a/figure.old/pos-scale-ticks-05-1.pdf and /dev/null differ diff --git a/figure.old/pos-scale-trans-01-1.pdf b/figure.old/pos-scale-trans-01-1.pdf deleted file mode 100644 index b02d1816..00000000 Binary files a/figure.old/pos-scale-trans-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-scale-trans-02-1.pdf b/figure.old/pos-scale-trans-02-1.pdf deleted file mode 100644 index 2ce06bba..00000000 Binary files a/figure.old/pos-scale-trans-02-1.pdf and /dev/null differ diff --git a/figure.old/pos-scale-trans-03-1.pdf b/figure.old/pos-scale-trans-03-1.pdf deleted file mode 100644 index 41893ee5..00000000 Binary files a/figure.old/pos-scale-trans-03-1.pdf and /dev/null differ diff --git a/figure.old/pos-scale-trans-04-1.pdf b/figure.old/pos-scale-trans-04-1.pdf deleted file mode 100644 index c7cb5290..00000000 Binary files a/figure.old/pos-scale-trans-04-1.pdf and /dev/null differ diff --git a/figure.old/pos-scale-trans-05-1.pdf b/figure.old/pos-scale-trans-05-1.pdf deleted file mode 100644 index b7ca4cc4..00000000 Binary files a/figure.old/pos-scale-trans-05-1.pdf and /dev/null differ diff --git a/figure.old/pos-scatter-01-1.pdf b/figure.old/pos-scatter-01-1.pdf deleted file mode 100644 index c2a39df2..00000000 Binary files a/figure.old/pos-scatter-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-scatter-11-1.pdf b/figure.old/pos-scatter-11-1.pdf deleted file mode 100644 index 7371a6b4..00000000 Binary files a/figure.old/pos-scatter-11-1.pdf and /dev/null differ diff --git a/figure.old/pos-scatter-11a-1.pdf b/figure.old/pos-scatter-11a-1.pdf deleted file mode 100644 index 25542c1d..00000000 Binary files a/figure.old/pos-scatter-11a-1.pdf and /dev/null differ diff --git a/figure.old/pos-scatter-12-1.pdf b/figure.old/pos-scatter-12-1.pdf deleted file mode 100644 index 991db610..00000000 Binary files a/figure.old/pos-scatter-12-1.pdf and /dev/null differ diff --git a/figure.old/pos-scatter-12a-1.pdf b/figure.old/pos-scatter-12a-1.pdf deleted file mode 100644 index e0a9e668..00000000 Binary files a/figure.old/pos-scatter-12a-1.pdf and /dev/null differ diff --git a/figure.old/pos-scatter-13-1.pdf b/figure.old/pos-scatter-13-1.pdf deleted file mode 100644 index c3e0ee20..00000000 Binary files a/figure.old/pos-scatter-13-1.pdf and /dev/null differ diff --git a/figure.old/pos-scatter-13a-1.pdf b/figure.old/pos-scatter-13a-1.pdf deleted file mode 100644 index 22f0e535..00000000 Binary files a/figure.old/pos-scatter-13a-1.pdf and /dev/null differ diff --git a/figure.old/pos-scatter-13b-1.pdf b/figure.old/pos-scatter-13b-1.pdf deleted file mode 100644 index 864760f9..00000000 Binary files a/figure.old/pos-scatter-13b-1.pdf and /dev/null differ diff --git a/figure.old/pos-scatter-13c-1.pdf b/figure.old/pos-scatter-13c-1.pdf deleted file mode 100644 index 65e92d62..00000000 Binary files a/figure.old/pos-scatter-13c-1.pdf and /dev/null differ diff --git a/figure.old/pos-scatter-13info-1.pdf b/figure.old/pos-scatter-13info-1.pdf deleted file mode 100644 index 6fc262c3..00000000 Binary files a/figure.old/pos-scatter-13info-1.pdf and /dev/null differ diff --git a/figure.old/pos-scatter-14-1.pdf b/figure.old/pos-scatter-14-1.pdf deleted file mode 100644 index 0fa80c3c..00000000 Binary files a/figure.old/pos-scatter-14-1.pdf and /dev/null differ diff --git a/figure.old/pos-scatter-16-1.pdf b/figure.old/pos-scatter-16-1.pdf deleted file mode 100644 index 6239a803..00000000 Binary files a/figure.old/pos-scatter-16-1.pdf and /dev/null differ diff --git a/figure.old/pos-scatter-18-1.pdf b/figure.old/pos-scatter-18-1.pdf deleted file mode 100644 index 9915e59b..00000000 Binary files a/figure.old/pos-scatter-18-1.pdf and /dev/null differ diff --git a/figure.old/pos-sf_plot-01-1.pdf b/figure.old/pos-sf_plot-01-1.pdf deleted file mode 100644 index 94903880..00000000 Binary files a/figure.old/pos-sf_plot-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-smooth-plot-02-1.pdf b/figure.old/pos-smooth-plot-02-1.pdf deleted file mode 100644 index f68788f9..00000000 Binary files a/figure.old/pos-smooth-plot-02-1.pdf and /dev/null differ diff --git a/figure.old/pos-smooth-plot-03-1.pdf b/figure.old/pos-smooth-plot-03-1.pdf deleted file mode 100644 index 73cf29a7..00000000 Binary files a/figure.old/pos-smooth-plot-03-1.pdf and /dev/null differ diff --git a/figure.old/pos-smooth-plot-04-1.pdf b/figure.old/pos-smooth-plot-04-1.pdf deleted file mode 100644 index e82acbee..00000000 Binary files a/figure.old/pos-smooth-plot-04-1.pdf and /dev/null differ diff --git a/figure.old/pos-smooth-plot-05-1.pdf b/figure.old/pos-smooth-plot-05-1.pdf deleted file mode 100644 index fd2256d7..00000000 Binary files a/figure.old/pos-smooth-plot-05-1.pdf and /dev/null differ diff --git a/figure.old/pos-smooth-plot-06-1.pdf b/figure.old/pos-smooth-plot-06-1.pdf deleted file mode 100644 index 5a6b09a5..00000000 Binary files a/figure.old/pos-smooth-plot-06-1.pdf and /dev/null differ diff --git a/figure.old/pos-smooth-plot-07-1.pdf b/figure.old/pos-smooth-plot-07-1.pdf deleted file mode 100644 index 656e1455..00000000 Binary files a/figure.old/pos-smooth-plot-07-1.pdf and /dev/null differ diff --git a/figure.old/pos-smooth-plot-08-1.pdf b/figure.old/pos-smooth-plot-08-1.pdf deleted file mode 100644 index e3268cba..00000000 Binary files a/figure.old/pos-smooth-plot-08-1.pdf and /dev/null differ diff --git a/figure.old/pos-smooth-plot-12-1.pdf b/figure.old/pos-smooth-plot-12-1.pdf deleted file mode 100644 index 6404655b..00000000 Binary files a/figure.old/pos-smooth-plot-12-1.pdf and /dev/null differ diff --git a/figure.old/pos-smooth-plot-13-1.pdf b/figure.old/pos-smooth-plot-13-1.pdf deleted file mode 100644 index a9b33391..00000000 Binary files a/figure.old/pos-smooth-plot-13-1.pdf and /dev/null differ diff --git a/figure.old/pos-spline-01-1.pdf b/figure.old/pos-spline-01-1.pdf deleted file mode 100644 index 16ec8a36..00000000 Binary files a/figure.old/pos-spline-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-spline-02-1.pdf b/figure.old/pos-spline-02-1.pdf deleted file mode 100644 index f941627c..00000000 Binary files a/figure.old/pos-spline-02-1.pdf and /dev/null differ diff --git a/figure.old/pos-step-plot-01-1.pdf b/figure.old/pos-step-plot-01-1.pdf deleted file mode 100644 index 21061d34..00000000 Binary files a/figure.old/pos-step-plot-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-summary-plot-02-1.pdf b/figure.old/pos-summary-plot-02-1.pdf deleted file mode 100644 index 4cc4bfee..00000000 Binary files a/figure.old/pos-summary-plot-02-1.pdf and /dev/null differ diff --git a/figure.old/pos-summary-plot-03-1.pdf b/figure.old/pos-summary-plot-03-1.pdf deleted file mode 100644 index 2816941b..00000000 Binary files a/figure.old/pos-summary-plot-03-1.pdf and /dev/null differ diff --git a/figure.old/pos-summary-plot-04-1.pdf b/figure.old/pos-summary-plot-04-1.pdf deleted file mode 100644 index d595b37e..00000000 Binary files a/figure.old/pos-summary-plot-04-1.pdf and /dev/null differ diff --git a/figure.old/pos-summary-plot-04a-1.pdf b/figure.old/pos-summary-plot-04a-1.pdf deleted file mode 100644 index a65244f5..00000000 Binary files a/figure.old/pos-summary-plot-04a-1.pdf and /dev/null differ diff --git a/figure.old/pos-summary-plot-09-1.pdf b/figure.old/pos-summary-plot-09-1.pdf deleted file mode 100644 index 83d2fb6a..00000000 Binary files a/figure.old/pos-summary-plot-09-1.pdf and /dev/null differ diff --git a/figure.old/pos-summary-plot-09a-1.pdf b/figure.old/pos-summary-plot-09a-1.pdf deleted file mode 100644 index e9f7bb25..00000000 Binary files a/figure.old/pos-summary-plot-09a-1.pdf and /dev/null differ diff --git a/figure.old/pos-summary-plot-10-1.pdf b/figure.old/pos-summary-plot-10-1.pdf deleted file mode 100644 index d426ff6a..00000000 Binary files a/figure.old/pos-summary-plot-10-1.pdf and /dev/null differ diff --git a/figure.old/pos-summary-plot-10-2.pdf b/figure.old/pos-summary-plot-10-2.pdf deleted file mode 100644 index 448e8f03..00000000 Binary files a/figure.old/pos-summary-plot-10-2.pdf and /dev/null differ diff --git a/figure.old/pos-summary-plot-12-1.pdf b/figure.old/pos-summary-plot-12-1.pdf deleted file mode 100644 index a033a614..00000000 Binary files a/figure.old/pos-summary-plot-12-1.pdf and /dev/null differ diff --git a/figure.old/pos-table-plot-02-1.pdf b/figure.old/pos-table-plot-02-1.pdf deleted file mode 100644 index cdb79245..00000000 Binary files a/figure.old/pos-table-plot-02-1.pdf and /dev/null differ diff --git a/figure.old/pos-table-plot-04-1.pdf b/figure.old/pos-table-plot-04-1.pdf deleted file mode 100644 index cd573fd5..00000000 Binary files a/figure.old/pos-table-plot-04-1.pdf and /dev/null differ diff --git a/figure.old/pos-text-plot-01-1.pdf b/figure.old/pos-text-plot-01-1.pdf deleted file mode 100644 index ae46d41f..00000000 Binary files a/figure.old/pos-text-plot-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-text-plot-02-1.pdf b/figure.old/pos-text-plot-02-1.pdf deleted file mode 100644 index 146bdc9d..00000000 Binary files a/figure.old/pos-text-plot-02-1.pdf and /dev/null differ diff --git a/figure.old/pos-text-plot-02a-1.pdf b/figure.old/pos-text-plot-02a-1.pdf deleted file mode 100644 index 91d1901c..00000000 Binary files a/figure.old/pos-text-plot-02a-1.pdf and /dev/null differ diff --git a/figure.old/pos-text-plot-04-1.pdf b/figure.old/pos-text-plot-04-1.pdf deleted file mode 100644 index d364cfd1..00000000 Binary files a/figure.old/pos-text-plot-04-1.pdf and /dev/null differ diff --git a/figure.old/pos-text-plot-06-1.pdf b/figure.old/pos-text-plot-06-1.pdf deleted file mode 100644 index 950fb3df..00000000 Binary files a/figure.old/pos-text-plot-06-1.pdf and /dev/null differ diff --git a/figure.old/pos-text-plot-07-1.pdf b/figure.old/pos-text-plot-07-1.pdf deleted file mode 100644 index 32e8995f..00000000 Binary files a/figure.old/pos-text-plot-07-1.pdf and /dev/null differ diff --git a/figure.old/pos-themes-01-1.pdf b/figure.old/pos-themes-01-1.pdf deleted file mode 100644 index c9c848bc..00000000 Binary files a/figure.old/pos-themes-01-1.pdf and /dev/null differ diff --git a/figure.old/pos-themes-03-1.pdf b/figure.old/pos-themes-03-1.pdf deleted file mode 100644 index e6d862dc..00000000 Binary files a/figure.old/pos-themes-03-1.pdf and /dev/null differ diff --git a/figure.old/pos-themes-11-1.pdf b/figure.old/pos-themes-11-1.pdf deleted file mode 100644 index ce40ce82..00000000 Binary files a/figure.old/pos-themes-11-1.pdf and /dev/null differ diff --git a/figure.old/pos-themes-12-1.pdf b/figure.old/pos-themes-12-1.pdf deleted file mode 100644 index eadbfa88..00000000 Binary files a/figure.old/pos-themes-12-1.pdf and /dev/null differ diff --git a/figure.old/pos-themes-21-1.pdf b/figure.old/pos-themes-21-1.pdf deleted file mode 100644 index d67d43eb..00000000 Binary files a/figure.old/pos-themes-21-1.pdf and /dev/null differ diff --git a/figure.old/pos-themes-33-1.pdf b/figure.old/pos-themes-33-1.pdf deleted file mode 100644 index b90faa6c..00000000 Binary files a/figure.old/pos-themes-33-1.pdf and /dev/null differ diff --git a/figure.old/pos-tile-plot-02-1.pdf b/figure.old/pos-tile-plot-02-1.pdf deleted file mode 100644 index f248cbf7..00000000 Binary files a/figure.old/pos-tile-plot-02-1.pdf and /dev/null differ diff --git a/figure.old/pos-tile-plot-03-1.pdf b/figure.old/pos-tile-plot-03-1.pdf deleted file mode 100644 index 1ce671ee..00000000 Binary files a/figure.old/pos-tile-plot-03-1.pdf and /dev/null differ diff --git a/figure.old/pos-tile-plot-04-1.pdf b/figure.old/pos-tile-plot-04-1.pdf deleted file mode 100644 index 9a455e49..00000000 Binary files a/figure.old/pos-tile-plot-04-1.pdf and /dev/null differ diff --git a/figure.old/pos-ts-02-1.pdf b/figure.old/pos-ts-02-1.pdf deleted file mode 100644 index a4c3f656..00000000 Binary files a/figure.old/pos-ts-02-1.pdf and /dev/null differ diff --git a/figure.old/pos-ts-03-1.pdf b/figure.old/pos-ts-03-1.pdf deleted file mode 100644 index d847dc5b..00000000 Binary files a/figure.old/pos-ts-03-1.pdf and /dev/null differ diff --git a/figure.old/pos-ts-03plot-1.pdf b/figure.old/pos-ts-03plot-1.pdf deleted file mode 100644 index 63845be2..00000000 Binary files a/figure.old/pos-ts-03plot-1.pdf and /dev/null differ diff --git a/figure.old/pos-ts-05-1.pdf b/figure.old/pos-ts-05-1.pdf deleted file mode 100644 index 51777030..00000000 Binary files a/figure.old/pos-ts-05-1.pdf and /dev/null differ diff --git a/figure.old/pos-ts-08-1.pdf b/figure.old/pos-ts-08-1.pdf deleted file mode 100644 index c71650a2..00000000 Binary files a/figure.old/pos-ts-08-1.pdf and /dev/null differ diff --git a/figure.old/pos-violin-plot-02-1.pdf b/figure.old/pos-violin-plot-02-1.pdf deleted file mode 100644 index ee2d8009..00000000 Binary files a/figure.old/pos-violin-plot-02-1.pdf and /dev/null differ diff --git a/figure.old/pos-wind-05-1.pdf b/figure.old/pos-wind-05-1.pdf deleted file mode 100644 index 6f5b2a07..00000000 Binary files a/figure.old/pos-wind-05-1.pdf and /dev/null differ diff --git a/figure.old/pos-wind-06-1.pdf b/figure.old/pos-wind-06-1.pdf deleted file mode 100644 index 2618c379..00000000 Binary files a/figure.old/pos-wind-06-1.pdf and /dev/null differ diff --git a/figure.old/pos-wind-08-1.pdf b/figure.old/pos-wind-08-1.pdf deleted file mode 100644 index 9380a0d4..00000000 Binary files a/figure.old/pos-wind-08-1.pdf and /dev/null differ diff --git a/learn-r-2ed-purl/R.as.calculator.R b/learn-r-2ed-purl/R.as.calculator.R new file mode 100644 index 00000000..4f16682e --- /dev/null +++ b/learn-r-2ed-purl/R.as.calculator.R @@ -0,0 +1,1457 @@ +## ----echo=FALSE, include=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_fig_wide) +opts_knit$set(concordance=TRUE) +opts_knit$set(unnamed.chunk.label = 'calculator-chunk') + + +## ----numbers-0---------------------------------------------------------------------------------------------------------- +(3 + exp(2)) / cos(pi) + + +## ----numbers-radians---------------------------------------------------------------------------------------------------- +sin(45/180 * pi) + + +## ----numbers-00--------------------------------------------------------------------------------------------------------- +1 + 2 * 3 +1 + (2 * 3) +(1 + 2) * 3 + + +## ----numbers-1, eval=eval_playground------------------------------------------------------------------------------------ +1 + 1 +2 * 2 +2 + 10 / 5 +(2 + 10) / 5 +10^2 + 1 +sqrt(9) + + +## ----numbers-1a, eval=eval_playground----------------------------------------------------------------------------------- +pi +sin(pi) +log(100) +log10(100) +log2(8) +exp(1) + + +## ----numbers-2---------------------------------------------------------------------------------------------------------- +vct1 <- 1 +vct1 + 1 +vct1 +vct2 <- 10 +vct2 <- vct1 + vct2 +vct2 + + +## ----numbers-2a--------------------------------------------------------------------------------------------------------- +vct1 +print(vct1) +vct1 + 1 +print(vct1 + 1) + + +## ----numbers-3, tidy=FALSE, eval=eval_playground------------------------------------------------------------------------ +VCT1 <- VCT2 <- VCT3 <- 0 +VCT1 +VCT2 +VCT3 +1 -> VCT1 +VCT1 +VCT1 = 3 +VCT1 +remove(VCT1, VCT2, VCT3) # cleanup + + +## ----classes-01--------------------------------------------------------------------------------------------------------- +mode(1) +vct1 <- 1 +is.numeric(vct1) + + +## ----classes-02--------------------------------------------------------------------------------------------------------- +is.numeric(1L) +is.integer(1L) +is.double(1L) + + +## ----classes-03--------------------------------------------------------------------------------------------------------- +is.numeric(1) + + +## ----classes-03a-------------------------------------------------------------------------------------------------------- +is.integer(1) +is.double(1) + + +## ----numbers-4aann------------------------------------------------------------------------------------------------------ +c(3, 1, 2) + + +## ----numbers-4aa-------------------------------------------------------------------------------------------------------- +vct4 <- c(3, 1, 2) +length(vct4) +vct4 + + +## ----numbers-4bb-------------------------------------------------------------------------------------------------------- +vct5 <- c(4, 5, 0) +vct5 + + +## ----numbers-4cc-------------------------------------------------------------------------------------------------------- +vct6 <- c(vct4, vct5) +vct6 + + +## ----numbers-4dd-------------------------------------------------------------------------------------------------------- +vct7 <- c(vct5, vct4) +vct7 + + +## ----numeric-extract-member--------------------------------------------------------------------------------------------- +vct7[3] +vct7[c(6, 2)] + + +## ----numeric-empty-faq-------------------------------------------------------------------------------------------------- +numeric() + + +## ----numbers-4ee-------------------------------------------------------------------------------------------------------- +c(vct7, numeric()) + + +## ----numbers-4a--------------------------------------------------------------------------------------------------------- +append(vct4, vct5) + + +## ----numbers-4b--------------------------------------------------------------------------------------------------------- +append(vct4, values = vct5, after = 2) + + +## ----numbers-5, eval=eval_playground------------------------------------------------------------------------------------ +-1:5 +5:-1 +seq(from = -1, to = 1, by = 0.1) +rep(-5, times = 4) +rep(1:2, length.out = 4) + + +## ----numeric-zeros1-faq------------------------------------------------------------------------------------------------- +numeric(length = 10) + + +## ----numeric-zeros2-faq------------------------------------------------------------------------------------------------- +rep(0, times = 10) + + +## ----numbers-6aa-------------------------------------------------------------------------------------------------------- +log10(100) +log10(c(10, 5, 100, 200)) + + +## ----numbers-6ab-------------------------------------------------------------------------------------------------------- +sum(100) +sum(c(10, 5, 100, 200)) + + +## ----numbers-6ac-------------------------------------------------------------------------------------------------------- +c(3, 1, 2) + c(1, 2, 31) + + +## ----numbers-6ad-------------------------------------------------------------------------------------------------------- +c(3, 1, 2) + 1 +c(3, 1, 2) + c(1, 1, 1) + + +## ----numbers-6---------------------------------------------------------------------------------------------------------- +vct4 <- c(3, 1, 2) +(vct4 + 1) * 2 +vct4 * 0:1 +vct4 - vct4 + + +## ----numbers-6a--------------------------------------------------------------------------------------------------------- +vct8 <- rep(1, 6) +vct8 +vct8 + 1:2 +vct8 + 1:3 +vct8 + 1:4 + + +## ----------------------------------------------------------------------------------------------------------------------- +vct9 <- numeric(length = 0) # named argument +vct9 +length(vct8) + + +## ----------------------------------------------------------------------------------------------------------------------- +numeric() # default argument + + +## ----------------------------------------------------------------------------------------------------------------------- +length(c(vct4, vct9, vct5)) +length(c(vct4, vct5)) + + +## ----------------------------------------------------------------------------------------------------------------------- +log(numeric(0)) +5 + numeric(0) + + +## ----------------------------------------------------------------------------------------------------------------------- +numeric(length = 5) + + +## ----------------------------------------------------------------------------------------------------------------------- +vct10 <- 1:5 +length(vct10) <- 10 +vct10 + + +## ----------------------------------------------------------------------------------------------------------------------- +vct11 <- 1:10 +vct11 +length(vct11) <- 5 +vct11 + + +## ----numbers-8---------------------------------------------------------------------------------------------------------- +vct12 <- c(NA, 5) +vct12 +vct12 + 1 + + +## ----numbers-8a--------------------------------------------------------------------------------------------------------- +vct12 + Inf +Inf / vct12 +-1 / 0 +1 / 0 +Inf / Inf +Inf + 4 +-Inf * -1 + + +## ----numbers-PG00, eval=eval_playground--------------------------------------------------------------------------------- +123 + numeric() +123 + NA + + +## ----numbers-8b--------------------------------------------------------------------------------------------------------- +is.na(c(NA, 1)) + + +## ----numbers-9---------------------------------------------------------------------------------------------------------- +1 - 1e-20 + + +## ----integers-1--------------------------------------------------------------------------------------------------------- +1L + 3L +1L * 3L + + +## ----integers-1a-------------------------------------------------------------------------------------------------------- +1L / 3L +1L %/% 3L +1L %% 3L + + +## ----integers-1b-------------------------------------------------------------------------------------------------------- +1000000L * 1000000L + + +## ----integers-2--------------------------------------------------------------------------------------------------------- +is.numeric(1L) +is.integer(1L) +is.double(1L) +is.double(1L / 3L) +is.numeric(1L / 3L) + + +## ----integers-PG1, eval=eval_playground--------------------------------------------------------------------------------- +1 * 1000000L * 1000000L +1000000L * 1000000L * 1 + + +## ----comparison-5------------------------------------------------------------------------------------------------------- +1e20 == 1 + 1e20 +1 == 1 + 1e-20 +0 == 1e-20 + + +## ----numbers-EB10------------------------------------------------------------------------------------------------------- +format(5.123, digits = 16) # near maximun resolution +format(5.123, digits = 22) # more digits than in resolution + + +## ----machine-eps-01----------------------------------------------------------------------------------------------------- +.Machine$double.eps +.Machine$double.neg.eps +.Machine$double.max +.Machine$double.min +.Machine$double.base + + +## ----machine-eps-01a---------------------------------------------------------------------------------------------------- +.Machine$double.xmax +.Machine$integer.max + + +## ----machine-eps-02----------------------------------------------------------------------------------------------------- +1e1026 +1e-1026 + + +## ----machine-eps-03, warning=TRUE--------------------------------------------------------------------------------------- +2147483699L + + +## ----machine-eps-04----------------------------------------------------------------------------------------------------- +2147483600L + 99L +2147483600L + 99 +2147483600L * 2147483600L +2147483600L * 2147483600 + + +## ----machine-eps-05----------------------------------------------------------------------------------------------------- +2147483600L * 2147483600L +2147483600L^2L + + +## ----convert-3---------------------------------------------------------------------------------------------------------- +round(0.0124567, digits = 3) +signif(0.0124567, digits = 3) +round(1789.1234, digits = -1) +round(1789.1234, digits = 3) +signif(1789.1234, digits = 3) + + +## ----convert-3x--------------------------------------------------------------------------------------------------------- +vct13 <- 0.12345 +vct14 <- round(vct13, digits = 2) +vct13 == vct14 +vct13 - vct14 +vct14 + + +## ----convert-3a--------------------------------------------------------------------------------------------------------- +round(0.0124567, digits = 3) +round(0.0124567, 3) + + +## ----convert-3b--------------------------------------------------------------------------------------------------------- +round(0.0124567, di = 3) + + +## ----numbers-complex---------------------------------------------------------------------------------------------------- +cmp1 <- complex(real = c(-1, 1), imaginary = c(0, 0)) +cmp1 +cmp2 <- sqrt(cmp1) +cmp2 +cmp2^2 + + +## ----------------------------------------------------------------------------------------------------------------------- +an.object <- 1:4 +remove(an.object) # using a bare name + + +## ----------------------------------------------------------------------------------------------------------------------- +an.object <- 5:2 +remove(list = "an.object") # using a character vector + + +## ----------------------------------------------------------------------------------------------------------------------- +an.object <- 1:4 +another.object <- 2 +objects(pattern = "*.object") +remove(an.object) +objects(pattern = "*.object") + + +## ----numbers-7---------------------------------------------------------------------------------------------------------- +objects(pattern = "^vec.*") + + +## ----numbers-last------------------------------------------------------------------------------------------------------- +remove(list = objects(pattern = "^vct[[:digit:]]?")) + + +## ----char-1------------------------------------------------------------------------------------------------------------- +vct1 <- "A" +vct1 +vct2 <- 'A' +vct2 +vct1 == vct2 # two variables holding character values, or named objects +"A" == 'A' # two constant character values, or anonymous objects + + +## ----char-1a------------------------------------------------------------------------------------------------------------ +vct3 <- 'ABC' +vct4 <- "bcdefg" +vct5 <- c("123", "xyz") +c(vct3, vct4, vct5) + + +## ----char-3------------------------------------------------------------------------------------------------------------- +"He said 'hello' when he came in" +'He said "hello" when he came in' + + +## ----char-4------------------------------------------------------------------------------------------------------------- +vct6 <- "abc\ndef\tx\"yz\"\\\tm" +print(vct6) +cat(vct6) + + +## ----char-nchar-01------------------------------------------------------------------------------------------------------ +nchar(x = "abracadabra") +nchar(x = c("abracadabra", "workaholic", "")) + + +## ----char-toupper-01---------------------------------------------------------------------------------------------------- +toupper(x = "aBcD") +tolower(x = "aBcD") + + +## ----char-trim-01------------------------------------------------------------------------------------------------------- +strtrim(x = "abracadabra", width = 6) +strtrim(x = "abra", width = 6) +strtrim(x = c("abracadabra", "workaholic"), 6) +strtrim(x = c("abracadabra", "workaholic"), c(6, 3)) + + +## ----char-wrap-01------------------------------------------------------------------------------------------------------- +strwrap(x = "This is a long sentence used to show how line wrapping works.", width = 20) + + +## ----char-wrap-02, eval=eval_playground--------------------------------------------------------------------------------- +wrapped_sentence <- + strwrap(x = "This is a very long sentence used to show how line wrapping works.", + width = 10, + prefix = "\n") +print(wrapped_sentence) +cat(wrapped_sentence, "\n") + + +## ----char-paste-01------------------------------------------------------------------------------------------------------ +paste("n =", 3) +paste("n", 3, sep = " = ") + + +## ----char-paste-02------------------------------------------------------------------------------------------------------ +friends <- c("John ", "Yan ", "Juana ", "Mary ") +fruits <- c("apples", "lichees", "oranges", "strawberries") +paste(friends, "eats ", fruits, ".", sep = "") + + +## ----char-paste-03------------------------------------------------------------------------------------------------------ +cat(paste(friends, "eats ", fruits, collapse = ".\n", sep = "")) + + +## ----char-paste-04------------------------------------------------------------------------------------------------------ +collapsed_fruits <- paste(fruits, collapse = ", ") +paste("My friends eat", collapsed_fruits, "and other fruits.") + + +## ----char-paste-05------------------------------------------------------------------------------------------------------ +paste("My friends eat", paste(fruits, collapse = ", "), "and other fruits.") + + +## ----char-strrep-01----------------------------------------------------------------------------------------------------- +rep(x = "ABC", times = 3) +strrep(x = "ABC", times = 3) +strrep(x = "ABC", times = c(2, 4)) +strrep(x = c("ABC", "X"), times = 2) +strrep(x = c("ABC", "X"), times = c(2, 5)) + + +## ----char-str-00a------------------------------------------------------------------------------------------------------- +trimws(x = " two words ") +trimws(x = c(" eight words and a newline at the end\n", " two words ")) + + +## ----char-str-01-------------------------------------------------------------------------------------------------------- +substr(x = "abracadabra", start = 5, stop = 9) +substr(x = c("abracadabra", "workaholic"), start = 5, stop = 11) + + +## ----char-str-02-------------------------------------------------------------------------------------------------------- +vct7 <- c("abracadabra", "workaholic") +substr(x = vct7, start = 5, stop = 9) <- "xxx" +vct7 + + +## ----char-str-03-------------------------------------------------------------------------------------------------------- +vct8 <- c("abracadabra", "workaholic") +substr(x = vct8, start = 5, stop = 6) <- "xxx" +vct8 + + +## ----char-str-04, eval=eval_playground---------------------------------------------------------------------------------- +VCT1 <- c("abracadabra", "workaholic") +substr(x = VCT1, start = 5, stop = 11) <- "xxx" +VCT1 +remove(VCT1) # clean up + + +## ----char-regex-01------------------------------------------------------------------------------------------------------ +sub(pattern = "ab", replacement = "AB", x = "about") + + +## ----char-regex-02------------------------------------------------------------------------------------------------------ +sub(pattern = "ab", replacement = "x", x = "abracadabra") +gsub(pattern = "ab", replacement = "x", x = "abracadabra") + + +## ----char-regex-03, eval=eval_playground-------------------------------------------------------------------------------- +sub(pattern = "ab", replacement = "x", x = c("abra", "cadabra")) +gsub(pattern = "ab", replacement = "x", x = c("abra", "cadabra")) + + +## ----char-regex-04------------------------------------------------------------------------------------------------------ +grep(pattern = "C", x = c("R", "C++", "C", "Perl", "Pascal")) +grep(pattern = "C", x = c("R", "C++", "C", "Perl", "Pascal"), value = TRUE) +grep(pattern = "C", x = c("R", "C++", "C", "Perl", "Pascal"), ignore.case = TRUE) + + +## ----char-regex-05------------------------------------------------------------------------------------------------------ +grepl(pattern = "C", x = c("R", "C++", "C", "Perl", "Pascal")) +grepl(pattern = "C", x = c("R", "C++", "C", "Perl", "Pascal"), ignore.case = TRUE) + + +## ----char-regex-06------------------------------------------------------------------------------------------------------ +sub(pattern = "ab", replacement = "AB", x = "about", fixed = TRUE) + + +## ----char-regex-07------------------------------------------------------------------------------------------------------ +gsub(pattern = "ab|t", replacement = "123", x = "about") + + +## ----char-regex-08------------------------------------------------------------------------------------------------------ +gsub(pattern = "a[0123456789]", + replacement = "ab", + x = c("a1out", "a9out", "a3out")) + + +## ----char-regex-09------------------------------------------------------------------------------------------------------ +gsub(pattern = "^a[0123456789]", + replacement = "ab", + x = c("a1out", "a9out", " a3out")) + + +## ----char-regex-10------------------------------------------------------------------------------------------------------ +gsub(pattern = "out$", + replacement = "", + x = c("about", "a9out", "a3outx")) + + +## ----char-regex-11------------------------------------------------------------------------------------------------------ +gsub(pattern = ".$", + replacement = "", + x = c("about", "a9out", "a3outx")) + + +## ----char-regex-12------------------------------------------------------------------------------------------------------ +gsub(pattern = "^.[0-9][a-z]*$", + replacement = "gone", + x = c("about", "a9out", "a3outx")) + + +## ----char-regex-13a----------------------------------------------------------------------------------------------------- +gsub(pattern = "^.([[:digit:]])[[:lower:]]*$", + replacement = "gone with \\1", + x = c("about", "a9out", "a3outx")) + + +## ----char-regex-13------------------------------------------------------------------------------------------------------ +gsub(pattern = "^.([0-9])[a-z]*$", + replacement = "gone with \\1", + x = c("about", "a9out", "a3outx")) + + +## ----char-regex-14, eval=eval_playground-------------------------------------------------------------------------------- +gsub(pattern = "^.+$", + replacement = "", + x = c("about", "a9out", "a3outx")) +gsub(pattern = "^.?$", + replacement = "", + x = c("about", "a9out", "a3outx")) + + +## ----char-regex-20------------------------------------------------------------------------------------------------------ +strsplit(x = "2023-07-29 10:30", split = " ") + + +## ----char-regex-21------------------------------------------------------------------------------------------------------ +strsplit(x = "2023-07-29 10:30", split = " |-|:") + + +## ----char-regex-23------------------------------------------------------------------------------------------------------ +strsplit(x = "2023-07-29", split = "-[0-9]+$") + + +## ----char-regex-22------------------------------------------------------------------------------------------------------ +strsplit(x = c("2023-07-29 10:30", "2023-07-29 19:17"), split = " ") + + +## ----include=FALSE------------------------------------------------------------------------------------------------------ +rm(list = setdiff(ls(pattern="*"), to.keep)) + + +## ----logical-1---------------------------------------------------------------------------------------------------------- +vct1 <- TRUE +mode(vct1) +vct1 +!TRUE # negation +TRUE && FALSE # logical AND +TRUE || FALSE # logical OR +xor(TRUE, FALSE) # exclusive OR + + +## ----logical-2---------------------------------------------------------------------------------------------------------- +c(TRUE, FALSE) & c(TRUE,TRUE) # vectorised AND +c(TRUE, FALSE) | c(TRUE,TRUE) # vectorised OR + + +## ----------------------------------------------------------------------------------------------------------------------- +vct2 <- c(TRUE, FALSE, FALSE) +any(vct2) +all(vct2) +any(c(TRUE, FALSE) & c(TRUE,TRUE)) +all(c(TRUE, FALSE) & c(TRUE,TRUE)) +any(c(TRUE, FALSE) | c(TRUE,TRUE)) +all(c(TRUE, FALSE) | c(TRUE,TRUE)) + + +## ----logical-3---------------------------------------------------------------------------------------------------------- +TRUE || NA +FALSE || NA +TRUE && NA +FALSE && NA +TRUE && FALSE && NA +TRUE && TRUE && NA + + +## ----logical-4---------------------------------------------------------------------------------------------------------- +c(TRUE, FALSE) & c(TRUE,TRUE) & NA +c(TRUE, FALSE) & c(TRUE,TRUE) & c(NA, NA) +c(TRUE, FALSE) | c(TRUE,TRUE) | c(NA, NA) + + +## ----logical-PG01,eval=eval_playground---------------------------------------------------------------------------------- +c(TRUE, FALSE, TRUE, NA) & FALSE +c(TRUE, FALSE, TRUE, NA) | c(TRUE, FALSE) + + +## ----faq-vectors-01----------------------------------------------------------------------------------------------------- +vct2 <- rep(NA, 5) # toy data +tmp <- is.na(vct2) # tmp for temporary +all(tmp) + + +## ---- 1.0 +1.2 >= 1.0 +1.2 == 1.0 +1.2 != 1.0 +1.2 <= 1.0 +1.2 < 1.0 + + +## ----comparison-2------------------------------------------------------------------------------------------------------- +vct3 <- 1:10 +vct3 > 5 +vct3 < 5 +vct3 == 5 +all(vct3 > 5) +any(vct3 > 5) +vct4 <- vct3 > 5 +vct4 +any(vct4) +all(vct4) + + +## ----logical-2a--------------------------------------------------------------------------------------------------------- +vct5 <- -2:3 +vct5 >= -1 & vct5 <= 1 + + +## ----logical-2b--------------------------------------------------------------------------------------------------------- +!(vct5 >= -1 & vct5 <= 1) + + +## ----logical-2c--------------------------------------------------------------------------------------------------------- +vct5 < -1 | vct5 > 1 + + +## ----comparison-PG00, eval=eval_playground------------------------------------------------------------------------------ +vct6 <- 1:10 +vct6 > 3 | vct6 + 2 < 3 + + +## ----comparison-4------------------------------------------------------------------------------------------------------- +vct7 <- c(1:10, NA) +all(vct7 < 20) +any(vct7 > 20) +all(vct7 < 20, na.rm=TRUE) +any(vct7 > 20, na.rm=TRUE) + + +## ----machine-eps-06----------------------------------------------------------------------------------------------------- +sin(pi) == 0 # angle in radians, not degrees! +sin(2 * pi) == 0 +abs(sin(pi)) < 1e-15 +abs(sin(2 * pi)) < 1e-15 +sin(pi) +sin(2 * pi) + + +## ----include=FALSE------------------------------------------------------------------------------------------------------ +rm(list = setdiff(ls(pattern="*"), to.keep)) + + +## ----sets-00------------------------------------------------------------------------------------------------------------ +fruits <- c("apple", "pear", "orange", "lemon", "tangerine") +bakery <- c("bread", "buns", "cake", "cookies") +dairy <- c("milk", "butter", "cheese") +shopping <- c("bread", "butter", "apple", "cheese", "orange") +intersect(fruits, shopping) +intersect(bakery, shopping) +intersect(dairy, shopping) +"lemon" %in% dairy +"lemon" %in% fruits +dairy %in% shopping +union(bakery, dairy) +setdiff(union(bakery, dairy), shopping) # nested call + + +## ----sets-00a----------------------------------------------------------------------------------------------------------- +union(c("a", "a", "b"), c("b", "a", "b")) # set operation + + +## ----sets-0ba----------------------------------------------------------------------------------------------------------- +setequal(c("a", "a", "b"), c("b", "a", "b")) # sets compared +all.equal(c("a", "a", "b"), c("b", "a", "b")) # vectors compared +identical(c("a", "a", "b"), c("b", "a", "b")) # vectors compared + + +## ----sets-01------------------------------------------------------------------------------------------------------------ +vct1 <- c("a", "b", "c", "b") + + +## ----sets-02------------------------------------------------------------------------------------------------------------ +is.element("a", vct1) +"a" %in% vct1 +c("a", "a", "z") %in% vct1 + + +## ----sets-02a----------------------------------------------------------------------------------------------------------- +vct1 %in% "a" + + +## ----sets-02b----------------------------------------------------------------------------------------------------------- +!is.element("a", vct1) +!"a" %in% vct1 +!c("a", "a", "z") %in% vct1 + + +## ----sets-PG00, eval=FALSE---------------------------------------------------------------------------------------------- +## vct2 <- c("a", "a", "z") +## vct2 == "a" | vct2 == "b" | vct2 == "c" | xvct2 == "d" + + +## ----sets-03------------------------------------------------------------------------------------------------------------ +unique(vct1) + + +## ----sets-03a----------------------------------------------------------------------------------------------------------- +length(unique(vct1)) + + +## ----sets-03b, eval=eval_playground------------------------------------------------------------------------------------- +c("a", "a", "z") %in% vct1 +c("a", "a", "z") %in% unique(vct1) + + +## ----sets-expl-01------------------------------------------------------------------------------------------------------- +duplicated(vct1) +anyDuplicated(vct1) + + +## ----sets-PG01, eval=eval_playground------------------------------------------------------------------------------------ +union(c("a", "a", "z"), vct1) +c(c("a", "a", "z"), vct1) +c("a", "a", "z", vct1) + + +## ----sets-EB01---------------------------------------------------------------------------------------------------------- +9 %in% 2:4 +9 %in% ((2:4) * (2:4)) +c(1, 16) %in% ((2:4) * (2:4)) + + +## ----sets-EB02a--------------------------------------------------------------------------------------------------------- +c("ab", "xy") %in% character() +character() %in% c("a", "b", "c") +union("ab", character()) + + +## ----sets-warn-flt1----------------------------------------------------------------------------------------------------- +c(cos(pi), sin(pi)) %in% c(0, -1) +c(cos(pi), sin(pi)) + + +## ----include=FALSE------------------------------------------------------------------------------------------------------ +rm(list = setdiff(ls(pattern="*"), to.keep)) + + +## ----mode-1------------------------------------------------------------------------------------------------------------- +vct1 <- 1:5 +class(vct1) +inherits(vct1, "character") +inherits(vct1, "numeric") + + +## ----mode-2------------------------------------------------------------------------------------------------------------- +is.numeric(vct1) # no distinction of integer or double +is.double(vct1) +is.integer(vct1) +is.logical(vct1) +is.character(vct1) + + +## ----mode-1b------------------------------------------------------------------------------------------------------------ +inherits(vct1, c("numeric", "character", "logical"), which = TRUE) + + +## ----mode-3a------------------------------------------------------------------------------------------------------------ +mode(c(1, 2, 3)) # no distinction of integer or double +typeof(c(1, 2, 3)) +class(c(1, 2, 3)) +mode(c(1L, 2L, 3L)) # no distinction of integer or double +typeof(c(1L, 2L, 3L)) +class(c(1L, 2L, 3L)) + + +## ----mode-3b------------------------------------------------------------------------------------------------------------ +mode(factor(c("a", "b", "c"))) # no distinction of integer or double +typeof(factor(c("a", "b", "c"))) +class(factor(c("a", "b", "c"))) + + +## ----mode-3c------------------------------------------------------------------------------------------------------------ +mode(c("a", "b", "c")) +typeof(c("a", "b", "c")) +class(c("a", "b", "c")) + + +## ----mode-3d------------------------------------------------------------------------------------------------------------ +mode(c(TRUE, FALSE)) +typeof(c(TRUE, FALSE)) +class(c(TRUE, FALSE)) + + +## ----include=FALSE------------------------------------------------------------------------------------------------------ +rm(list = setdiff(ls(pattern="*"), to.keep)) + + +## ----convert-1---------------------------------------------------------------------------------------------------------- +as.character(102) +as.character(TRUE) +as.character(3.0e10) +as.numeric("203") +as.logical("TRUE") +as.logical(100) +as.logical(0) +as.logical(-1) + + +## ----convert-1a--------------------------------------------------------------------------------------------------------- +TRUE + 10 +1 || 0 +FALSE | -2:2 + + +## ----convert-PG1, eval=eval_playground---------------------------------------------------------------------------------- +as.numeric("5E+5") +as.numeric("50e+4") +as.numeric(".12") +as.numeric("0.12") +as.numeric("A") +as.logical("TRUE") +as.logical("FALSE") +as.logical("T") +as.logical("t") +as.logical("true") +as.logical("NA") + + +## ----convert-2a--------------------------------------------------------------------------------------------------------- +vct1 <- c("1", "2", "3") +length(vct1) + + +## ----convert-2b--------------------------------------------------------------------------------------------------------- +vct2 <- "123.1" +length(vct2) + + +## ----convert-2c--------------------------------------------------------------------------------------------------------- +as.numeric(vct1) +as.numeric(vct2) +as.integer(vct1) +as.integer(vct2) + + +## ----convert-5---------------------------------------------------------------------------------------------------------- +vct2 = c(123.4567890, 1.0) +format(vct2) # using defaults +format(123.4567890) # using defaults +format(1.0) # using defaults +format(vct2, digits = 3, nsmall = 1) +format(vct2, digits = 3, scientific = TRUE) + + +## ----convert-6---------------------------------------------------------------------------------------------------------- +x = c(123.4567890, 1.0) +sprintf("The numbers are: %4.2f and %.0f", x[1], x[2]) +sprintf("The numbers are: %.4g and %.2g", x[1], x[2]) +sprintf("The numbers are: %4.2e and %.0e", x[1], x[2]) + + +## ----nas-01------------------------------------------------------------------------------------------------------------- +vct3 <- c(1, NA) +is.numeric(vct3[2]) +is.numeric(NA) + + +## ----nas-01a------------------------------------------------------------------------------------------------------------ +vct4 <- c("abc", NA) +is.character(vct4[2]) +class(NA_character_) + + +## ----nas-01b------------------------------------------------------------------------------------------------------------ +is.character(NA) +class(NA) + + +## ----nas-01c------------------------------------------------------------------------------------------------------------ +vct5 <- NA +c(vct5, 2:3) + + +## ----nas-02------------------------------------------------------------------------------------------------------------- +vct3[3] <- vct4[2] + + +## ----include=FALSE------------------------------------------------------------------------------------------------------ +rm(list = setdiff(ls(pattern="*"), to.keep)) + + +## ----vectors-1a--------------------------------------------------------------------------------------------------------- +vct1 <- letters[1:10] +vct1 + + +## ----vectors-1---------------------------------------------------------------------------------------------------------- +vct1[2] + + +## ----vectors-eb-01------------------------------------------------------------------------------------------------------ +month.name +month.name[6] + + +## ----faq-vectors-1------------------------------------------------------------------------------------------------------ +month.name[length(month.name)] + + +## ----vectors-2---------------------------------------------------------------------------------------------------------- +vct1[c(3, 2)] +vct1[10:1] + + +## ----vectors-PG1, eval=eval_playground---------------------------------------------------------------------------------- +length(vct1) +vct1[c(3, 3, 3, 3)] +vct1[c(10:1, 1:10)] +vct1[c(1, 11)] +vct1[11] + + +## ----vectors-3---------------------------------------------------------------------------------------------------------- +vct1[-2] +vct1[-c(3,2)] +vct1[-3:-2] + + +## ----vectors-5, eval=eval_playground------------------------------------------------------------------------------------ +vct1[ ] +vct1[0] +vct1[numeric(0)] +vct1[NA] +vct1[c(1, NA)] +vct1[NULL] +vct1[c(1, NULL)] + + +## ----vectors-6---------------------------------------------------------------------------------------------------------- +vct1[TRUE] +vct1[FALSE] +vct1[c(TRUE, FALSE)] +vct1[c(FALSE, TRUE)] +vct1 > "c" +vct1[vct1 > "c"] + + +## ----vectors-PG6, eval=eval_playground---------------------------------------------------------------------------------- +vct1 <- letters[1:10] +vct2 <- 1:10 +selector <- vct1 > "c" +selector +vct1[selector] +vct2[selector] + + +## ----vectors-PG6a, eval=eval_playground--------------------------------------------------------------------------------- +indexes <- which(vct1 > "c") +indexes +vct1[indexes] + + +## ----vectors-named-01--------------------------------------------------------------------------------------------------- +treat <- c("H", "C", "H", "W", "C", "H", "H", "W", "W") + + +## ----vectors-named-02--------------------------------------------------------------------------------------------------- +treat.map <- c(H = "hot", C = "cold", W = "warm") +treat.map +names(treat.map) + + +## ----vectors-named-03--------------------------------------------------------------------------------------------------- +treat.map["H"] + + +## ----vectors-named-04--------------------------------------------------------------------------------------------------- +treat.new <- treat.map[treat] +treat.new + + +## ----vectors-named-05--------------------------------------------------------------------------------------------------- +treat.new <- unname(treat.new) +treat.new + + +## ----vectors-7---------------------------------------------------------------------------------------------------------- +vct2 <- 1:10 +vct2 +vct2[1] <- 99 +vct2 + +vct2 <- 1:10 +vct2[c(2,4)] <- -99 # recycling +vct2 + +vct2 <- 1:10 +vct2[c(2,4)] <- c(-99, 99) +vct2 + +vct2 <- 1:10 +vct2[TRUE] <- 1 # recycling +vct2 + +vct2 <- 1:10 +vct2 <- 1 # no recycling +vct2 + + +## ----vectors-8---------------------------------------------------------------------------------------------------------- +vct3 <- letters[1:10] +vct3[1:2] <- vct3[2:1] +vct3 + + +## ----vectors-8a, eval=eval_playground----------------------------------------------------------------------------------- +VCT1 <- letters[1:10] +VCT1[5:1] <- VCT1[c(TRUE,FALSE)] +VCT1 + + +## ----vectors-9---------------------------------------------------------------------------------------------------------- +vct4 <- LETTERS[1:10] +vct4 +vct4[1] +vct4[1.1] +vct4[1.9999] # surprise!! +vct4[2] + + +## ----vectors-9a--------------------------------------------------------------------------------------------------------- +vct4 <- LETTERS[1:10] +vct4 +vct4[-1] +vct4[-1.1] +vct4[-1.9999] +vct4[-2] + + +## ----vectors-10--------------------------------------------------------------------------------------------------------- +vct5 <- c(10, 4, 22, 1, 4) +sort(vct5) +sort(vct5, decreasing = TRUE) + + +## ----vectors-11--------------------------------------------------------------------------------------------------------- +order(vct5) +vct5[order(vct5)] +vct6 <- c("ab", "aa", "c", "zy", "e") +vct6[order(vct5)] + + +## ----vectors-EB21------------------------------------------------------------------------------------------------------- +vct7 <- letters[c(1, 5, 10, 3, 1, 4, 21, 1, 10)] +vct7 +sort(vct7) +rle(sort(vct7)) + + +## ----include=FALSE------------------------------------------------------------------------------------------------------ +rm(list = setdiff(ls(pattern="*"), to.keep)) + + +## ----matrix-01---------------------------------------------------------------------------------------------------------- +matrix(1:15, ncol = 3) +matrix(1:15, nrow = 3) + + +## ----matrix-character-01------------------------------------------------------------------------------------------------ +matrix(letters[1:15], nrow = 3) + + +## ----matrix-PG00, eval=FALSE-------------------------------------------------------------------------------------------- +## help(matrix) + + +## ----matrix-PG00a, eval=eval_playground--------------------------------------------------------------------------------- +matrix(1:15) + + +## ----matrix-PG00b, eval=FALSE------------------------------------------------------------------------------------------- +## matrix(1:15, ncol = 2) + + +## ----matrix-10---------------------------------------------------------------------------------------------------------- +mat1 <- matrix(1:20, ncol = 4) +mat1 +mat1[1, 2] +mat1[2, 1] + + +## ----matrix-11---------------------------------------------------------------------------------------------------------- +mat1[1, ] +mat1[ , 1] +mat1[2:3, c(1,3)] +mat1[3, 4] <- 99 +mat1 +mat1[4:3, 2:1] <- mat1[3:4, 1:2] +mat1 + + +## ----matrix-11a--------------------------------------------------------------------------------------------------------- +is.matrix(mat1[1, ]) +is.matrix(mat1[1:2, 1:2]) + + +## ----matrix-11b--------------------------------------------------------------------------------------------------------- +is.vector(mat1[1, ]) +is.vector(mat1[1:2, 1:2]) + + +## ----matrix-11c--------------------------------------------------------------------------------------------------------- +is.matrix(mat1[1, , drop = FALSE]) +is.matrix(mat1[1:2, 1:2, drop = FALSE]) + + +## ----matrix-12---------------------------------------------------------------------------------------------------------- +colnames(mat1) +rownames(mat1) +colnames(mat1) <- c("a", "b", "c", "d") +mat1 +rownames(mat1) <- c("A", "B", "C", "D", "E") +mat1 +mat1[c("E", "A", "D"), c("b", "a")] +colnames(mat1) <- NULL +mat1 + + +## ----matrix-13---------------------------------------------------------------------------------------------------------- +mat1 <- matrix(1:20, ncol = 4) +mat1 +dim(mat1) +mat1[10] +mat1[5, 2] + + +## ----matrix-14---------------------------------------------------------------------------------------------------------- +mat2 <- matrix(1:20, ncol = 4, byrow = TRUE) +mat2 +dim(mat2) +mat2[10] +mat2[5, 2] + + +## ----dimensions-box-01-------------------------------------------------------------------------------------------------- +vct1 <- 1:6 +dim(vct1) + + +## ----dimensions-box-02a------------------------------------------------------------------------------------------------- +one.col.matrix <- matrix(1:6, ncol = 1) +dim(one.col.matrix) + + +## ----dimensions-box-02b------------------------------------------------------------------------------------------------- +two.col.matrix <- matrix(1:6, ncol = 2) +dim(two.col.matrix) + + +## ----dimensions-box-02c------------------------------------------------------------------------------------------------- +one.elem.matrix <- matrix(1, ncol = 1) +dim(one.elem.matrix) + + +## ----dimensions-box-02d------------------------------------------------------------------------------------------------- +no.elem.matrix <- matrix(numeric(), ncol = 0) +dim(no.elem.matrix) + + +## ----matrix-21---------------------------------------------------------------------------------------------------------- +ary1 <- array(1:27, dim = c(3, 3, 3)) +ary1 +ary1[2, 2, 2] + + +## ----matrix-PG01, eval=eval_playground---------------------------------------------------------------------------------- +VCT2 <- 1:10 +MAT1 <- matrix(VCT2, ncol = 2) +MAT2 <- matrix(VCT2, ncol = 2, byrow = TRUE) +MAT3 <- matrix(VCT2, nrow = 2) +MAT4 <- matrix(VCT2, nrow = 2, byrow = TRUE) + + +## ----matrix-PG02, eval=eval_playground---------------------------------------------------------------------------------- +ARY1 <- array(VCT2, dim = c(5, 2)) +ARY2 <- array(VCT2, dim = c(5, 2), dimnames = list(NULL, c("c1", "c2"))) +ARY3 <- array(VCT2, dim = c(2, 5)) + + +## ----matrix-32---------------------------------------------------------------------------------------------------------- +mat3 <- matrix(1:20, ncol = 4) +mat3 + 2 +mat3 * 0:1 +mat3 * 1:0 + + +## ----matrix32a, eval=eval_playground------------------------------------------------------------------------------------ +matrix(rep(1, 6)) * 1:6 + + +## ----matrix-31---------------------------------------------------------------------------------------------------------- +mat3 +t(mat3) + + +## ----matrix-33---------------------------------------------------------------------------------------------------------- +mat4 <- matrix(1:16, ncol = 4) +mat4 * mat4 +mat4 %*% mat4 + + +## ----matrix-34---------------------------------------------------------------------------------------------------------- +mat5 <- diag(4) +mat5 +mat4 %*% mat5 + + +## ----matrix-35---------------------------------------------------------------------------------------------------------- +mat6 <- matrix(c(3, 2, 0, 1, 3, 2, 7, 2, 4), ncol = 3) +solve(mat6) + + +## ----include=FALSE------------------------------------------------------------------------------------------------------ +rm(list = setdiff(ls(pattern="*"), to.keep)) + + +## ----factors-1---------------------------------------------------------------------------------------------------------- +factor(x = c(1, 2, 2, 1, 2, 1, 1)) +ordered(x = c(1, 2, 2, 1, 2, 1, 1)) +factor(x = c(1, 2, 2, 1, 2, 1, 1), ordered = TRUE) + + +## ----factors-bx-01------------------------------------------------------------------------------------------------------ +gl(n = 2, k = 5, labels = c("A", "B")) + + +## ----factors-bx-01a----------------------------------------------------------------------------------------------------- +gl(n = 2, k = 1, length = 10, labels = c("A", "B")) + + +## ----factors-2---------------------------------------------------------------------------------------------------------- +vct1 <- c("treated", "treated", "control", "control", "control", "treated") +factor(vct1) + + +## ----factors-3---------------------------------------------------------------------------------------------------------- +factor(x = vct1, levels = c("treated", "control")) + + +## ----factors-4---------------------------------------------------------------------------------------------------------- +factor(x = c("a", "a", "b", "b", "b", "a"), levels = c("a", "b"), labels = c("treated", "control")) + + +## ----factors-4a--------------------------------------------------------------------------------------------------------- +factor(x = c("a", "a", "b", "b", "b", "a"), labels = c(a = "treated", b = "control")) + + +## ----factors-5---------------------------------------------------------------------------------------------------------- +fct1 <- factor(x = vct1) +fct1 +factor(x = fct1, levels = c("treated", "control")) +factor(x = fct1, labels = c(control = "cooled", treated = "heated")) +factor(x = fct1, + levels = c("treated", "control"), + labels = c("heated", "cooled")) + + +## ----factors-eb3-------------------------------------------------------------------------------------------------------- +fct2 <- gl(4, 3, labels = c("A", "F", "B", "Z")) +fct2 +factor(fct2, + levels = c("A", "B", "F", "Z"), + labels = c("A", "B", "C", "C")) + + +## ----factors-6---------------------------------------------------------------------------------------------------------- +fct1 +levels(fct1) +length(fct1) +fct1.control <- fct1[fct1 == "control"] +fct1.control +levels(fct1.control) # same as in my.factor +length(fct1.control) # shorter than my.factor + + +## ----factor-drop-faq---------------------------------------------------------------------------------------------------- +fct1.control <- factor(fct1.control) +levels(fct1.control) # the unused level was dropped + + +## ----factors-7---------------------------------------------------------------------------------------------------------- +vct3 <- rep(3:5, 4) +vct3 +fct3 <- factor(vct3) +fct3 +as.numeric(fct3) +as.numeric(as.character(fct3)) + + +## ----factors-eb2-------------------------------------------------------------------------------------------------------- +class(fct3) +mode(fct3) +str(fct3) + + +## ----factors-10--------------------------------------------------------------------------------------------------------- +fct4 <- factor(c("treated", "treated", "control", "control", "control", "treated")) +levels(fct4) +fct4 <- factor(fct4, levels = rev(levels(fct4))) +levels(fct4) + + +## ----factors-11--------------------------------------------------------------------------------------------------------- +fct5 <- factor(fct4, + levels = sort(levels(fct4), decreasing = TRUE)) +levels(fct5) + + +## ----factors-12--------------------------------------------------------------------------------------------------------- +fct6 <- factor(fct4, levels = levels(fct4)[c(2, 1)]) +levels(fct6) + + +## ----factors-13--------------------------------------------------------------------------------------------------------- +fct7 <- gl(2, 5, labels = c("A", "B")) +vct4 <- c(5.6, 7.3, 3.1, 8.7, 6.9, 2.4, 4.5, 2.1, 1.4, 2.0) +fct7 +fct7ord <- reorder(fct7, vct4) +levels(fct7ord) +fct7rev <- reorder(fct7, -vct4) # a simple trick: change sign +levels(fct7rev) + + +## ----factors-ADVPG-11a, eval=eval_playground---------------------------------------------------------------------------- +# gl() keeps order of levels +FCT1 <- gl(4, 3, labels = c("A", "F", "B", "Z")) +FCT1 +as.integer(FCT1) + + +## ----factors-ADVPG-11b, eval=eval_playground---------------------------------------------------------------------------- +# factor() orders levels alphabetically +FCT2 <- factor(rep(c("A", "F", "B", "Z"), times = rep(3, times = 4))) # nested calls +FCT2 +as.integer(FCT2) +levels(FCT2)[as.integer(FCT2)] + + +## ----factors-ADVPG-12, eval=eval_playground----------------------------------------------------------------------------- +sort(FCT2) +FCT2[order(FCT2)] +FCT2[order(as.integer(FCT2))] + + +## ----factors-cleanup, include=FALSE------------------------------------------------------------------------------------- +rm(list = setdiff(ls(pattern="*"), to.keep)) + + +## ----echo=FALSE, include=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_fig_wide) +opts_knit$set(concordance=TRUE) +# opts_knit$set(unnamed.chunk.label = 'calculator-chunk') + + +## ----calculator-chapter-cleanup, include=FALSE-------------------------------------------------------------------------- +rm(list = setdiff(ls(pattern="*"), to.keep)) + + +## ----eval=eval_diag, include=eval_diag, echo=eval_diag, cache=FALSE----------------------------------------------------- +## knitter_diag() +## R_diag() +## other_diag() + diff --git a/learn-r-2ed-purl/R.data.R b/learn-r-2ed-purl/R.data.R new file mode 100644 index 00000000..4b3ea222 --- /dev/null +++ b/learn-r-2ed-purl/R.data.R @@ -0,0 +1,474 @@ +## ----echo=FALSE, include=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_fig_wide) +opts_knit$set(concordance=TRUE) +opts_knit$set(unnamed.chunk.label = 'data-chunk') + + +## ----tidyverse-00------------------------------------------------------------------------------------------------------- +tidyverse::tidyverse_packages() + + +## ----eval=FALSE--------------------------------------------------------------------------------------------------------- +## install.packages(learnrbook::pkgs_ch_data) + + +## ----message=FALSE------------------------------------------------------------------------------------------------------ +library(learnrbook) +library(tibble) +library(magrittr) +library(wrapr) +library(stringr) +library(dplyr) +library(tidyr) +library(lubridate) + + +## ----tibble-info-01----------------------------------------------------------------------------------------------------- +my.tb <- tibble(numbers = 1:3) +is_tibble(my.tb) +inherits(my.tb, "tibble") +class(my.tb) + + +## ----tibble-01---------------------------------------------------------------------------------------------------------- +show_classes <- function(x) { + cat( + paste(paste(class(x)[1], + "containing:"), + paste(names(x), + sapply(x, class), collapse = ", ", sep = ": "), + sep = "\n") + ) +} + + +## ----tibble-02---------------------------------------------------------------------------------------------------------- +my.df <- data.frame(codes = c("A", "B", "C"), numbers = -1:1, integers = 1L:3L) +is.data.frame(my.df) +is_tibble(my.df) +show_classes(my.df) + + +## ----tibble-03---------------------------------------------------------------------------------------------------------- +my.tb <- tibble(codes = c("A", "B", "C"), numbers = -1:1, integers = 1L:3L) +is.data.frame(my.tb) +is_tibble(my.tb) +show_classes(my.tb) + + +## ----tibble-04---------------------------------------------------------------------------------------------------------- +print(my.df) +print(my.tb) + + +## ----tibble-print-02---------------------------------------------------------------------------------------------------- +options(tibble.print_max = 3, tibble.print_min = 3) + + +## ----tibble-05---------------------------------------------------------------------------------------------------------- +my_conv.tb <- as_tibble(my.df) +is.data.frame(my_conv.tb) +is_tibble(my_conv.tb) +show_classes(my_conv.tb) + + +## ----tibble-06---------------------------------------------------------------------------------------------------------- +my_conv.df <- as.data.frame(my.tb) +is.data.frame(my_conv.df) +is_tibble(my_conv.df) +show_classes(my_conv.df) + + +## ----tibble-box-01------------------------------------------------------------------------------------------------------ +class(my.tb) +class(my_conv.df) +my.tb == my_conv.df +identical(my.tb, my_conv.df) + + +## ----tibble-box-02------------------------------------------------------------------------------------------------------ +my.xtb <- my.tb +class(my.xtb) <- c("xtb", class(my.xtb)) +class(my.xtb) +my_conv_x.tb <- as_tibble(my.xtb) +class(my_conv_x.tb) +my.xtb == my_conv_x.tb +identical(my.xtb, my_conv_x.tb) + + +## ----tibble-box-03a----------------------------------------------------------------------------------------------------- +class(my.df) +class(my.tb) + + +## ----tibble-box-03b----------------------------------------------------------------------------------------------------- +class(cbind(my.df, my.tb)) +class(cbind(my.tb, my.df)) + + +## ----tibble-box-03c----------------------------------------------------------------------------------------------------- +class(cbind(my.df, added = -3:-1)) +class(cbind(my.tb, added = -3:-1)) +identical(cbind(my.tb, added = -3:-1), cbind(my.df, added = -3:-1)) + + +## ----tibble-07---------------------------------------------------------------------------------------------------------- +tibble(a = 1:5, b = 5:1, c = a + b, d = letters[a + 1]) + + +## ----tibble-08---------------------------------------------------------------------------------------------------------- +tibble(a = 1:5, b = 5:1, c = list("a", 2, 3, 4, 5)) + + +## ----tibble-09---------------------------------------------------------------------------------------------------------- +tibble(a = 1:5, b = 5:1, c = list("a", 1:2, 0:3, letters[1:3], letters[3:1])) + + +## ----pipes-x00---------------------------------------------------------------------------------------------------------- +data.in <- 1:10 + + +## ----pipes-x04---------------------------------------------------------------------------------------------------------- +data.in %>% sqrt() %>% sum() -> data0.out + + +## ----pipes-x04a--------------------------------------------------------------------------------------------------------- +data.in %>% sqrt(x = .) %>% sum(.) -> data1.out +all.equal(data0.out, data1.out) + + +## ----pipes-x04b--------------------------------------------------------------------------------------------------------- +data.in %>% sqrt %>% sum -> data5.out +all.equal(data0.out, data5.out) + + +## ----pipes-x05---------------------------------------------------------------------------------------------------------- +data.in %.>% sqrt(.) %.>% sum(.) -> data2.out +all.equal(data0.out, data2.out) + + +## ----pipes-x05a--------------------------------------------------------------------------------------------------------- +data.in %>% sqrt(.) %>% sum(.) -> data3.out +all.equal(data0.out, data3.out) + + +## ----pipes-x05b--------------------------------------------------------------------------------------------------------- +data.in |> sqrt(x = _) |> sum(x = _) -> data4.out +all.equal(data0.out, data4.out) + + +## ----pipes-x05c--------------------------------------------------------------------------------------------------------- +data.in |> sqrt() |> sum() -> data4.out +all.equal(data0.out, data4.out) + + +## ----pipes-x07---------------------------------------------------------------------------------------------------------- +data.in %.>% (.^2) -> data7.out + + +## ----pipes-x07b--------------------------------------------------------------------------------------------------------- +data.in %>% `^`(e1 = ., e2 = 2) -> data9.out +all.equal(data7.out, data9.out) + + +## ----pipes-x07c--------------------------------------------------------------------------------------------------------- +data.in %.>% (.^2 + sqrt(. + 1)) + + +## ----pipes-x08---------------------------------------------------------------------------------------------------------- +data.in |> print() |> sqrt() |> print() |> sum() |> print() -> data10.out +data10.out + + +## ----pipes-clean, include=FALSE----------------------------------------------------------------------------------------- +rm(data6.out, data7.out, data8.out) + + +## ----pipes-x06---------------------------------------------------------------------------------------------------------- +data.in |> assign(x = "data6.out", value = _) +all.equal(data.in, data6.out) + + +## ----pipes-x06a--------------------------------------------------------------------------------------------------------- +data.in %.>% assign(x = "data7.out", value = .) +all.equal(data.in, data7.out) + + +## ----pipes-x06b--------------------------------------------------------------------------------------------------------- +data.in %>% assign(x = "data8.out", value = .) +if (exists("data8.out")) { + all.equal(data.in, data8.out) +} else { + print("'data8.out' not found!") +} + + +## ----tidy-tibble-00----------------------------------------------------------------------------------------------------- +iris.tb <- as_tibble(iris) + + +## ----tidy-tibble-01----------------------------------------------------------------------------------------------------- +long_iris.tb <- + pivot_longer(iris.tb, + cols = -Species, + names_to = "part", + values_to = "dimension") +long_iris.tb + + +## ----tidy-tibble-01a---------------------------------------------------------------------------------------------------- +wide_iris.tb <- + pivot_wider(long_iris.tb, + names_from = "part", + values_from = "dimension", + values_fn = list) |> + unnest(cols = -Species) +wide_iris.tb + + +## ----tidy-tibble-pivot-pg01, eval=eval_playground----------------------------------------------------------------------- +identical(iris.tb, wide_iris.tb) +all.equal(iris.tb, wide_iris.tb) +all.equal(iris.tb, wide_iris.tb[ , colnames(iris.tb)]) + + +## ----tidy-tibble-01c, eval=eval_playground------------------------------------------------------------------------------ +poor_long_iris.tb <- + poorman::pivot_longer( + iris, + cols = -Species, + names_to = "part", + values_to = "dimension") +identical(long_iris.tb, poor_long_iris.tb) +all.equal(long_iris.tb, poor_long_iris.tb) +class(long_iris.tb) +class(poor_long_iris.tb) + + +## ----tidy-tibble-02z---------------------------------------------------------------------------------------------------- +tibble(a = 1:5, b = 2 * a) + + +## ----tidy-tibble-02----------------------------------------------------------------------------------------------------- +long_iris.tb |> + mutate(plant_part = str_extract(part, "^[:alpha:]*"), + part_dimension = str_extract(part, "[:alpha:]*$")) -> long_iris.tb +long_iris.tb + + +## ----tidy-tibble-03----------------------------------------------------------------------------------------------------- +arrange(long_iris.tb, Species, plant_part, part_dimension) + + +## ----tidy-tibble-04----------------------------------------------------------------------------------------------------- +filter(long_iris.tb, plant_part == "Petal") + + +## ----tidy-tibble-05----------------------------------------------------------------------------------------------------- +slice(long_iris.tb, 1:5) + + +## ----tidy-tibble-06----------------------------------------------------------------------------------------------------- +select(long_iris.tb, -part) + + +## ----tidy-tibble-06a---------------------------------------------------------------------------------------------------- +select(iris.tb, -starts_with("Sepal")) + + +## ----tidy-tibble-06b---------------------------------------------------------------------------------------------------- +select(iris.tb, Species, matches("pal")) + + +## ----tidy-tibble-07----------------------------------------------------------------------------------------------------- +long_iris.tb |> +select(-part) |> +rename(part = plant_part, size = dimension, dimension = part_dimension) + + +## ----tibble-grouped-01-------------------------------------------------------------------------------------------------- +tibble(numbers = 1:9, Letters = rep(letters[1:3], 3)) |> + group_by(Letters) |> + summarise(mean_num = mean(numbers), + median_num = median(numbers), + n = n()) |> + ungroup() # not always needed but safer + + +## ----tibble-grouped-02-------------------------------------------------------------------------------------------------- +tibble(numbers = 1:9, Letters = rep(letters[1:3], 3)) |> + summarise(.by = Letters, + mean_num = mean(numbers), + median_num = median(numbers), + n = n()) + + +## ----tibble-grouped-box-01---------------------------------------------------------------------------------------------- +my.tb <- tibble(numbers = 1:9, Letters = rep(letters[1:3], 3)) +is.grouped_df(my.tb) +class(my.tb) +names(attributes(my.tb)) + + +## ----tibble-grouped-box-02---------------------------------------------------------------------------------------------- +my_gr.tb <- group_by(.data = my.tb, Letters) +is.grouped_df(my_gr.tb) +class(my_gr.tb) + + +## ----tibble-grouped-box-02a--------------------------------------------------------------------------------------------- +names(attributes(my_gr.tb)) +setdiff(attributes(my_gr.tb), attributes(my.tb)) + + +## ----tibble-grouped-box-03---------------------------------------------------------------------------------------------- +my_ugr.tb <- ungroup(my_gr.tb) +class(my_ugr.tb) +names(attributes(my_ugr.tb)) + + +## ----tibble-grouped-box-04---------------------------------------------------------------------------------------------- +all(my.tb == my_gr.tb) +all(my.tb == my_ugr.tb) +identical(my.tb, my_gr.tb) +identical(my.tb, my_ugr.tb) + + +## ----tibble-print-10, echo=FALSE---------------------------------------------------------------------------------------- +options(tibble.print_max = 6, tibble.print_min = 6) + + +## ----joins-00----------------------------------------------------------------------------------------------------------- +first.tb <- tibble(idx = c(1:4, 5), values1 = "a") +second.tb <- tibble(idx = c(1:4, 6), values2 = "b") + + +## ----joins-01----------------------------------------------------------------------------------------------------------- +full_join(x = first.tb, y = second.tb) + + +## ----joins-01a---------------------------------------------------------------------------------------------------------- +full_join(x = second.tb, y = first.tb) + + +## ----joins-02----------------------------------------------------------------------------------------------------------- +left_join(x = first.tb, y = second.tb) + + +## ----joins-02a---------------------------------------------------------------------------------------------------------- +left_join(x = second.tb, y = first.tb) + + +## ----joins-03----------------------------------------------------------------------------------------------------------- +right_join(x = first.tb, y = second.tb) + + +## ----joins-03a---------------------------------------------------------------------------------------------------------- +right_join(x = second.tb, y = first.tb) + + +## ----joins-04----------------------------------------------------------------------------------------------------------- +inner_join(x = first.tb, y = second.tb) + + +## ----joins-04a---------------------------------------------------------------------------------------------------------- +inner_join(x = second.tb, y = first.tb) + + +## ----joins-05----------------------------------------------------------------------------------------------------------- +semi_join(x = first.tb, y = second.tb) + + +## ----joins-05a---------------------------------------------------------------------------------------------------------- +semi_join(x = second.tb, y = first.tb) + + +## ----joins-06----------------------------------------------------------------------------------------------------------- +anti_join(x = first.tb, y = second.tb) + + +## ----joins-06a---------------------------------------------------------------------------------------------------------- +anti_join(x = second.tb, y = first.tb) + + +## ----joins-01b---------------------------------------------------------------------------------------------------------- +first2.tb <- rename(first.tb, idx2 = idx) +full_join(x = first2.tb, y = second.tb, by = c("idx2" = "idx")) + + +## ----tibble-print-11, echo=FALSE---------------------------------------------------------------------------------------- +options(tibble.print_max = 3, tibble.print_min = 3) + + +## ----lubridate-01------------------------------------------------------------------------------------------------------- +this.day <- today() +class(this.day) +as.POSIXct(this.day, tz = "") # local time zone + + +## ----lubridate-02------------------------------------------------------------------------------------------------------- +this.instant <- now() +class(this.instant) +this.instant + + +## ----lubridate-03------------------------------------------------------------------------------------------------------- +dmy_h("04/10/23 15", tz = "EET") +dmy_h("04/10/23 3pm", tz = "EET") +dmy_h("04/10/23 15 EET") # Wrong decoding! + + +## ----lubridate-04------------------------------------------------------------------------------------------------------- +class(ymd("2023-10-04")) +class(ymd("2023-10-04", tz = "")) +class(today(tzone = "")) + + +## ----lubridate-05------------------------------------------------------------------------------------------------------- +as.POSIXct(ymd("2023-10-04"), tzone = "") - ymd("2023-10-04", tz = "") + + +## ----lubridate-05a------------------------------------------------------------------------------------------------------ +force_tz(as.POSIXct(ymd("2023-10-04")), tzone = "") - ymd("2023-10-04", tz = "") + + +## ----lubridate-06------------------------------------------------------------------------------------------------------- +ymd_hms("2010-05-25 12:05:00") - ymd_hms("1810-05-25 12:00:00") + + +## ----lubridate-07------------------------------------------------------------------------------------------------------- +ymd_hms("1810-05-25 12:00:00") + years(200) + minutes(5) +ymd_hms("2010-05-25 12:05:00") - ymd_hms("1810-05-25 12:00:00") +ymd("2023-01-01") + seconds(123) + + +## ----lubridate-08------------------------------------------------------------------------------------------------------- +my.time <- now() +my.time +year(my.time) +hour(my.time) +second(my.time) +second(my.time) <- 0 + + +## ----lubridate-09------------------------------------------------------------------------------------------------------- +trunc(my.time, "days") +round(my.time, "hours") + + +## ----echo=FALSE--------------------------------------------------------------------------------------------------------- +try(detach(package:lubridate)) +try(detach(package:tidyr)) +try(detach(package:dplyr)) +try(detach(package:stringr)) +try(detach(package:wrapr)) +try(detach(package:magrittr)) +try(detach(package:tibble)) +try(detach(package:learnrbook)) + + +## ----eval=eval_diag, include=eval_diag, echo=eval_diag, cache=FALSE----------------------------------------------------- +## knitter_diag() +## R_diag() +## other_diag() + diff --git a/learn-r-2ed-purl/R.data.containers.R b/learn-r-2ed-purl/R.data.containers.R new file mode 100644 index 00000000..8ff63f0e --- /dev/null +++ b/learn-r-2ed-purl/R.data.containers.R @@ -0,0 +1,719 @@ +## ----echo=FALSE, include=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_fig_wide) +opts_knit$set(concordance=TRUE) +opts_knit$set(unnamed.chunk.label = 'container-chunk') + + +## ----lists-0------------------------------------------------------------------------------------------------------------ +lst1 <- list(x = 1:3, y = "ab", z = c(TRUE, FALSE)) + + +## ----lists-0a----------------------------------------------------------------------------------------------------------- +str(lst1) +names(lst1) + + +## ----list-empty-faq----------------------------------------------------------------------------------------------------- +list() + + +## ----lists-1a----------------------------------------------------------------------------------------------------------- +lst1["x"] +lst1[1] +lst1[c(1, 3)] + + +## ----lists-1ay---------------------------------------------------------------------------------------------------------- +lst1[-1] +lst1[c(-1, -3)] + + +## ----lists-1------------------------------------------------------------------------------------------------------------ +lst1$x +lst1[["x"]] +lst1[[1]] + + +## ----index-partial-1---------------------------------------------------------------------------------------------------- +lst1a <- list(abcd = 123, xyzw = 789) +lst1a[[1]] +lst1a[["abcd"]] +vct1 <- "abcd" +lst1a[[vct1]] + + +## ----index-partial-1a--------------------------------------------------------------------------------------------------- +lst1a$abcd +lst1a$ab +lst1a$a + + +## ----lists-1az---------------------------------------------------------------------------------------------------------- +is.vector(lst1[1]) +is.list(lst1[1]) +is.vector(lst1[[1]]) +is.list(lst1[[1]]) + + +## ----lists-1ax---------------------------------------------------------------------------------------------------------- +lst1[[1]][3] + + +## ----lists-pg-01, eval=eval_playground---------------------------------------------------------------------------------- +lst2 <- list() +lst2[["x"]] <- 1:3 +lst2[["y"]] <- "ab" +lst2[["z"]] <- c(TRUE, FALSE) + + +## ----lists-pg-02, eval=eval_playground---------------------------------------------------------------------------------- +lst2[["y"]] <- lst2[["x"]] + + +## ----lists-eb-xx>------------------------------------------------------------------------------------------------------- +list(a = 1, b = 2, c = 3)[-2] + + +## ----lists-1b----------------------------------------------------------------------------------------------------------- +lst3 <- append(lst1, list(yy = 1:10, zz = letters[5:1]), after = 2) +lst3 + + +## ----lists-1c----------------------------------------------------------------------------------------------------------- +lst1$y <- NULL +lst1 + + +## ----lists-1aa---------------------------------------------------------------------------------------------------------- +print(lst1) +str(lst1) + + +## ----lists-2------------------------------------------------------------------------------------------------------------ +lst4 <- list("a", "aa", 10) +lst5 <- list("b", TRUE) +lst6 <- list(A = lst4, B = lst5) # nested +str(lst6) + + +## ----lists-3------------------------------------------------------------------------------------------------------------ +lst7 <- list(A = list("a", "aa", 10), B = list("b", TRUE)) +str(lst7) + + +## ----lists-3s----------------------------------------------------------------------------------------------------------- +lst8 <- list(A = list("a", "aa", 10), + B = list("b", TRUE), + C = c(1, 3, 9), + D = 4321) +str(lst8) + + +## ----lists-PG4, eval=eval_playground------------------------------------------------------------------------------------ +LST9 <- list(A = list("a", "aa", "aaa"), B = list("b", "bb")) +# str(LST9) +LST9[2:1] +LST9[1] +LST9[[1]][2] +LST9[[1]][[2]] +LST9[2] +LST9[2][[1]] + + +## ----lists-EB1b--------------------------------------------------------------------------------------------------------- +str(lst8, max.level = 1) + + +## ----lists-5------------------------------------------------------------------------------------------------------------ +lst10 <- list(A = list("a", "aa", "aaa"), B = list("b", "bb")) +vct1 <- unlist(lst10) +vct1 +is.list(lst10) +is.list(vct1) +mode(lst10) +mode(vct1) +names(lst10) +names(vct1) + + +## ----lists-6------------------------------------------------------------------------------------------------------------ +str(vct1) +vct1[2] +vct1["A2"] + + +## ----lists-7------------------------------------------------------------------------------------------------------------ +unname(vct1) +unname(lst10) + + +## ----lists-cleanup, include=FALSE--------------------------------------------------------------------------------------- +rm(list = setdiff(ls(pattern="*"), to.keep)) + + +## ----data-frames-0------------------------------------------------------------------------------------------------------ +df1 <- data.frame(treatment = factor(rep(c("hot", "cold"), 3)), + height = c(10.2, 8.3, 12.0, 9.0, 11.2, 8.7), + weight = c(2.2, 3.3, 2.5, 2.8, 2.4, 3.0)) +df1 +colnames(df1) +rownames(df1) +str(df1) +class(df1) +mode(df1) +is.data.frame(df1) +is.list(df1) + + +## ----data-frames-0a----------------------------------------------------------------------------------------------------- +df2 <- data.frame(treatment = factor(c("hot", "cold")), + height = c(10.2, 8.3, 12.0, 9.0, 11.2, 8.7), + weight = c(2.2, 3.3, 2.5, 2.8, 2.4, 3.0)) + + +## ----data-frames-1------------------------------------------------------------------------------------------------------ +df1$height +df1[["height"]] +df1[[2]] +class(df1[["height"]]) + + +## ----data-frames-2------------------------------------------------------------------------------------------------------ +df1$x2 <- 6:1 +df1[["x3"]] <- "b" +str(df1) + + +## ----data-frames-2a----------------------------------------------------------------------------------------------------- +LST1 <- list(x = 1:6, y = "a", z = c(TRUE, FALSE)) +str(LST1) +LST1$x2 <- 6:1 +LST1$x3 <- "b" +str(LST1) + + +## ----data-frame-empty-faq----------------------------------------------------------------------------------------------- +data.frame() + + +## ----data-frame-listof-faq---------------------------------------------------------------------------------------------- +list(df1, df2) + + +## ----data-frame-add-co1l-faq-------------------------------------------------------------------------------------------- +DF1 <- data.frame(A = 1:5, B = factor(5:1)) +DF1[["C"]] <- 11:15 +DF1 + + +## ----data-frame-add-col2-faq-------------------------------------------------------------------------------------------- +DF2 <- data.frame(A = 1:5, B = factor(5:1)) +cbind(C = 11:15, DF2) + + +## ----data-frames-bx-03-------------------------------------------------------------------------------------------------- +df1[2:3, 1:2] + + +## ----data-frames-3------------------------------------------------------------------------------------------------------ +# first column, df1[[1]] preferred +df1[ , 1] +# first column, df1[["x"]] or df1$x preferred +df1[ , "treatment"] +# first row +df1[1, ] +# first two rows of the third and fourth columns +df1[1:2, c(FALSE, FALSE, TRUE, TRUE, FALSE)] +# the rows for which comparison is true +df1[df1$treatment == "hot" , ] +# the heights > 8 +df1[df1$height > 8, "height"] + + +## ----data-frames-3a----------------------------------------------------------------------------------------------------- +df1[1, 2] <- 99 +df1 +df1[ , 2] <- -99 +df1 +df1[["height"]] <- c(10, 12) +df1 +df1[1, 2] <- df1[6, 3] +df1 +df1[3:6, 2] <- df1[6, 3] +df1 + + +## ----data-frames-2b----------------------------------------------------------------------------------------------------- +is.data.frame(df1[1, ]) +is.data.frame(df1[ , 2]) +is.data.frame(df1[ , "treatment"]) +is.data.frame(df1[1:2, 2:3]) +is.vector(df1[1, ]) +is.vector(df1[ , 2]) +is.factor(df1[ , "treatment"]) +is.vector(df1[1:2, 2:3]) + + +## ----data-frames-2bb---------------------------------------------------------------------------------------------------- +is.data.frame(df1[ , 1, drop = FALSE]) +is.data.frame(df1[ , "treatment", drop = FALSE]) + + +## ----data-frames-bx-constr-01------------------------------------------------------------------------------------------- +vct1 <- c(one = 1, two = 2, three = 3, four = 4) +fct1 <- as.factor(c(1, 2, 3, 2)) +df1 <- data.frame(fct1, vct1) +df1 +df1$vct1 + + +## ----data-frames-bx-constr-02------------------------------------------------------------------------------------------- +df2 <- data.frame(fct1, I(vct1)) +df2 +df2$vct1 + + +## ----data-frames-bx-constr-04------------------------------------------------------------------------------------------- +mat1 <- matrix(1:12, ncol = 3) +df4 <- data.frame(fct1, mat1) + + +## ----data-frames-bx-constr-04a------------------------------------------------------------------------------------------ +df4 + + +## ----data-frames-bx-constr-05------------------------------------------------------------------------------------------- +df5 <- data.frame(fct1, I(mat1)) +df5 +df5$mat1 + + +## ----data-frames-bx-constr-06------------------------------------------------------------------------------------------- +lst1 <- list(a = 4:1, b = letters[4:1], c = "n", d = "z") +df6<- data.frame(fct1, lst1) +df6 + + +## ----data-frames-bx-constr-07------------------------------------------------------------------------------------------- +df7<- data.frame(fct1, I(lst1)) +df7 + +## ----data-frames-bx-constr-07b------------------------------------------------------------------------------------------ +df7$lst1 + + +## ----data-frames-4------------------------------------------------------------------------------------------------------ +df8 <- data.frame(x = 1:6, y = "a", z = c(TRUE, FALSE)) +subset(df8, x > 3) + + +## ----data-frames-5------------------------------------------------------------------------------------------------------ +subset(df8, x > 3)[ , -3] +subset(df8, x > 3)[ , "x", drop = FALSE] +subset(df8, x > 3)[ , "x"] + + +## ----data-frames-5aa---------------------------------------------------------------------------------------------------- +subset(df8, x > 3, select = 2) + + +## ----data-frames-5ab---------------------------------------------------------------------------------------------------- +subset(df8, x > 3, select = x) + + +## ----data-frames-5ac---------------------------------------------------------------------------------------------------- +subset(df8, x > 3, select = "x") + + +## ----data-frames-5b----------------------------------------------------------------------------------------------------- +A <- 1 +subset(df8, A > 3) + + +## ----data-frames-5c----------------------------------------------------------------------------------------------------- +columns <- c("x", "z") +subset(df8, select = columns) + + +## ----data-frames-6------------------------------------------------------------------------------------------------------ +df9 <- df8 +head(df9) +df9[["y"]] <- NULL +head(df9) + + +## ----data-frames-6a----------------------------------------------------------------------------------------------------- +df8[ , -which(colnames(df8) == "y")] + + +## ----data-frames-7, eval=eval_playground-------------------------------------------------------------------------------- +df8[1:6, c(1,3)] <- df8[6:1, c(3,1)] +df8 + + +## ----data-frames-7aaa--------------------------------------------------------------------------------------------------- +summary(df8) + + +## ----data-frames-7aa---------------------------------------------------------------------------------------------------- +df10 <- data.frame(x1 = 1:6, x2 = c(1, 5, 4, 2, 6, 3), z = c("a", "b")) + + +## ----data-frames-7a----------------------------------------------------------------------------------------------------- +split(df10, df10$z) + + +## ----data-frames-7c----------------------------------------------------------------------------------------------------- +split(df10, ~ z) + + +## ----faq-aggregate-01--------------------------------------------------------------------------------------------------- +aggregate(x = iris$Petal.Length, + by = list(iris$Species), FUN = mean) + + +## ----faq-aggregate-02--------------------------------------------------------------------------------------------------- +aggregate(x = iris[ , sapply(iris, is.numeric)], + by = list(iris$Species), FUN = mean) + + +## ----data-frames-7d----------------------------------------------------------------------------------------------------- +aggregate(x1 ~ z, FUN = mean, data = df10) + + +## ----data-frames-7e----------------------------------------------------------------------------------------------------- +aggregate(cbind(x1, x2) ~ z, FUN = mean, data = df10) + + +## ----data-frames-7f----------------------------------------------------------------------------------------------------- +aggregate(. ~ z, FUN = mean, data = df10) + + +## ----data-frames-8------------------------------------------------------------------------------------------------------ +df11 <- data.frame(A = 1:10, B = 3, C = c("A", "B")) +head(df11, 2) +df11 <- df11[ , c("B", "A", "C")] +head(df11, 2) + + +## ----data-frames-8ax---------------------------------------------------------------------------------------------------- +df11 <- data.frame(A = 1:10, B = 3, C = c("A", "B")) +head(df11, 2) +df11[ , 1:2] <- df11[ , 2:1] +head(df11, 2) +colnames(df11)[1:2] <- colnames(df11)[2:1] +head(df11, 2) + + +## ----faq-data-frames-01------------------------------------------------------------------------------------------------- +sorted_cols_iris <- iris[ , sort(colnames(iris), decreasing = TRUE)] +head(sorted_cols_iris, 5) + + +## ----faq-data-frames-02------------------------------------------------------------------------------------------------- +sorted_rows_iris <- iris[order(iris$Petal.Length), ] +head(sorted_rows_iris, 5) + + +## ----data-frames-9------------------------------------------------------------------------------------------------------ +df12 <- + data.frame(genotype = rep(c("WT", "mutant1", "mutant2"), 2), + value = c(1.5, 3.2, 4.5, 8.2, 7.4, 6.2)) +mutant <- c(WT = FALSE, mutant1 = TRUE, mutant2 = TRUE) +df12$mutant <- mutant[df12$genotype] +df12 + + +## ----data-frames-10----------------------------------------------------------------------------------------------------- +df13 <- data.frame(individual = rep(1:3, 2), + value = c(1.5, 3.2, 4.5, 8.2, 7.4, 6.2)) +genotype <- c("WT", "mutant1", "mutant2") +df13$genotype <- genotype[df13$individual] +df13 + + +## ----data-frames-11, eval=eval_playground------------------------------------------------------------------------------- +DF1 <- data.frame(individual = rep(c(2, 4, 1), 2), + value = c(1.5, 3.2, 4.5, 8.2, 7.4, 6.2)) + + +## ----data-frames-EB-12-------------------------------------------------------------------------------------------------- +df14 <- data.frame(A = 1:10, B = 3) +df14$C <- with(df14, (A + B) / A) # add column +head(df14, 3) + + +## ----data-frames-EB-13-------------------------------------------------------------------------------------------------- +df14$C <- NULL +df15 <- within(df14, C <- (A + B) / A) # midified copy +head(df15, 3) + + +## ----data-frames-EB-14-------------------------------------------------------------------------------------------------- +df16 <- within(df14, + {C <- (A + B) / A + D <- A * B + E <- A / B + 1} + ) +head(df16, 3) + + +## ----data-frames-EB-10-------------------------------------------------------------------------------------------------- +df14$C <- (df14$A + df14$B) / df14$A +df14$D <- df14$A * df14$B +df14$D <- df14$A / df14$B + 1 +head(df14, 3) + + +## ----data-frames-EB-11a------------------------------------------------------------------------------------------------- +df17 <- data.frame(A = 1:10, B = 3) +A +attach(df17) +A +detach(df17) +A + + +## ----data-frames-EB-11-------------------------------------------------------------------------------------------------- +attach(df17) +df17$C <- (A + B) / A +detach(df17) +head(df17, 2) + + +## ----include=FALSE------------------------------------------------------------------------------------------------------ +rm(list = setdiff(ls(pattern="*"), to.keep)) + + +## ----data-frames-reshape-01--------------------------------------------------------------------------------------------- +# artifical data +df1 <- data.frame(id = rep(1:4, rep(2,4)), + Time = factor(rep(c("Before","After"), 4)), + Weight = rnorm(n = 4, mean = c(20.1, 30.8)), + Height = rnorm(n = 4, mean = c(9.5, 14.2))) +df1 +# make it wider +df2 <- reshape(df1, timevar = "Time", idvar = "id", direction = "wide") +df2 +# possible further calculation +within(df2, + { + Height.growth <- Height.After - Height.Before + Weight.growth <- Weight.After - Weight.Before + }) + + +## ----data-frames-reshape-02--------------------------------------------------------------------------------------------- +df1$ID <- rownames(df1) # unique ID for each row +# make it longer +reshape(df1, + idvar = "ID", + timevar = "Quantity", + times = c("Weight", "Height"), + v.names = "Value", + direction = "long", + varying = c("Weight", "Height")) + + +## ----exploring-dfs-0a, eval=FALSE--------------------------------------------------------------------------------------- +## View(cars) +## edit(cars) + + +## ----echo=FALSE,cache=FALSE--------------------------------------------------------------------------------------------- +rm(list = setdiff(ls(pattern="*"), to.keep)) + + +## ----attributes-00------------------------------------------------------------------------------------------------------ +df1 <- data.frame(x = 1:6, y = c("a", "b"), z = c(TRUE, FALSE, NA)) +df1 +attributes(df1) +str(df1) + + +## ----attributes-01------------------------------------------------------------------------------------------------------ +comment(df1) +comment(df1) <- "this is stored as a comment" +comment(df1) + + +## ----attributes-01a----------------------------------------------------------------------------------------------------- +attr(df1, "comment") +attr(df1, "comment") <- NULL +attr(df1, "comment") +comment(df1) # same as previous line + + +## ----attributes-02------------------------------------------------------------------------------------------------------ +names(df1) +colnames(df1) # same as names() +colnames(df1) <- toupper(colnames(df1)) +colnames(df1) +attr(df1, "names") # same as previous line + + +## ----attributes-names-ebx-01, eval=eval_playground---------------------------------------------------------------------- +VCT1 <- 5:10 +names(VCT1) +labels(VCT1) +names(VCT1) <- letters[5:10] +names(VCT1) +labels(VCT1) + + +## ----attibutes-names-ebx-02, eval=eval_playground----------------------------------------------------------------------- +MAT1 <- matrix(1:10, ncol = 2) +dimnames(MAT1) +labels(MAT1) +colnames(MAT1) <- c("a", "b") +colnames(MAT1) +dimnames(MAT1) +labels(MAT1) + + +## ----attributes-02a----------------------------------------------------------------------------------------------------- +attr(df1, "my.attribute") <- "this is stored in my attribute" +attributes(df1) + + +## ----attibutes-ebx-01a-------------------------------------------------------------------------------------------------- +mat1 <- matrix(1:10, ncol = 2) +attributes(mat1) +dim(mat1) +dimnames(mat1) + + +## ----attibutes-ebx-01aa------------------------------------------------------------------------------------------------- +labels(mat1) +mat1 + + +## ----attibutes-ebx-01b-------------------------------------------------------------------------------------------------- +attr(mat1, "dim") +attr(mat1, "dim") <- c(2, 5) +mat1 + + +## ----attibutes-ebx-01c-------------------------------------------------------------------------------------------------- +attr(mat1, "dim") <- NULL +is.vector(mat1 ) +mat1 + + +## ----attibutes-ebx-01d-------------------------------------------------------------------------------------------------- +dim(mat1) <- NULL +is.vector(mat1 ) + + +## ----include=FALSE------------------------------------------------------------------------------------------------------ +rm(list = setdiff(ls(pattern="*"), to.keep)) + + +## ----data-1------------------------------------------------------------------------------------------------------------- +data(cars) + + +## ----rda-01------------------------------------------------------------------------------------------------------------- +df1 <- data.frame(x = 1:5, y = 5:1) +df1 +save(df1, file = "df1.rda") + + +## ----rda-02------------------------------------------------------------------------------------------------------------- +remove(df1) +objects(pattern = "df1") + + +## ----rda-03------------------------------------------------------------------------------------------------------------- +load(file = "df1.rda") +objects(pattern = "df1") +df1 + + +## ----rda-04------------------------------------------------------------------------------------------------------------- +dfs <- objects(pattern = "*.df") +save(list = dfs, file = "my-dfs.rda") + + +## ----rda-05------------------------------------------------------------------------------------------------------------- +save(list = objects(pattern = "*.df"), file = "my-dfs.rda") + + +## ----rda-06------------------------------------------------------------------------------------------------------------- +file.remove(c("my-dfs.rda", "df1.rda")) + + +## ----rds-1-------------------------------------------------------------------------------------------------------------- +saveRDS(df1, "df1.rds") + + +## ----rds-1a------------------------------------------------------------------------------------------------------------- +readRDS("df1.rds") + + +## ----rds-2-------------------------------------------------------------------------------------------------------------- +df2 <- readRDS("df1.rds") +identical(df1, df2) + + +## ----rds-03------------------------------------------------------------------------------------------------------------- +file.remove("df1.rds") + + +## ----dput-01------------------------------------------------------------------------------------------------------------ +dput(df1) + + +## ----include=FALSE------------------------------------------------------------------------------------------------------ +rm(list = setdiff(ls(pattern="*"), to.keep)) + + +## ----plot-0, include=FALSE, cache=FALSE--------------------------------------------------------------------------------- +opts_chunk$set(opts_fig_narrow_square) + + +## ----plot-1a, eval=FALSE------------------------------------------------------------------------------------------------ +## plot(x = cars$speed, y = cars$dist) + + +## ----plot-1b, eval=FALSE------------------------------------------------------------------------------------------------ +## with(cars, plot(x = speed, y = dist)) + + +## ----plot-1c------------------------------------------------------------------------------------------------------------ +plot(dist ~ speed, data = cars) + + +## ----plot-3------------------------------------------------------------------------------------------------------------- +plot(weight ~ feed, data = chickwts) + + +## ----gr-devices-01, message=FALSE--------------------------------------------------------------------------------------- +pdf(file = "output/my-file.pdf", width = 6, height = 5, onefile = TRUE) +plot(dist ~ speed, data = cars) +plot(weight ~ feed, data = chickwts) +dev.off() + + +## ----gr-devices-02, message=FALSE--------------------------------------------------------------------------------------- +png(file = "output/my-file.png", width = 600, height = 500) +plot(weight ~ feed, data = chickwts) +dev.off() + + +## ----gr-devices-03, message=FALSE--------------------------------------------------------------------------------------- +png(file = "output/my-file.png", width = 600, height = 500) +plot(dist ~ speed, data = cars) +text(x = 10, y = 110, labels = "some texts to be added") +dev.off() + + +## ----container-chapter-cleanup, include=FALSE--------------------------------------------------------------------------- +rm(list = setdiff(ls(pattern="*"), to.keep)) + + +## ----eval=eval_diag, include=eval_diag, echo=eval_diag, cache=FALSE----------------------------------------------------- +## knitter_diag() +## R_diag() +## other_diag() + diff --git a/learn-r-2ed-purl/R.data.io.R b/learn-r-2ed-purl/R.data.io.R new file mode 100644 index 00000000..2f48d002 --- /dev/null +++ b/learn-r-2ed-purl/R.data.io.R @@ -0,0 +1,609 @@ +## ----echo=FALSE, include=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_fig_wide) +opts_knit$set(concordance=TRUE) +opts_knit$set(unnamed.chunk.label = 'data-chunk') + + +## ----echo=FALSE--------------------------------------------------------------------------------------------------------- +# set to TRUE to execute code chunks that require internet access and instruments, repectively +eval_online_data <- TRUE +eval_yoctopuce <- TRUE + + +## ----eval=FALSE--------------------------------------------------------------------------------------------------------- +## install.packages(learnrbook::pkgs_ch10_2ed) + + +## ----message=FALSE------------------------------------------------------------------------------------------------------ +library(learnrbook) +library(tibble) +library(purrr) +library(stringr) +library(dplyr) +library(tidyr) +library(readr) +library(readxl) +library(xlsx) +library(readODS) +library(pdftools) +library(foreign) +library(haven) +library(xml2) +library(XML) +library(ncdf4) +library(tidync) +library(lubridate) +library(jsonlite) + + +## ----copy-data-files---------------------------------------------------------------------------------------------------- +pkg.path <- system.file("extdata", package = "learnrbook") +file.copy(pkg.path, ".", overwrite = TRUE, recursive = TRUE) + + +## ----make-dir----------------------------------------------------------------------------------------------------------- +save.path = "./data" +if (!dir.exists(save.path)) { + dir.create(save.path) +} + + +## ----filenames-01------------------------------------------------------------------------------------------------------- +basename("extdata/my-file.txt") + + +## ----filenames-02------------------------------------------------------------------------------------------------------- +basename("extdata/my-file.txt") +basename("extdata\\my-file.txt") + + +## ----filenames-03------------------------------------------------------------------------------------------------------- +dirname("extdata/my-file.txt") + + +## ----filenames-05,eval=FALSE-------------------------------------------------------------------------------------------- +## # not run +## getwd() + + +## ----filenames-06,eval=FALSE-------------------------------------------------------------------------------------------- +## # not run +## oldwd <- setwd("..") +## getwd() + + +## ----filenames-07,eval=FALSE-------------------------------------------------------------------------------------------- +## # not run +## oldwd +## setwd(oldwd) +## getwd() + + +## ----filenames-09------------------------------------------------------------------------------------------------------- +head(list.files()) +head(list.dirs()) + + +## ----filenames-08------------------------------------------------------------------------------------------------------- +if (!file.exists("xxx.txt")) { + file.create("xxx.txt") +} +file.size("xxx.txt") +file.info("xxx.txt") +file.rename("xxx.txt", "zzz.txt") +file.exists("xxx.txt") +file.exists("zzz.txt") +file.remove("zzz.txt") + + +## ----file-open-01------------------------------------------------------------------------------------------------------- +f1 <- file("extdata/not-aligned-ASCII-UK.csv", open = "r") # open for reading +readLines(f1, n = 1) + + +## ----file-open-02------------------------------------------------------------------------------------------------------- +readLines(f1, n = 2) +close(f1) + + +## ----file-io-txt-00a---------------------------------------------------------------------------------------------------- +data.frame(a = 1, "a " = 2, " a" = 3, check.names = FALSE) +data.frame(a = 1, "a " = 2, " a" = 3) + + +## ----file-io-txt-00b---------------------------------------------------------------------------------------------------- +data.frame(al = 1, a1 = 2, aO = 3, a0 = 4) + + +## ----file-io-csv-00a, comment='', echo=FALSE---------------------------------------------------------------------------- +cat(readLines("extdata/not-aligned-ASCII-UK.csv"), sep = "\n") + + +## ----file-io-csv-01----------------------------------------------------------------------------------------------------- +from_csv_a.df <- + read.csv("extdata/not-aligned-ASCII-UK.csv", stringsAsFactors = FALSE) + + +## ----file-io-csv-02----------------------------------------------------------------------------------------------------- +from_csv_a.df +from_csv_a.df[["col4"]] +sapply(from_csv_a.df, class) + + +## ----file-io-csv-03, comment='', echo=FALSE----------------------------------------------------------------------------- +cat(readLines("extdata/aligned-ASCII-UK.csv"), sep = "\n") + + +## ----file-io-csv-03a---------------------------------------------------------------------------------------------------- +from_csv_b.df <- + read.csv("extdata/aligned-ASCII-UK.csv", stringsAsFactors = FALSE) + + +## ----file-io-csv-03aa--------------------------------------------------------------------------------------------------- +from_csv_b.df +from_csv_b.df[["col4"]] +sapply(from_csv_b.df, class) + + +## ----file-io-csv-05----------------------------------------------------------------------------------------------------- +from_csv_c.df <- + read.csv("extdata/aligned-ASCII-UK.csv", + stringsAsFactors = FALSE, strip.white = TRUE) + + +## ----file-io-csv-05aa--------------------------------------------------------------------------------------------------- +from_csv_c.df +from_csv_c.df[["col4"]] +sapply(from_csv_c.df, class) + + +## ----file-io-csv-03b---------------------------------------------------------------------------------------------------- +from_csv_b.df <- + read.csv("extdata/aligned-ASCII-UK.csv", stringsAsFactors = TRUE) + + +## ----file-io-csv-04----------------------------------------------------------------------------------------------------- +sapply(from_csv_b.df, class) +from_csv_b.df[["col4"]] +levels(from_csv_b.df[["col4"]]) + + +## ----file-io-txt-00, comment='', echo=FALSE----------------------------------------------------------------------------- +cat(readLines("extdata/aligned-ASCII.txt"), sep = "\n") + + +## ----file-io-txt-01----------------------------------------------------------------------------------------------------- +from_txt_b.df <- + read.table("extdata/aligned-ASCII.txt", + stringsAsFactors = FALSE, header = TRUE) + + +## ----file-io-txt-02----------------------------------------------------------------------------------------------------- +from_txt_b.df +from_txt_b.df[["col4"]] +sapply(from_txt_b.df, class) + + +## ----file-io-fwf-00, comment='', echo=FALSE----------------------------------------------------------------------------- +cat(readLines("extdata/aligned-ASCII.fwf"), sep = "\n") + + +## ----file-io-fwf-01----------------------------------------------------------------------------------------------------- +from_fwf_a.df <- + read.fortran("extdata/aligned-ASCII.fwf", + format = c("2F3.1", "I3", "A3"), + col.names = c("col1", "col2", "col3", "col4")) + + +## ----file-io-fwf-02----------------------------------------------------------------------------------------------------- +from_fwf_a.df +from_fwf_a.df[["col4"]] +sapply(from_fwf_a.df, class) + + +## ----file-io-txt-03----------------------------------------------------------------------------------------------------- +my.df <- data.frame(x = 1:5, y = 5:1 / 10, z = letters[1:5]) + + +## ----file-io-txt-04----------------------------------------------------------------------------------------------------- +write.csv(my.df, file = "my-file1.csv", row.names = FALSE) +file.show("my-file1.csv", pager = "console") + + +## ----file-io-txt-05, comment='', echo=FALSE----------------------------------------------------------------------------- +cat(readLines("my-file1.csv"), sep = "\n") + + +## ----file-io-txt-11----------------------------------------------------------------------------------------------------- +my.lines <- c("abcd", "hello world", "123.45") +cat(my.lines, file = "my-file2.txt", sep = "\n") +file.show("my-file2.txt", pager = "console") + + +## ----file-io-txt-12, comment='', echo=FALSE----------------------------------------------------------------------------- +cat(readLines('my-file2.txt'), sep = '\n') + + +## ----eval=FALSE, include=FALSE------------------------------------------------------------------------------------------ +## citation(package = "readr") + + +## ----readr-01----------------------------------------------------------------------------------------------------------- +read_csv(file = "extdata/aligned-ASCII-UK.csv", show_col_types = FALSE) + + +## ----readr-02----------------------------------------------------------------------------------------------------------- +read_csv(file = "extdata/not-aligned-ASCII-UK.csv", show_col_types = FALSE) + + +## ----readr-03, eval=FALSE----------------------------------------------------------------------------------------------- +## read_table(file = "extdata/aligned-ASCII.txt") + + +## ----readr-04, eval=FALSE----------------------------------------------------------------------------------------------- +## read_table(file = "extdata/not-aligned-ASCII.txt") + + +## ----readr-05a---------------------------------------------------------------------------------------------------------- +read_delim(file = "extdata/not-aligned-ASCII.txt", + delim = " ", show_col_types = FALSE) + + +## ----tibble-print-20, echo=FALSE---------------------------------------------------------------------------------------- +options(tibble.print_max = 6, tibble.print_min = 6) + + +## ----readr-06----------------------------------------------------------------------------------------------------------- +read_table(file = "extdata/miss-aligned-ASCII.txt", show_col_types = FALSE) + + +## ----readr-07----------------------------------------------------------------------------------------------------------- +read_table(file = "extdata/miss-aligned-ASCII.txt", show_col_types = FALSE, + guess_max = 3L) + + +## ----tibble-print-21, echo=FALSE---------------------------------------------------------------------------------------- +options(tibble.print_max = 3, tibble.print_min = 3) + + +## ----readr-10----------------------------------------------------------------------------------------------------------- +write_excel_csv(my.df, file = "my-file6.csv") +file.show("my-file6.csv", pager = "console") + + +## ----readr-11, comment='', echo=FALSE----------------------------------------------------------------------------------- +cat(read_lines('my-file6.csv'), sep = '\n') + + +## ----readr-12----------------------------------------------------------------------------------------------------------- +one.str <- read_file(file = "extdata/miss-aligned-ASCII.txt") +length(one.str) +cat(one.str) + + +## ----xml2-00, eval=FALSE, include=FALSE--------------------------------------------------------------------------------- +## citation(package = "xml2") + + +## ----xml2-01------------------------------------------------------------------------------------------------------------ +web_page <- read_html("https://www.learnr-book.info/") + + +## ----xml2-01a, eval=FALSE----------------------------------------------------------------------------------------------- +## html_structure(web_page) + + +## ----xml2-02------------------------------------------------------------------------------------------------------------ +xml_text(xml_find_all(web_page, ".//title")) + + +## ----gps-01------------------------------------------------------------------------------------------------------------- +xmlTreeParse(file = "extdata/GPSDATA.gpx", useInternalNodes = TRUE) |> +xmlRoot(x = _) |> +xmlToList(node = _) |> +_[["trk"]] |> +assign(x = "temp", value = _) |> +_[names(x = temp) == "trkseg"] |> +unlist(x = _, recursive = FALSE) |> +map_df(.x = _, .f = function(x) as_tibble(x = t(x = unlist(x = x)))) +rm(temp) # cleanup + + +## ----readxl-00, eval=FALSE, include=FALSE------------------------------------------------------------------------------- +## citation(package = "readxl") + + +## ----readxl-01---------------------------------------------------------------------------------------------------------- +sheets <- excel_sheets("extdata/Book1.xlsx") +sheets + + +## ----readxl-02---------------------------------------------------------------------------------------------------------- +Book1.df <- read_excel("extdata/Book1.xlsx", + sheet = "my data") +Book1.df + + +## ----readxl-03---------------------------------------------------------------------------------------------------------- +Book1_region.df <- read_excel("extdata/Book1.xlsx", + sheet = "my data", + range = "A1:B8") +Book1_region.df + + +## ----readxl-04---------------------------------------------------------------------------------------------------------- +Book1_region.df <- read_excel("extdata/Book1.xlsx", + sheet = "my data", + range = "A2:B8", + col_names = c("A", "B")) +Book1_region.df + + + +## ----xlsx-00, eval=FALSE, include=FALSE--------------------------------------------------------------------------------- +## citation(package = "xlsx") + + +## ----xlsx-01------------------------------------------------------------------------------------------------------------ +Book1_xlsx.df <- read.xlsx("extdata/Book1.xlsx", + sheetName = "my data") +Book1_xlsx.df +sapply(Book1_xlsx.df, class) + + +## ----xlsx-05------------------------------------------------------------------------------------------------------------ +set.seed(456321) +my.data <- data.frame(x = 1:10, y = letters[1:10]) +write.xlsx(my.data, + file = "extdata/my-data.xlsx", + sheetName = "first copy") +write.xlsx(my.data, + file = "extdata/my-data.xlsx", + sheetName = "second copy", + append = TRUE) + + +## ----readODS-00--------------------------------------------------------------------------------------------------------- +list_ods_sheets("extdata/Book1.ods") + + +## ----readODS-01--------------------------------------------------------------------------------------------------------- +ods.df <- read_ods("extdata/Book1.ods", sheet = 1) + + +## ----readODS-02--------------------------------------------------------------------------------------------------------- +ods.df + + +## ----foreign-00, eval=FALSE, include=FALSE------------------------------------------------------------------------------ +## citation(package = "foreign") + + +## ----foreign-01--------------------------------------------------------------------------------------------------------- +my_spss.df <- read.spss(file = "extdata/my-data.sav", to.data.frame = TRUE) +my_spss.df[1:6, c(1:6, 17)] + + +## ----foreign-02--------------------------------------------------------------------------------------------------------- +thiamin.df <- read.spss(file = "extdata/thiamin.sav", to.data.frame = TRUE) +head(thiamin.df) + + +## ----foreign-03--------------------------------------------------------------------------------------------------------- +my_systat.df <- read.systat(file = "extdata/BIRCH1.SYS") +head(my_systat.df) + + +## ----haven-00, eval=FALSE, include=FALSE-------------------------------------------------------------------------------- +## citation(package = "haven") + + +## ----haven-01----------------------------------------------------------------------------------------------------------- +my_spss.tb <- read_sav(file = "extdata/my-data.sav") +my_spss.tb[1:6, c(1:6, 17)] + + +## ----haven-02----------------------------------------------------------------------------------------------------------- +thiamin.tb <- read_sav(file = "extdata/thiamin.sav") +thiamin.tb + + +## ----haven-02a---------------------------------------------------------------------------------------------------------- +thiamin.tb <- as_factor(thiamin.tb) +thiamin.tb + + +## ----ncdf4-00, eval=FALSE, include=FALSE-------------------------------------------------------------------------------- +## citation(package = "ncdf4") + + +## ----ncdf4-01----------------------------------------------------------------------------------------------------------- +meteo_data.nc <- nc_open("extdata/pevpr.sfc.mon.ltm.nc") +str(meteo_data.nc, max.level = 1) + + +## ----ncdf4-02----------------------------------------------------------------------------------------------------------- +time.vec <- ncvar_get(meteo_data.nc, "time") +head(time.vec) +longitude <- ncvar_get(meteo_data.nc, "lon") +head(longitude) +latitude <- ncvar_get(meteo_data.nc, "lat") +head(latitude) + + +## ----ncdf4-03----------------------------------------------------------------------------------------------------------- +pet.tb <- + tibble(time = time.vec, + month = month(ymd("1800-01-01") + days(time)), + lon = longitude[6], + lat = latitude[2], + pet = ncvar_get(meteo_data.nc, "pevpr")[6, 2, ] + ) +pet.tb + + +## ----tidync-00, eval=FALSE, include=FALSE------------------------------------------------------------------------------- +## citation(package = "tidync") + + +## ----tidync-01---------------------------------------------------------------------------------------------------------- +meteo_data.tnc <- tidync("extdata/pevpr.sfc.mon.ltm.nc") +meteo_data.tnc + + +## ----tidync-01a--------------------------------------------------------------------------------------------------------- +hyper_dims(meteo_data.tnc) + + +## ----tidync-01b--------------------------------------------------------------------------------------------------------- +hyper_vars(meteo_data.tnc) + + +## ----tidync-02---------------------------------------------------------------------------------------------------------- +hyper_tibble(meteo_data.tnc, + lon = signif(lon, 1) == 9, + lat = signif(lat, 2) == 87) |> + mutate(.data = _, month = month(ymd("1800-01-01") + days(time))) |> + select(.data = _, -time) + + +## ----tidync-03---------------------------------------------------------------------------------------------------------- +hyper_tibble(meteo_data.tnc, + lon = signif(lon, 1) == 9) |> + mutate(.data = _, month = month(ymd("1800-01-01") + days(time))) |> + select(.data = _, -time) + + +## ----url-01, eval=eval_online_data-------------------------------------------------------------------------------------- +logger.df <- + read.csv2(file = "http://r4photobiology.info/learnr/logger_1.txt", + header = FALSE, + col.names = c("time", "temperature")) +sapply(logger.df, class) + + +## ----url-11, eval=eval_online_data-------------------------------------------------------------------------------------- +download.file("http://r4photobiology.info/learnr/my-data.xlsx", + "data/my-data-dwn.xlsx", + mode = "wb") + + +## ----url-03, eval=eval_online_data-------------------------------------------------------------------------------------- +remote_thiamin.df <- + read.spss(file = "http://r4photobiology.info/learnr/thiamin.sav", + to.data.frame = TRUE) +head(remote_thiamin.df) + + +## ----url-04, eval=eval_online_data-------------------------------------------------------------------------------------- +remote_my_spss.tb <- + read_sav(file = "http://r4photobiology.info/learnr/thiamin.sav") +remote_my_spss.tb + + +## ----url-05, eval=eval_online_data-------------------------------------------------------------------------------------- +if (!file.exists("extdata/pevpr.sfc.mon.ltm.nc")) { + my.url <- paste("ftp://ftp.cdc.noaa.gov/Datasets/ncep.reanalysis.derived/", + "surface_gauss/pevpr.sfc.mon.ltm.nc", + sep = "") + download.file(my.url, + mode = "wb", + destfile = "extdata/pevpr.sfc.mon.ltm.nc") +} +pet_ltm.nc <- nc_open("extdata/pevpr.sfc.mon.ltm.nc") + + +## ----dbplyr-00a, eval=FALSE, include=FALSE------------------------------------------------------------------------------ +## citation(package = "dbplyr") + + +## ----dbplyr-00b, eval=FALSE, include=FALSE------------------------------------------------------------------------------ +## citation(package = "DBI") + + +## ----dbplyr-00c, eval=FALSE, include=FALSE------------------------------------------------------------------------------ +## citation(package = "RSQLite") + + +## ----dbplyr-01---------------------------------------------------------------------------------------------------------- +library(dplyr) +con <- DBI::dbConnect(RSQLite::SQLite(), dbname = ":memory:") +copy_to(con, weather_wk_25_2019.tb, "weather", + temporary = FALSE, + indexes = list( + c("month_name", "calendar_year", "solar_time"), + "time", + "sun_elevation", + "was_sunny", + "day_of_year", + "month_of_year" + ) +) +weather.db <- tbl(con, "weather") +colnames(weather.db) +weather.db |> + filter(.data = _, sun_elevation > 5) |> + group_by(.data = _, day_of_year) |> + summarise(.data = _, energy_Wh = sum(global_watt, na.rm = TRUE) * 60 / 3600) + + +## ----clear-up-data-folders---------------------------------------------------------------------------------------------- +unlink("./data", recursive = TRUE) +unlink("./extdata", recursive = TRUE) + + +## ----iot-00, eval=FALSE, include=FALSE---------------------------------------------------------------------------------- +## citation(package = "jsonlite") + + +## ----iot-01, eval=eval_yoctopuce---------------------------------------------------------------------------------------- +hub.url <- "http://localhost:4444/" +Meteo01.df <- + fromJSON(paste(hub.url, "byName/C1-Meteo/dataLogger.json", + sep = ""), flatten = TRUE) +str(Meteo01.df, max.level = 2) + + +## ----iot-02, eval=eval_yoctopuce, cache=TRUE---------------------------------------------------------------------------- +Meteo01.df[["streams"]][[which(Meteo01.df$id == "temperature")]] |> + as_tibble(x = _) |> + dplyr::transmute(.data = _, + utc.time = as.POSIXct(utc, origin = "1970-01-01", tz = "UTC"), + t_min = unlist(val)[c(TRUE, FALSE, FALSE)], + t_mean = unlist(val)[c(FALSE, TRUE, FALSE)], + t_max = unlist(val)[c(FALSE, FALSE, TRUE)]) -> temperature.df + +Meteo01.df[["streams"]][[which(Meteo01.df$id == "humidity")]] |> + as_tibble(x = _) |> + dplyr::transmute(.data = _, + utc.time = as.POSIXct(utc, origin = "1970-01-01", tz = "UTC"), + hr_min = unlist(val)[c(TRUE, FALSE, FALSE)], + hr_mean = unlist(val)[c(FALSE, TRUE, FALSE)], + hr_max = unlist(val)[c(FALSE, FALSE, TRUE)]) -> humidity.df + +full_join(temperature.df, humidity.df) + + +## ----echo=FALSE--------------------------------------------------------------------------------------------------------- +try(detach(package:jsonlite)) +try(detach(package:lubridate)) +try(detach(package:tidync)) +try(detach(package:ncdf4)) +try(detach(package:xml2)) +try(detach(package:haven)) +try(detach(package:foreign)) +try(detach(package:xlsx)) +try(detach(package:readxl)) +try(detach(package:readr)) +try(detach(package:tidyr)) +try(detach(package:dplyr)) +try(detach(package:stringr)) +try(detach(package:tibble)) +try(detach(package:learnrbook)) + + +## ----eval=eval_diag, include=eval_diag, echo=eval_diag, cache=FALSE----------------------------------------------------- +## knitter_diag() +## R_diag() +## other_diag() + diff --git a/learn-r-2ed-purl/R.functions.R b/learn-r-2ed-purl/R.functions.R new file mode 100644 index 00000000..2bf82330 --- /dev/null +++ b/learn-r-2ed-purl/R.functions.R @@ -0,0 +1,244 @@ +## ----echo=FALSE, include=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_fig_wide) +opts_knit$set(concordance=TRUE) +opts_knit$set(unnamed.chunk.label = 'functions-chunk') + + +## ----fun-00a------------------------------------------------------------------------------------------------------------ +fun1 <- function(x, y){x * y} + + +## ----fun-00b------------------------------------------------------------------------------------------------------------ +fun1(x = 4, y = 3) + + +## ----fun-00c, eval=eval_playground-------------------------------------------------------------------------------------- +fun1(x = 10, y = 50) +fun1(x = 10, y = 50) * 3 + + +## ----fun-02, eval=eval_playground--------------------------------------------------------------------------------------- +FN1 <- function(x) print("prn") +FN1("arg") +FN2 <- function(x){print("prn") + return(x)} +FN2("arg") +FN3 <- function(x){return(x) + print("prn")} +FN3("arg") +FN4 <- function(x){return() + print("prn")} +FN4("arg") +FN5 <- function(x){return(print(x)) + print("prn")} +FN5("arg") + + +## ----fun-01------------------------------------------------------------------------------------------------------------- +fn2 <- function(x){x <- 99} +a <- 1 +fn2(a) +a + + +## ----fun-10------------------------------------------------------------------------------------------------------------- +(function(x, y){x * y})(x = 4, y = 3) + + +## ----fun-11------------------------------------------------------------------------------------------------------------- +(\(x, y){x * y})(x = 4, y = 3) + + +## ----scope-01----------------------------------------------------------------------------------------------------------- +pi +pi <- "apple pie" +pi +rm(pi) +pi +exists("pi") + + +## ----scope-02----------------------------------------------------------------------------------------------------------- +my.pie <- "raspberry pie" +my.pie +my.pie <- "apple pie" +my.pie +rm(my.pie) +exists("my.pie") + + +## ----fun-03------------------------------------------------------------------------------------------------------------- +SEM <- function(x){sqrt(var(x) / length(x))} + + +## ----fun-04------------------------------------------------------------------------------------------------------------- +a <- c(1, 2, 3, -5) +a.na <- c(a, NA) +SEM(x = a) +SEM(x = a.na) + + +## ----fun-05------------------------------------------------------------------------------------------------------------- +sem <- function(x, na.rm = FALSE) { + if (na.rm) { + x <- na.omit(x) + } + sqrt(var(x)/length(x)) +} + + +## ----fun-06------------------------------------------------------------------------------------------------------------- +sem(x = a) +sem(x = a.na) +sem(x = a.na, na.rm = TRUE) + + +## ----fun-07------------------------------------------------------------------------------------------------------------- +sem + + +## ----fun-08------------------------------------------------------------------------------------------------------------- +sd + + +## ----fun-09------------------------------------------------------------------------------------------------------------- +list + + +## ----oper-01------------------------------------------------------------------------------------------------------------ +1 / 2 +`/`(1 , 2) +`/`(e1 = 1 , e2 = 2) + + +## ----oper-02------------------------------------------------------------------------------------------------------------ +`/` + + +## ----oper-EB01---------------------------------------------------------------------------------------------------------- +"%-mean%" <- function(e1, e2) { + e1 - mean(e2) +} + + +## ----oper-EB02---------------------------------------------------------------------------------------------------------- +10:15 %-mean% 1:20 + + +## ----oper-EB03---------------------------------------------------------------------------------------------------------- +`%-mean%` + + +## ----object-classes-00-------------------------------------------------------------------------------------------------- +mean + + +## ----object-classes-00a------------------------------------------------------------------------------------------------- +methods(mean) + + +## ----object-classes-00b------------------------------------------------------------------------------------------------- +methods(class = "list") + + +## ----explain-object-classes-01------------------------------------------------------------------------------------------ +a <- 123 +class(a) +class(a) <- c("myclass", class(a)) +class(a) + + +## ----explain-object-classes-02------------------------------------------------------------------------------------------ +print.myclass <- function(x) { + sprintf("[myclass] %.0f", as.numeric(x)) +} + + +## ----explain-object-classes-03------------------------------------------------------------------------------------------ +print(a) +print(as.numeric(a)) + + +## ----explain-object-classes-04------------------------------------------------------------------------------------------ +b <- 456 +class(b) <- c("derivclass", class(a)) + + +## ----explain-object-classes-05------------------------------------------------------------------------------------------ +print(b) + + +## ----explain-object-classes-05a----------------------------------------------------------------------------------------- +print(as.numeric(b)) + + +## ----pkg-00, eval=FALSE------------------------------------------------------------------------------------------------- +## install.packages("learnrbook") + + +## ----pkg-00x, eval=FALSE------------------------------------------------------------------------------------------------ +## update.packages() + + +## ----remotes-00y, eval=FALSE-------------------------------------------------------------------------------------------- +## remotes::install_github("aphalo/learnrbook-pkg") + + +## ----pak-00z, eval=FALSE------------------------------------------------------------------------------------------------ +## pak::pkg_install("learnrbook") # from CRAN +## pak::pkg_install("aphalo/learnrbook-pkg") # from GitHub + + +## ----pkg-00a, eval=FALSE------------------------------------------------------------------------------------------------ +## library("learnrbook") + + +## ----pkg-version-01----------------------------------------------------------------------------------------------------- +packageVersion(pkg="learnrbook") + + +## ----citation-1--------------------------------------------------------------------------------------------------------- +citation() + + +## ----pkg-01, eval=eval_playground--------------------------------------------------------------------------------------- +class(cars) +head(cars, 3) +getAnywhere("cars")$where # defined in package + + +## ----pkg-01a, eval=eval_playground-------------------------------------------------------------------------------------- +cars <- 1:10 +class(cars) +head(cars, 3) # prints 'cars' defined in the global environment +rm(cars) # clean up +head(cars, 3) +getAnywhere("cars")$where # the first visible definition is in the global environemnt + + +## ----pkg-02a------------------------------------------------------------------------------------------------------------ +mean + + +## ----pkg-02b------------------------------------------------------------------------------------------------------------ +mean <- mean(1:5) +mean +mean(8:9) + + +## ----pkg-02c------------------------------------------------------------------------------------------------------------ +getAnywhere("mean")$where +rm(mean) +getAnywhere("mean")$where + + +## ----pkg-03, eval=FALSE------------------------------------------------------------------------------------------------- +## datasets::cars <- "my car is green" +## rm(datasets::cars) + + +## ----eval=eval_diag, include=eval_diag, echo=eval_diag, cache=FALSE----------------------------------------------------- +## knitter_diag() +## R_diag() +## other_diag() + diff --git a/learn-r-2ed-purl/R.learning.R b/learn-r-2ed-purl/R.learning.R new file mode 100644 index 00000000..6001a892 --- /dev/null +++ b/learn-r-2ed-purl/R.learning.R @@ -0,0 +1,4 @@ +## ----syntax-highlight-1------------------------------------------------------------------------------------------------- +z <- mean(1, 5, 3) +print(z) + diff --git a/learn-r-2ed-purl/R.plotting.R b/learn-r-2ed-purl/R.plotting.R new file mode 100644 index 00000000..738db2bc --- /dev/null +++ b/learn-r-2ed-purl/R.plotting.R @@ -0,0 +1,1870 @@ +## ----echo=FALSE, include=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_fig_wide) +opts_knit$set(concordance=TRUE) +opts_knit$set(unnamed.chunk.label = 'plotting-chunk') + + +## ----eval=FALSE, include=FALSE------------------------------------------------------------------------------------------ +## citation(package = "ggplot2") + + +## ----eval=FALSE--------------------------------------------------------------------------------------------------------- +## install.packages(learnrbook::pkgs_ch_ggplot) + + +## ----message=FALSE------------------------------------------------------------------------------------------------------ +library(learnrbook) +library(scales) +library(ggplot2) +library(ggrepel) +library(gginnards) +library(broom) +library(ggpmisc) +library(ggbeeswarm) +library(lubridate) +library(tibble) +library(dplyr) +library(patchwork) + + +## ----echo=FALSE--------------------------------------------------------------------------------------------------------- +theme_set(theme_gray(14)) + + +## ----echo=FALSE--------------------------------------------------------------------------------------------------------- +# set to TRUE to test non-executed code chunks and rendering of plots +eval_plots_all <- FALSE + + +## ----ggplot-basics-01--------------------------------------------------------------------------------------------------- +ggplot() + + +## ----ggplot-basics-02, eval=eval_plots_all------------------------------------------------------------------------------ +ggplot(data = mtcars) + + +## ----ggplot-basics-03--------------------------------------------------------------------------------------------------- +ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg)) + + +## ----ggplot-basics-04--------------------------------------------------------------------------------------------------- +ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg)) + + geom_point() + + +## ----ggplot-basics-04-wb1----------------------------------------------------------------------------------------------- +p1 <- ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg)) + + geom_point() + + +## ----ggplot-basics-04-wb2, eval=eval_plots_all-------------------------------------------------------------------------- +print(p1) + + +## ----ggplot-basics-04a-------------------------------------------------------------------------------------------------- +ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg)) + + geom_point(colour = "blue", shape = "square") + + +## ----ggplot-basics-04b-------------------------------------------------------------------------------------------------- +ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg)) + + geom_point(mapping = aes(colour = "blue", shape = "square")) + + +## ----ggplot-basics-05--------------------------------------------------------------------------------------------------- +ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg)) + + geom_point() + + stat_smooth(geom = "line", method = "lm", formula = y ~ x) + + +## ----ggplot-basics-06--------------------------------------------------------------------------------------------------- +ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg)) + + geom_point() + + stat_smooth(geom = "line", method = "lm", formula = y ~ x) + + scale_y_log10() + + +## ----ggplot-basics-07--------------------------------------------------------------------------------------------------- +ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg)) + + geom_point() + + stat_smooth(geom = "line", method = "lm", formula = y ~ x) + + coord_cartesian(ylim = c(15, 25)) + + +## ----ggplot-basics-08--------------------------------------------------------------------------------------------------- +ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg)) + + geom_point() + + stat_smooth(geom = "line", method = "lm", formula = y ~ x) + + coord_trans(y = "log10") + + +## ----ggplot-basics-09--------------------------------------------------------------------------------------------------- +ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg)) + + geom_point() + + theme_classic() + + +## ----ggplot-basics-10--------------------------------------------------------------------------------------------------- +ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg)) + + geom_point() + + theme_classic(base_size = 20, base_family = "serif") + + +## ----ggplot-basics-11--------------------------------------------------------------------------------------------------- +ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg)) + + geom_point() + + labs(x = "Engine displacement (cubic inches)", + y = "Fuel use efficiency\n(miles per gallon)", + title = "Motor Trend Car Road Tests", + subtitle = "Source: 1974 Motor Trend US magazine") + + +## ----ggplot-basics-info-01---------------------------------------------------------------------------------------------- +ggplot(data = mtcars, + mapping = aes(x = disp / cyl, y = mpg)) + + geom_point() + + +## ----ggplot-basics-04-wb1----------------------------------------------------------------------------------------------- +p1 <- ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg)) + + geom_point() + + +## ----ggplot-objects-02-------------------------------------------------------------------------------------------------- +p1 + stat_smooth(geom = "line", method = "lm", formula = y ~ x) + + +## ----ggplot-objects-info-01--------------------------------------------------------------------------------------------- +p.ls <- list( + stat_smooth(geom = "line", method = "lm", formula = y ~ x), + scale_y_log10()) + + +## ----ggplot-objects-info-02--------------------------------------------------------------------------------------------- +p1 + p.ls + + +## ----ggplot-objects-03a------------------------------------------------------------------------------------------------- +summary(p1) + + +## ----ggplot-objects-03b------------------------------------------------------------------------------------------------- +names(p1) + + +## ----ggplot-objects-box-03, eval=FALSE---------------------------------------------------------------------------------- +## str(p1$layers, max.level = 1) + + +## ----ggplot-basics-12a-------------------------------------------------------------------------------------------------- +p2 <- + ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg, colour = cyl)) + + geom_point() +p2 + + +## ----ggplot-basics-12b-------------------------------------------------------------------------------------------------- +p2 + scale_colour_viridis_c(option = "magma", end = 0.85) + + +## ----ggplot-basics-12c-------------------------------------------------------------------------------------------------- +ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg, colour = factor(cyl))) + + geom_point() + + +## ----ggplot-basics-12d, eval=eval_plots_all----------------------------------------------------------------------------- +ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg, colour = ordered(cyl))) + + geom_point() + + +## ----ggplot-basics-13, eval=eval_plots_all------------------------------------------------------------------------------ +p3 <- + ggplot() + + geom_point(data = mtcars, + mapping = aes(x = disp, y = mpg, colour = cyl)) +p3 + + +## ----ggplot-basics-14, eval=eval_plots_all------------------------------------------------------------------------------ +ggplot(data = mtcars) + + aes(x = disp, y = mpg) + + geom_point() + + +## ----ggplot-basics-15, eval=eval_plots_all------------------------------------------------------------------------------ +ggplot() + + aes(x = disp, y = mpg) + + geom_point(data = mtcars) + + +## ----ggplot-basics-15a, eval=eval_plots_all----------------------------------------------------------------------------- +my.mapping <- aes(x = disp, y = mpg) +ggplot(data = mtcars, + mapping = my.mapping) + + geom_point() + + +## ----ggplot-basics-15b-------------------------------------------------------------------------------------------------- +ggplot(data = mtcars, + mapping =aes(x = disp, y = mpg * 0.43)) + + geom_point() + + +## ----ggplot-basics-16--------------------------------------------------------------------------------------------------- +ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg)) + + geom_point(size = 4) + + geom_point(data = function(x){subset(x = x, cyl == 4)}, + colour = "yellow", size = 1.5) + + +## ----ggplot-basics-17, eval=eval_plots_all------------------------------------------------------------------------------ +ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg)) + + geom_point(size = 4) + + geom_point(data = . %>% subset(x = ., cyl == 4), colour = "yellow", + size = 1.5) + + +## ----ggplot-basics-18, eval=eval_plots_all------------------------------------------------------------------------------ +ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg)) + + geom_point(size = 4) + + geom_point(colour = "yellow", + mapping = aes(size = ifelse(cyl == 4, 1.5, NA)), + na.rm = TRUE) + + scale_size_identity() + + +## ----mapping-stage-01--------------------------------------------------------------------------------------------------- +set.seed(4321) +X <- 0:10 +Y <- (X + X^2 + X^3) + rnorm(length(X), mean = 0, sd = mean(X^3) / 4) +df1 <- data.frame(X, Y) +df2 <- df1 +df2[6, "Y"] <-df1[6, "Y"] * 10 + + +## ----mapping-stage-02--------------------------------------------------------------------------------------------------- +ggplot(data = df2, mapping = aes(x = X, y = Y)) + + stat_fit_residuals(formula = y ~ poly(x, 3, raw = TRUE), method = "rlm", + mapping = aes(colour = after_stat(weights)), + show.legend = TRUE) + + scale_colour_gradient(low = "red", high = "blue", limits = c(0, 1), + guide = "colourbar") + + +## ----mapping-stage-03--------------------------------------------------------------------------------------------------- +ggplot(df2) + + stat_fit_residuals(formula = y ~ poly(x, 3, raw = TRUE), + method = "rlm", + mapping = aes(x = X, + y = stage(start = Y, + after_stat = y * weights), + colour = after_stat(weights)), + show.legend = TRUE) + + scale_colour_gradient(low = "red", high = "blue", limits = c(0, 1), + guide = "colourbar") + + +## ----scatter-01--------------------------------------------------------------------------------------------------------- +p.base <- + ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg, shape = factor(cyl))) + + geom_point() +p.base + + +## ----scatter-11--------------------------------------------------------------------------------------------------------- +p.base + + scale_shape_discrete(solid = FALSE) + + +## ----scatter-11a, eval=eval_plots_all----------------------------------------------------------------------------------- +p.base + + scale_shape_manual(values = c("circle open", + "square open", + "diamond open")) + + +## ----scatter-12, eval=eval_plots_all------------------------------------------------------------------------------------ +p.base + + scale_shape_manual(values = c("4", "6", "8"), guide = "none") + + +## ----scatter-14, eval=eval_plots_all------------------------------------------------------------------------------------ +ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg, + shape = factor(cyl), colour = factor(cyl))) + + geom_point() + + +## ----scatter-12a-------------------------------------------------------------------------------------------------------- +ggplot(data = mtcars, + mapping = aes(x = factor(cyl), y = mpg)) + + geom_point(alpha = 1/3) + + +## ----scatter-13--------------------------------------------------------------------------------------------------------- +ggplot(data = mtcars, + mapping = aes(x = factor(cyl), y = mpg)) + + geom_point(position = position_jitter(width = 0.25, heigh = 0)) + + +## ----scatter-13info, eval=eval_plots_all-------------------------------------------------------------------------------- +ggplot(data = mtcars, + mapping = aes(x = factor(cyl), y = mpg), colour = factor(cyl)) + + geom_point(position = "jitter") + + +## ----scatter-16--------------------------------------------------------------------------------------------------------- +ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg, colour = factor(cyl), size = wt)) + + scale_size_area() + + geom_point(shape = "circle open", stroke = 1.5) + + +## ----scatter-18--------------------------------------------------------------------------------------------------------- +ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg, shape = factor(cyl), + fill = factor(cyl), size = wt)) + + geom_point(alpha = 0.33, colour = "black") + + scale_size_area() + + scale_shape_manual(values = c("circle filled", + "square filled", + "diamond filled")) + + +## ----rug-plot-01-------------------------------------------------------------------------------------------------------- +ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg, colour = factor(cyl))) + + geom_point() + + geom_rug(sides = "btlr") + + +## ----line-plot-01------------------------------------------------------------------------------------------------------- +ggplot(data = Orange, + mapping = aes(x = age, y = circumference, linetype = Tree)) + + geom_line() + + +## ----step-plot-01------------------------------------------------------------------------------------------------------- +ggplot(data = Orange, + mapping = aes(x = age, y = circumference, linetype = Tree)) + + geom_step() + + +## ----line-plots-PG01,eval=eval_playground------------------------------------------------------------------------------- +toy.df <- data.frame(x = c(1,3,2,4), y = c(0,1,0,1)) + + +## ----area-plot-01------------------------------------------------------------------------------------------------------- +p1 <- # will be used again later + ggplot(data = Orange, + mapping = aes(x = age, y = circumference, fill = Tree)) + + geom_area(position = "stack", colour = "white", linewidth = 1) +p1 + + +## ----area-plot-02------------------------------------------------------------------------------------------------------- +p1 + + geom_vline(xintercept = 365 * 1:3, colour = "gray75") + + geom_vline(xintercept = 365 * 1:3, linetype = "dashed") + + +## ----col-plot-01-------------------------------------------------------------------------------------------------------- +set.seed(654321) +my.col.data <- + data.frame(treatment = factor(rep(c("A", "B", "C"), 2)), + group = factor(rep(c("male", "female"), c(3, 3))), + measurement = rnorm(6) + c(5.5, 5, 7)) + + +## ----echo=FALSE, include=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_fig_medium) + + +## ----col-plot-02-------------------------------------------------------------------------------------------------------- +ggplot(subset(my.col.data, group == "female"), + mapping = aes(x = treatment, y = measurement)) + + geom_col() + + +## ----echo=FALSE, include=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_2fig_very_wide) + + +## ----col-plot-03-------------------------------------------------------------------------------------------------------- +p.base <- + ggplot(my.col.data, + mapping = aes(x = treatment, y = measurement, fill = group)) + + +## ----col-plot-03a------------------------------------------------------------------------------------------------------- +p1 <- p.base + geom_col(width = 0.5) + ggtitle("stack (default)") + + +## ----col-plot-04-------------------------------------------------------------------------------------------------------- +p2 <- p.base + geom_col(position = "dodge") + ggtitle("dodge") + + +## ----col-plot-04a------------------------------------------------------------------------------------------------------- +p1 + p2 + + +## ----echo=FALSE, include=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_fig_wide) + + +## ----tile-plot-01------------------------------------------------------------------------------------------------------- +set.seed(1234) +randomf.df <- data.frame(F.value = rf(100, df1 = 2, df2 = 20), + x = rep(letters[1:10], 10), + y = LETTERS[rep(1:10, rep(10, 10))]) + + +## ----tile-plot-02------------------------------------------------------------------------------------------------------- +ggplot(data = randomf.df, + mapping = aes(x, y, fill = F.value)) + + geom_tile() + + +## ----tile-plot-03------------------------------------------------------------------------------------------------------- +ggplot(data = randomf.df, + mapping = aes(x, y, fill = F.value)) + + geom_tile(colour = "gray75", linewidth = 1) + + +## ----tile-plot-04, eval=eval_plots_all---------------------------------------------------------------------------------- +ggplot(data = randomf.df, + mapping = aes(x, y, fill = F.value)) + + geom_tile(colour = "white") + + scale_fill_gradient(low = "gray15", high = "gray85", na.value = "red") + + +## ----sf_plot-01--------------------------------------------------------------------------------------------------------- +nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) +ggplot(nc) + + geom_sf(mapping = aes(fill = AREA), colour = "gray90") + + +## ----text-plot-01------------------------------------------------------------------------------------------------------- +ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg, + colour = factor(cyl), size = wt, label = cyl)) + + geom_point(alpha = 1/3) + + geom_text(colour = "darkblue", size = 3) + + +## ----text-plot-02------------------------------------------------------------------------------------------------------- +my.data <- + data.frame(x = 1:5, + y = rep(2, 5), + label = c("ab", "bc", "cd", "de", "ef")) + + +## ----text-plot-02a------------------------------------------------------------------------------------------------------ +ggplot(data = my.data, + mapping = aes(x, y, label = label)) + + geom_text(angle = 90, hjust = 1.5, size = 4) + + geom_point() + + +## ----text-plot-04, eval=eval_plots_all---------------------------------------------------------------------------------- +ggplot(data = my.data, + mapping = aes(x, y, label = label)) + + geom_text(angle = 90, hjust = 1.5, size = 4, family = "serif") + + geom_point() + + +## ----text-plot-05------------------------------------------------------------------------------------------------------- +my.data <- + data.frame(x = 1:5, y = rep(2, 5), label = paste("alpha[", 1:5, "]", sep = "")) +my.data$label + + +## ----text-plot-06------------------------------------------------------------------------------------------------------- +ggplot(data = my.data, + mapping = aes(x, y, label = label)) + + geom_text(hjust = -0.2, parse = TRUE, size = 6) + + geom_point() + + expand_limits(x = 5.2) + + +## ----text-plot-07, eval=eval_plots_all---------------------------------------------------------------------------------- +ggplot(data = my.data, + mapping = aes(x, y, label = paste("alpha[", x, "]", sep = ""))) + + geom_text(hjust = -0.2, parse = TRUE, size = 6) + + geom_point() + + +## ----label-plot-01------------------------------------------------------------------------------------------------------ +my.data <- + data.frame(x = 1:5, y = rep(2, 5), + label = c("one", "two", "three", "four", "five")) + +ggplot(data = my.data, + mapping = aes(x, y, label = label)) + + geom_label(hjust = -0.2, size = 6, + label.size = 0, + label.r = unit(0, "lines"), + label.padding = unit(0.15, "lines"), + fill = "yellow", alpha = 0.5) + + geom_point() + + expand_limits(x = 5.6) + + +## ----repel-plot-01------------------------------------------------------------------------------------------------------ +ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg, + colour = factor(cyl), size = wt, label = cyl)) + + scale_size() + + geom_point(alpha = 1/3) + + geom_text_repel(colour = "black", size = 3, + min.segment.length = 0.2, point.padding = 0.1) + + +## ----table-plot-01------------------------------------------------------------------------------------------------------ +mtcars |> + group_by(cyl) |> + summarize("mean wt" = format(mean(wt), digits = 3), + "mean disp" = format(mean(disp), digits = 2), + "mean mpg" = format(mean(mpg), digits = 2)) -> my.table +table.tb <- tibble(x = 500, y = 35, table.inset = list(my.table)) + + +## ----table-plot-02------------------------------------------------------------------------------------------------------ +ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg, colour = factor(cyl), size = wt)) + + scale_size() + + geom_point() + + geom_table(data = table.tb, + mapping = aes(x = x, y = y, label = table.inset), + colour = "black", size = 3) + + +## ----plot-plot-01------------------------------------------------------------------------------------------------------- +mtcars |> + group_by(cyl) |> + summarize(mean.mpg = mean(mpg)) |> + ggplot(data = _, + mapping = aes(factor(cyl), mean.mpg, fill = factor(cyl))) + + scale_fill_discrete(guide = "none") + + scale_y_continuous(name = NULL) + + geom_col() + + theme_bw(8) -> my.plot +plot.tb <- data.frame(x = 500, y = 35, plot.inset = I(list(my.plot))) + + +## ----plot-plot-02------------------------------------------------------------------------------------------------------- +ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg, colour = factor(cyl))) + + geom_point() + + geom_plot(data = plot.tb, + aes(x = x, y = y, label = plot.inset), + vp.width = 1/2, + hjust = "inward", vjust = "inward") + + +## ----plot-plot-03a------------------------------------------------------------------------------------------------------ +p.main <- + ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg, colour = factor(cyl))) + + geom_point() + + +## ----plot-plot-03b------------------------------------------------------------------------------------------------------ +p.inset <- p.main + + coord_cartesian(xlim = c(270, 330), ylim = c(14, 19)) + + labs(x = NULL, y = NULL) + + scale_colour_discrete(guide = "none") + + theme_bw(8) + theme(aspect.ratio = 1) + + +## ----plot-plot-03c------------------------------------------------------------------------------------------------------ +p.main + + geom_plot(x = 480, y = 34, label = list(p.inset), vp.height = 1/2) + + annotate(geom = "rect", fill = NA, colour = "black", + xmin = 270, xmax = 330, ymin = 14, ymax = 19, + linetype = "dotted") + + +## ----plot-grob-01a------------------------------------------------------------------------------------------------------ +file1.name <- + system.file("extdata", "Isoquercitin.png", + package = "ggpp", mustWork = TRUE) +Isoquercitin <- magick::image_read(file1.name) +file2.name <- + system.file("extdata", "Robinin.png", + package = "ggpp", mustWork = TRUE) +Robinin <- magick::image_read(file2.name) + + +## ----plot-grob-01b------------------------------------------------------------------------------------------------------ +grob.tb <- + data.frame(x = c(0, 100), y = c(10, 20), height = 1/3, width = c(1/2), + grobs = I(list(grid::rasterGrob(image = Isoquercitin), + grid::rasterGrob(image = Robinin)))) + + +## ----plot-grob-01c------------------------------------------------------------------------------------------------------ +ggplot() + + geom_grob(data = grob.tb, + mapping = aes(x = x, y = y, label = grobs, + vp.height = height, vp.width = width), + hjust = "inward", vjust = "inward") + + +## ----plot-npc-eb-02----------------------------------------------------------------------------------------------------- +ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg, colour = factor(cyl))) + + geom_point() + + geom_label(x = I(0.5), y = I(0.9), label = "a label", colour = "black") + + +## ----plot-npc-eb-01, eval=eval_plots_all-------------------------------------------------------------------------------- +ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg, colour = factor(cyl))) + + geom_point() + + geom_label_npc(npcx = 0.5, npcy = 0.9, label = "a label", colour = "black", + vjust = "center") + + +## ----function-plot-01--------------------------------------------------------------------------------------------------- +ggplot(data = data.frame(x = c(-3,3)), + mapping = aes(x = x)) + + stat_function(fun = dnorm) + + +## ----function-plot-02, eval=eval_plots_all------------------------------------------------------------------------------ +ggplot(data = data.frame(x = c(-3,4)), + mapping = aes(x = x)) + + stat_function(fun = dnorm, args = list(mean = 1, sd = .5)) + + +## ----function-plot-03, eval=eval_plots_all------------------------------------------------------------------------------ +ggplot(data = data.frame(x = 0:1), + mapping = aes(x = x)) + + stat_function(fun = function(x, a, b){a + b * x^2}, + args = list(a = 1, b = 1.4)) + + +## ----summary-plot-00---------------------------------------------------------------------------------------------------- +fake.data <- data.frame( + y = c(rnorm(10, mean = 2, sd = 0.5), + rnorm(10, mean = 4, sd = 0.7)), + group = factor(c(rep("A", 10), rep("B", 10)))) + + +## ----summary-plot-01---------------------------------------------------------------------------------------------------- +p1.base <- + ggplot(data = fake.data, mapping = aes(y = y, x = group)) + + geom_point(shape = "circle open") + + +## ----summary-plot-02---------------------------------------------------------------------------------------------------- +p1.base + stat_summary() + + +## ----summary-plot-03---------------------------------------------------------------------------------------------------- +p1.base + + stat_summary(fun = "mean", geom = "point", + colour = "red", shape = "-", size = 15) + + +## ----summary-plot-04---------------------------------------------------------------------------------------------------- +p1.base + + stat_summary(fun.data = "mean_cl_normal", fun.args = list(conf.int = 0.99), + colour = "red", size = 0.7, linewidth = 1, alpha = 0.5) + + +## ----summary-plot-09a--------------------------------------------------------------------------------------------------- +p2.base <- + ggplot(data = mpg, mapping = aes(x = class, y = hwy)) + + stat_summary(geom = "col", fun = mean) + + +## ----summary-plot-12---------------------------------------------------------------------------------------------------- +p2.base + + stat_summary(geom = "linerange", fun.data = "mean_cl_normal", + linewidth = 1, colour = "red") + + +## ----summary-plot-10, eval=eval_plots_all------------------------------------------------------------------------------- +ggplot(data = mpg, mapping = aes(x = class, y = hwy)) + + geom_col(stat = "summary", fun = mean) + +ggplot(data = mpg, mapping = aes(x = class, y = hwy)) + + stat_summary(geom = "col", fun = mean) + + +## ----smooth-plot-01----------------------------------------------------------------------------------------------------- +p3 <- + ggplot(data = mtcars, mapping = aes(x = disp, y = mpg)) + + geom_point() + + +## ----smooth-plot-02----------------------------------------------------------------------------------------------------- +p3 + stat_smooth(method = "loess", formula = y ~ x) + + +## ----smooth-plot-03, eval=eval_plots_all-------------------------------------------------------------------------------- +p3 + stat_smooth(method = "lm", formula = y ~ x) + + +## ----smooth-plot-04----------------------------------------------------------------------------------------------------- +p3 + aes(colour = factor(cyl)) + + stat_smooth(method = "lm", formula = y ~ x) + + +## ----smooth-plot-05, eval=eval_plots_all-------------------------------------------------------------------------------- +p3 + aes(colour = factor(cyl)) + + stat_smooth(method = "lm", formula = y ~ x, colour = "black") + + +## ----smooth-plot-06----------------------------------------------------------------------------------------------------- +p3 + aes(colour = factor(cyl)) + + stat_smooth(method = "lm", formula = y ~ poly(x, 2), colour = "grey20") + + +## ----smooth-plot-07----------------------------------------------------------------------------------------------------- +ggplot(data = Puromycin, + mapping = aes(conc, rate, colour = state)) + + geom_point() + + geom_smooth(method = "nls", formula = y ~ SSmicmen(x, Vm, K), se = FALSE) + + +## ----smooth-plot-08, eval=eval_plots_all-------------------------------------------------------------------------------- +ggplot(data = Puromycin, + mapping = aes(conc, rate, colour = state)) + + geom_point() + + geom_smooth(method = "nls", + formula = y ~ (Vmax * x) / (k + x), + method.args = list(start = list(Vmax = 200, k = 0.05)), + se = FALSE) + + +## ----smooth-plot-12, warning=FALSE-------------------------------------------------------------------------------------- +my.formula <- y ~ x + I(x^2) +p3 + aes(colour = factor(cyl)) + + stat_poly_line(formula = my.formula, colour = "black") + + stat_poly_eq(formula = my.formula, mapping = use_label(c("eq", "F")), + colour = "black", label.x = "right") + + +## ----smooth-plot-13----------------------------------------------------------------------------------------------------- +ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg, colour = factor(cyl))) + + stat_poly_line(formula = my.formula, colour = "black") + + stat_fit_tb(method.args = list(formula = my.formula), + colour = "black", + parse = TRUE, + tb.vars = c(Parameter = "term", + Estimate = "estimate", + "s.e." = "std.error", + "italic(t)" = "statistic", + "italic(P)" = "p.value"), + label.y = "top", label.x = "right") + + geom_point() + + expand_limits(y = 40) + + +## ----histogram-plot-00-------------------------------------------------------------------------------------------------- +set.seed(54321) +my.data <- + data.frame(X = rnorm(600), + Y = c(rnorm(300, -1, 1), rnorm(300, 1, 1)), + group = factor(rep(c("A", "B"), c(300, 300))) ) + + +## ----histogram-plot-01-------------------------------------------------------------------------------------------------- +ggplot(data = my.data, mapping = aes(x = X)) + + geom_histogram(bins = 15) + + +## ----histogram-plot-04, eval=eval_plots_all----------------------------------------------------------------------------- +ggplot(data = my.data, + mapping = aes(x = Y, fill = group)) + + stat_bin(bins = 15, position = "dodge") + + +## ----echo=FALSE, include=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_2fig_very_wide) + + +## ----histogram-plot-02-------------------------------------------------------------------------------------------------- +p.base <- + ggplot(data = my.data, + mapping = aes(x = Y, fill = group)) + + +## ----histogram-plot-02a------------------------------------------------------------------------------------------------- +p1 <- p.base + geom_histogram(bins = 15, position = "dodge") + + +## ----histogram-plot-03-------------------------------------------------------------------------------------------------- +p2 <- p.base + geom_histogram(mapping = aes(y = after_stat(density)), + bins = 15, position = "dodge") + + +## ----histogram-plot-03a------------------------------------------------------------------------------------------------- +p1 + p2 + + +## ----echo=FALSE, include=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_fig_wide) + + +## ----bin2d-plot-01a----------------------------------------------------------------------------------------------------- +p.base <- + ggplot(data = my.data, + mapping = aes(x = X, y = Y)) + + facet_wrap(facets = vars(group)) + + +## ----bin2d-plot-01------------------------------------------------------------------------------------------------------ +p.base + stat_bin2d(bins = 8) + + +## ----hex-plot-01-------------------------------------------------------------------------------------------------------- +p.base + stat_bin_hex(bins = 8) + + +## ----echo=FALSE, include=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_2fig_very_wide) + + +## ----density-plot-01---------------------------------------------------------------------------------------------------- +p3 <- + ggplot(data = my.data, + mapping = aes(x = Y, colour = group, fill = group)) + + geom_density(alpha = 0.3) + + +## ----density-plot-02---------------------------------------------------------------------------------------------------- +p4 <- + ggplot(data = my.data, + mapping = aes(x = X, colour = group, fill = group)) + + geom_density(alpha = 0.3) + + +## ----density-plot-03---------------------------------------------------------------------------------------------------- +p3 + p4 # plot composition + + +## ----echo=FALSE, include=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_fig_narrow_square) + + +## ----density-plot-10, out.width='.46\\textwidth'------------------------------------------------------------------------ +ggplot(data = my.data, + mapping = aes(x = X, y = Y, colour = group)) + + stat_density_2d() + + +## ----echo=FALSE, include=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_fig_wide) + + +## ----density-plot-12---------------------------------------------------------------------------------------------------- +ggplot(data = my.data, + mapping = aes(x = X, y = Y)) + + stat_density_2d(aes(fill = after_stat(level)), geom = "polygon") + + facet_wrap(facets = vars(group)) + + +## ----echo=FALSE, include=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_fig_narrow) + + +## ----echo=FALSE--------------------------------------------------------------------------------------------------------- +opts_chunk$set(opts_2fig_very_wide) + + +## ----bw-plot-00--------------------------------------------------------------------------------------------------------- +p.base <- + ggplot(data = my.data[c(TRUE, rep(FALSE, 5)) , ], + mapping = aes(x = group, y = Y)) + + +## ----bw-plot-01--------------------------------------------------------------------------------------------------------- +p1 <- p.base + stat_boxplot() + + +## ----bw-plot-02--------------------------------------------------------------------------------------------------------- +p2 <- + p.base + + stat_boxplot(notch = TRUE, width = 0.4, + outlier.colour = "red", outlier.shape = "*", outlier.size = 5) + + +## ----bw-plot-03--------------------------------------------------------------------------------------------------------- +p1 + p2 + + +## ----violin-plot-02----------------------------------------------------------------------------------------------------- +p3 <- p.base + + geom_violin(aes(fill = group), alpha = 0.16) + + geom_point(alpha = 0.33, size = 1.5, colour = "black", shape = 21) + + +## ----ggbeeswarm-plot-01------------------------------------------------------------------------------------------------- +p4 <- p.base + geom_quasirandom() + + +## ----ggbeeswarm-plot-012------------------------------------------------------------------------------------------------ +p3 + p4 + + +## ----echo=FALSE, include=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_fig_medium) + + +## ----echo=FALSE, include=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_2fig_very_wide) + + +## ----flipping_box-01a-ggplot-------------------------------------------------------------------------------------------- +p.base <- + ggplot(data = mtcars[1:8, ], mapping = aes(x = hp, y = mpg)) + + geom_point() +p1 <- p.base + geom_line() + ggtitle("Not flipped") +p2 <- p.base + geom_line(orientation = "y") + ggtitle("Flipped") +p1 + p2 + + +## ----flipping-01-ggplot------------------------------------------------------------------------------------------------- +p3 <- + ggplot(data = iris, mapping = aes(x = Species, y = Sepal.Length)) + + stat_boxplot() + + +## ----flipping-02-ggplot------------------------------------------------------------------------------------------------- +p4 <- + ggplot(data = iris, mapping = aes(x = Sepal.Length, y = Species)) + + stat_boxplot() + + +## ----flipping-01-02-ggplot---------------------------------------------------------------------------------------------- +p3 + p4 + + +## ----flipping-03-ggplot------------------------------------------------------------------------------------------------- +p5 <- + ggplot(data = iris, + mapping = aes(x = Sepal.Length, colour = Species)) + + stat_density(geom = "line", position = "identity") + + +## ----flipping-04-ggplot------------------------------------------------------------------------------------------------- +p6 <- + ggplot(data = iris, + mapping = aes(y = Sepal.Length, colour = Species)) + + stat_density(geom = "line", position = "identity") + + +## ----flipping-03-04-ggplot---------------------------------------------------------------------------------------------- +p5 + p6 + + +## ----echo=FALSE, include=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_fig_narrow) + + +## ----echo=FALSE, include=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_2fig_very_wide) + + +## ----flipping-05base-ggplot--------------------------------------------------------------------------------------------- +p.base <- + ggplot(data = iris, + mapping = aes(x = Sepal.Length, y = Petal.Length)) + + geom_point() + + facet_wrap(~Species, scales = "free") + + +## ----flipping-05-ggplot------------------------------------------------------------------------------------------------- +p.base + stat_smooth(method = "lm", formula = y ~ x) + + +## ----flipping-06-ggplot------------------------------------------------------------------------------------------------- +p.base + stat_smooth(method = "lm", formula = y ~ x, orientation = "y") + + +## ----flipping-06a-ggplot------------------------------------------------------------------------------------------------ +p.base + + stat_smooth(method = "lm", formula = y ~ x) + + coord_flip() + + +## ----flipping-07-ggpmisc------------------------------------------------------------------------------------------------ +p.base + + stat_poly_line() + + stat_poly_line(formula = x ~ y, colour = "red", fill = "yellow") + + +## ----flipping-08-ggpmisc, message=FALSE, warning=FALSE------------------------------------------------------------------ +p.base + stat_ma_line() + + +## ----echo=FALSE, include=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_fig_narrow) + + +## ----echo=FALSE--------------------------------------------------------------------------------------------------------- +opts_chunk$set(opts_fig_wide) + + +## ----facets-00---------------------------------------------------------------------------------------------------------- +p.base <- + ggplot(data = mtcars, + mapping = aes(x = wt, y = mpg)) + + geom_point() +p.base + + +## ----facets-01---------------------------------------------------------------------------------------------------------- +p.base + facet_grid(cols = vars(cyl)) + + +## ----facets-01a, eval=eval_plots_all------------------------------------------------------------------------------------ +p.base + facet_wrap(facets = vars(cyl), nrow = 1) + + +## ----facets-02a--------------------------------------------------------------------------------------------------------- +p.base + facet_wrap(facets = vars(cyl), nrow = 1, scales = "free_y") + + +## ----facets-02b, eval = FALSE, include = FALSE-------------------------------------------------------------------------- +## p.base + facet_grid(cols = vars(cyl), scales = "free_y", space = "free_y") + + +## ----facets-06---------------------------------------------------------------------------------------------------------- +p.base + facet_grid(cols = vars(cyl), margins = TRUE) + + +## ----echo=FALSE--------------------------------------------------------------------------------------------------------- +opts_chunk$set(opts_fig_narrow_square) + + +## ----facets-05---------------------------------------------------------------------------------------------------------- +p.base + facet_grid(rows = vars(vs), cols = vars(am), labeller = label_both) + + +## ----echo=FALSE--------------------------------------------------------------------------------------------------------- +opts_chunk$set(opts_fig_wide) + + +## ----facets-07---------------------------------------------------------------------------------------------------------- +p.base + facet_grid(cols = vars(vs, am), labeller = label_both) + + +## ----echo=FALSE--------------------------------------------------------------------------------------------------------- +opts_chunk$set(opts_fig_narrow_square) + + +## ----facets-13---------------------------------------------------------------------------------------------------------- +p.base + facet_wrap(facets = vars(cyl), nrow = 2) + + +## ----echo=FALSE--------------------------------------------------------------------------------------------------------- +opts_chunk$set(opts_fig_wide) + + +## ----facets-11, eval=eval_plots_all------------------------------------------------------------------------------------- +mtcars$cyl12 <- factor(mtcars$cyl, + labels = c("alpha", "beta", "sqrt(x, y)")) +ggplot(data = mtcars, + mapping = aes(mpg, wt)) + + geom_point() + + facet_grid(cols = vars(cyl12), labeller = label_parsed) + + +## ----facets-12---------------------------------------------------------------------------------------------------------- +p.base + + facet_grid(cols = vars(cyl), + labeller = label_bquote(cols = .(cyl)~"cylinders")) + + +## ----echo=FALSE, include=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_2fig_very_wide) + + +## ----position-01-------------------------------------------------------------------------------------------------------- +p.base <- + ggplot(data = Orange, + mapping = aes(x = age, y = circumference, fill = Tree)) + + +## ----position-02-------------------------------------------------------------------------------------------------------- +p1 <- p.base + geom_area(position = "stack", colour = "white", linewidth = 1) + + ggtitle("stack") +p2 <- p.base + geom_area(position = "fill", colour = "white", linewidth = 1) + + ggtitle("fill") + + +## ----position-03-------------------------------------------------------------------------------------------------------- +p1 + p2 + + +## ----position-04-------------------------------------------------------------------------------------------------------- +p.base <- + ggplot(data = mtcars, + mapping = aes(x = factor(cyl), y = mpg)) + + geom_point(colour = "blue") +p3 <- p.base + + geom_point_s(position = position_jitter_keep(width = 0.35, heigh = 0.6), + colour = "red") + + ggtitle("jitter") + + +## ----position-05-------------------------------------------------------------------------------------------------------- +p4 <- p.base + + geom_point_s(position = position_nudge_keep(x = 0.25, y = 1), + colour = "red") + + ggtitle("nudge") + + +## ----position-06-------------------------------------------------------------------------------------------------------- +p3 + p4 + + +## ----echo=FALSE, include=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_fig_narrow) + + +## ----axis-labels-00----------------------------------------------------------------------------------------------------- +p.base <- + ggplot(data = Orange, + mapping = aes(x = age, y = circumference, colour = Tree)) + + geom_line() + + geom_point() + + +## ----axis-labels-01----------------------------------------------------------------------------------------------------- +p.base + + expand_limits(y = 0) + + labs(title = "Growth of orange trees", + subtitle = "Starting from 1968-12-31", + caption = "see Draper, N. R. and Smith, H. (1998)", + tag = "A", + alt = "A data plot", + x = "Time (d)", + y = "Circumference (mm)", + colour = "Tree\nnumber") + + +## ----axis-labels-02----------------------------------------------------------------------------------------------------- +p.base + + expand_limits(y = 0) + + scale_x_continuous(name = "Time (d)") + + scale_y_continuous(name = "Circumference (mm)") + + ggtitle(label = "Growth of orange trees", + subtitle = "Starting from 1968-12-31") + + +## ----scales-01---------------------------------------------------------------------------------------------------------- +fake2.data <- + data.frame(y = c(rnorm(20, mean = 20, sd = 5), + rnorm(20, mean = 40, sd = 10)), + group = factor(c(rep("A", 20), rep("B", 20))), + z = rnorm(40, mean = 12, sd = 6)) + + +## ----echo=FALSE--------------------------------------------------------------------------------------------------------- +opts_chunk$set(opts_2fig_very_wide) + + +## ----scale-limits-00---------------------------------------------------------------------------------------------------- +p1.base <- + ggplot(data = fake2.data, mapping = aes(x = z, y = y)) + + geom_point() + + +## ----scale-limits-01---------------------------------------------------------------------------------------------------- +p1 <- p1.base + scale_y_continuous(limits = c(0, 100)) + + +## ----scale-limits-02---------------------------------------------------------------------------------------------------- +p2 <-p1.base + scale_y_continuous(limits = c(50, NA)) + + +## ----scale-limits-02a--------------------------------------------------------------------------------------------------- +p1 + p2 + + +## ----scale-limits-03, eval=eval_plots_all------------------------------------------------------------------------------- +p1.base + ylim(50, NA) + + +## ----echo=FALSE, include=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_fig_medium) + + +## ----scale-limits-04---------------------------------------------------------------------------------------------------- +p1.base + expand_limits(y = 0, x = 0) + + +## ----echo=FALSE--------------------------------------------------------------------------------------------------------- +opts_chunk$set(opts_2fig_very_wide) + + +## ----scale-limits-05---------------------------------------------------------------------------------------------------- +p2.base <- + ggplot(data = fake2.data, + mapping = aes(fill = group, colour = group, x = y)) + + stat_density(alpha = 0.3, position = "identity") + + +## ----scale-limits-05a--------------------------------------------------------------------------------------------------- +p1 <- + p2.base + scale_y_continuous(expand = expansion(add = c(0, 0.01))) + + +## ----scale-limits-06---------------------------------------------------------------------------------------------------- +p2 <- + p2.base + scale_y_continuous(expand = expansion(mult = c(0, 0.1))) + + +## ----scale-limits-07---------------------------------------------------------------------------------------------------- +p1 + p2 + + +## ----scale-limits-PG01, eval=eval_playground---------------------------------------------------------------------------- +p1.base <- scale_y_continuous(limits = c(100, 0)) + + +## ----scale-ticks-00----------------------------------------------------------------------------------------------------- +p3.base <- + ggplot(data = fake2.data, mapping = aes(x = z, y = y)) + + geom_point() + + +## ----scale-ticks-01, eval=eval_plots_all-------------------------------------------------------------------------------- +p3.base + scale_y_continuous(breaks = c(20, pi * 10, 40, 60)) + + +## ----scale-ticks-01a---------------------------------------------------------------------------------------------------- +p3 <- + p3.base + scale_y_continuous(breaks = pretty_breaks(n = 7)) + + +## ----scale-ticks-02----------------------------------------------------------------------------------------------------- +p4 <- + p3.base + + scale_y_continuous(breaks = c(20, pi * 10, 40, 60), + labels = c("20", expression(10*pi), "40", "60")) + + +## ----scale-ticks-02a---------------------------------------------------------------------------------------------------- +p3 + p4 + + +## ----scale-ticks-03----------------------------------------------------------------------------------------------------- +p5 <- + ggplot(data = fake2.data, mapping = aes(x = z, y = y / max(y))) + + geom_point() + + scale_y_continuous(labels = percent) + + +## ----scale-ticks-04----------------------------------------------------------------------------------------------------- +p6 <- + ggplot(data = fake2.data, mapping = aes(x = z, y = y * 1000)) + + geom_point() + + scale_y_continuous(name = "Mass", + labels = label_number(scale_cut = cut_si("g"))) + + +## ----scale-ticks-05----------------------------------------------------------------------------------------------------- +p5 + p6 + + +## ----echo=FALSE, include=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_fig_medium) + + +## ----scale-trans-02----------------------------------------------------------------------------------------------------- +ggplot(data = fake2.data, mapping = aes(x = z, y = y)) + + geom_point() + + scale_y_log10(breaks=c(10,20,50,100)) + + +## ----scale-trans-03, eval=eval_plots_all-------------------------------------------------------------------------------- +ggplot(data = fake2.data, mapping = aes(x = z, y = log10(y))) + + geom_point() + + +## ----scale-trans-04, eval=eval_plots_all-------------------------------------------------------------------------------- +ggplot(data = fake2.data, mapping = aes(x = z, y = y)) + + geom_point() + + scale_y_continuous(trans = "reciprocal") + + +## ----echo=FALSE, include=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_fig_wide) + + +## ----scale-trans-05----------------------------------------------------------------------------------------------------- +ggplot(data = Orange, + mapping = aes(x = age, y = circumference, colour = Tree)) + + geom_line() + + geom_point() + + scale_y_continuous(trans = "log", breaks = c(20, 50, 100, 200)) + + +## ----axis-position-01--------------------------------------------------------------------------------------------------- +ggplot(data = mtcars, mapping = aes(x = wt, y = mpg)) + + geom_point() + + scale_x_continuous(position = "top") + + scale_y_continuous(position = "right") + + +## ----axis-secondary-01-------------------------------------------------------------------------------------------------- +ggplot(data = mtcars, mapping = aes(x = wt, y = mpg)) + + geom_point() + + scale_y_continuous(sec.axis = sec_axis(~ . ^-1, name = "gpm") ) + + +## ----axis-secondary-02, eval=eval_plots_all----------------------------------------------------------------------------- +ggplot(data = mtcars, mapping = aes(x = wt, y = mpg)) + + geom_point() + + scale_y_continuous(sec.axis = sec_axis(~ . / 2.3521458, + name = expression(km / l), + breaks = c(5, 7.5, 10, 12.5))) + + +## ----echo=FALSE, include=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_fig_very_wide) + + +## ----scale-datetime-01-------------------------------------------------------------------------------------------------- +ggplot(data = weather_wk_25_2019.tb, + mapping = aes(x = with_tz(time, tzone = "EET"), + y = air_temp_C)) + + geom_line(na.rm = TRUE) + + scale_x_datetime(name = NULL, + breaks = ymd_h("2019-06-11 12", tz = "EET") + days(0:1), + limits = ymd_h("2019-06-11 00", tz = "EET") + days(c(0, 2))) + + scale_y_continuous(name = "Air temperature (C)") + + expand_limits(y = 0) + + +## ----scale-datetime-02-------------------------------------------------------------------------------------------------- +ggplot(data = weather_wk_25_2019.tb, + mapping = aes(x = with_tz(time, tzone = "EET"), + y = air_temp_C)) + + geom_line(na.rm = TRUE) + + scale_x_datetime(name = NULL, + date_breaks = "1 hour", + limits = ymd_h("2019-06-16 00", tz = "EET") + hours(c(6, 18)), + date_labels = "%H:%M") + + scale_y_continuous(name = "Air temperature (C)") + + expand_limits(y = 0) + + +## ----echo=FALSE, include=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_fig_narrow) + +## ----scale-discrete-10, eval=eval_plots_all----------------------------------------------------------------------------- +ggplot(data = mpg, + mapping = aes(x = class, y = hwy)) + + stat_summary(geom = "col", fun = mean, na.rm = TRUE) + + scale_x_discrete(limits = c("compact", "subcompact", "midsize"), + labels = c("COMPACT", "SUBCOMPACT", "MIDSIZE")) + + +## ----scale-discrete-10a------------------------------------------------------------------------------------------------- +ggplot(data = mpg, + mapping = aes(x = class, y = hwy)) + + stat_summary(geom = "col", fun = mean, na.rm = TRUE, width = 0.6) + + scale_x_discrete(name = "Vehicle class", + limits = c("compact", "subcompact", "midsize"), + labels = toupper) + + scale_y_continuous(name = "Petrol use efficiency (mpg)", limits = c(0, 30)) + + +## ----scale-discrete-11, eval=eval_plots_all----------------------------------------------------------------------------- +ggplot(data = mpg, + mapping = aes(x = reorder(x = factor(class), X = hwy, FUN = mean), + y = hwy)) + + stat_summary(geom = "col", fun = mean) + + +## ----echo=FALSE, include=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_fig_wide) + + +## ----scale-colour-01---------------------------------------------------------------------------------------------------- +length(colors()) +grep("dark",colors(), value = TRUE) + + +## ----scale-colour-02---------------------------------------------------------------------------------------------------- +col2rgb("purple") +col2rgb("#FF0000") + + +## ----scale-colour-03---------------------------------------------------------------------------------------------------- +col2rgb("purple", alpha = TRUE) + + +## ----scale-colour-04---------------------------------------------------------------------------------------------------- +rgb(1, 1, 0) +rgb(1, 1, 0, names = "my.color") +rgb(255, 255, 0, names = "my.color", maxColorValue = 255) + + +## ----scale-colour-05---------------------------------------------------------------------------------------------------- +hsv(c(0,0.25,0.5,0.75,1), 0.5, 0.5) + + +## ----scale-colour-06---------------------------------------------------------------------------------------------------- +hcl(c(0,0.25,0.5,0.75,1) * 360) + + +## ----mapping-stage-01--------------------------------------------------------------------------------------------------- +set.seed(4321) +X <- 0:10 +Y <- (X + X^2 + X^3) + rnorm(length(X), mean = 0, sd = mean(X^3) / 4) +df1 <- data.frame(X, Y) +df2 <- df1 +df2[6, "Y"] <-df1[6, "Y"] * 10 + + +## ----mapping-stage-01a-------------------------------------------------------------------------------------------------- + + +## ----binned-scales-01--------------------------------------------------------------------------------------------------- +ggplot(data = df2) + + stat_fit_residuals(formula = y ~ poly(x, 3, raw = TRUE), + method = "rlm", + mapping = aes(x = X, + y = stage(start = Y, + after_stat = y * weights), + colour = after_stat(weights)), + show.legend = TRUE) + + scale_colour_binned(low = "red", high = "blue", limits = c(0, 1), + guide = "colourbar", n.breaks = 5) + + +## ----scale-colour-10---------------------------------------------------------------------------------------------------- +df3 <- data.frame(X = 1:10, Y = dnorm(10), colours = rep(c("red", "blue"), 5)) + +ggplot(data = df3, mapping = aes(x = X, y = Y, colour = colours)) + + geom_point() + + scale_colour_identity() + + +## ----annotate-01-------------------------------------------------------------------------------------------------------- +ggplot(data = fake2.data, mapping = aes(x = z, y = y)) + + geom_point() + + annotate(geom = "text", + label = "origin", + x = 0, y = 0, + colour = "blue", + size=4) + + +## ----inset-01----------------------------------------------------------------------------------------------------------- +p <- ggplot(data = fake2.data, mapping = aes(x = z, y = y)) + + geom_point() +p + expand_limits(x = 40) + + annotation_custom(ggplotGrob(p + coord_cartesian(xlim = c(4, 10), ylim = c(20, 30)) + + theme_bw(10)), + xmin = 25, xmax = 40, ymin = 30, ymax = 60) + + +## ----annotate-03-------------------------------------------------------------------------------------------------------- +ggplot(data = data.frame(x = c(0, 2 * pi)), + mapping = aes(x = x)) + + stat_function(fun = sin) + + scale_x_continuous( + breaks = c(0, 0.5, 1, 1.5, 2) * pi, + labels = c("0", expression(0.5~pi), expression(pi), + expression(1.5~pi), expression(2~pi))) + + labs(y = "sin(x)") + + annotate(geom = "text", + label = c("+", "-"), + x = c(0.5, 1.5) * pi, y = c(0.5, -0.5), + size = 20) + + annotate(geom = "point", + colour = "red", + shape = 21, + fill = "white", + x = c(0, 1, 2) * pi, y = 0, + size = 6) + + +## ----wind-05------------------------------------------------------------------------------------------------------------ +p.wind <- + ggplot(data = viikki_d29.dat, + mapping = aes(x = WindDir_D1_WVT)) + + stat_bin(colour = "black", fill = "gray50", geom = "bar", + binwidth = 30, boundary = 0, na.rm = TRUE) + + coord_polar() + + scale_x_continuous(breaks = c(0, 90, 180, 270), + labels = c("N", "E", "S", "W"), + limits = c(0, 360), + expand = c(0, 0), + name = "Wind direction") + + scale_y_continuous(name = "Frequency (min/d)") +p.wind + + +## ----echo=FALSE--------------------------------------------------------------------------------------------------------- +opts_chunk$set(opts_fig_very_wide) + + +## ----wind-08------------------------------------------------------------------------------------------------------------ +p.wind + + facet_wrap(~factor(ifelse(hour(solar_time) < 12, "AM", "PM"))) + + +## ----echo=FALSE--------------------------------------------------------------------------------------------------------- +opts_chunk$set(opts_fig_narrow) + + +## ----------------------------------------------------------------------------------------------------------------------- +ggplot(data = mpg, + mapping = aes(x = factor(1), fill = factor(class))) + + geom_bar(width = 1, colour = "black") + + coord_polar(theta = "y") + + scale_fill_brewer() + + scale_x_discrete(breaks = NULL) + + labs(x = NULL, fill = "Vehicle class") + + +## ----themes-01---------------------------------------------------------------------------------------------------------- +ggplot(data = fake2.data, + mapping = aes(x = z, y = y)) + + geom_point() + + theme_gray(base_size = 18, + base_family = "serif") + + +## ----themes-03---------------------------------------------------------------------------------------------------------- +p.base <- + ggplot(data = fake2.data, + mapping = aes(x = z, y = y)) + + geom_point() +print(p.base + theme_bw()) + + +## ----themes-05, eval=eval_plots_all------------------------------------------------------------------------------------- +old_theme <- theme_set(theme_bw(15)) + + +## ----themes-06, eval=eval_plots_all------------------------------------------------------------------------------------- +theme_set(old_theme) + + +## ----themes-11---------------------------------------------------------------------------------------------------------- +ggplot(data = fake2.data, + mapping = aes(x = z + 1000, y = y)) + + geom_point() + + scale_x_continuous(breaks = scales::pretty_breaks(n = 8)) + + theme(axis.text.x = element_text(angle = 33, hjust = 1, vjust = 1)) + + +## ----themes-12, eval=eval_plots_all------------------------------------------------------------------------------------- +ggplot(fake2.data, aes(z + 100, y)) + + geom_point() + + scale_x_continuous(breaks = scales::pretty_breaks(n = 20)) + + theme(axis.text.x = element_text(size = rel(0.6))) + + +## ----themes-15, eval=eval_plots_all------------------------------------------------------------------------------------- +old_theme <- theme_update(text = element_text(colour = "darkred")) + + +## ----themes-16, eval=eval_plots_all------------------------------------------------------------------------------------- +theme_set(old_theme) + + +## ----themes-21---------------------------------------------------------------------------------------------------------- +my_theme <- theme_bw(15) + theme(text = element_text(colour = "darkred")) +p.base + my_theme + + +## ----themes-32---------------------------------------------------------------------------------------------------------- +my_theme_gray <- + function (base_size = 11, + base_family = "serif", + base_line_size = base_size/22, + base_rect_size = base_size/22, + base_colour = "darkblue") { + + theme_gray(base_size = base_size, + base_family = base_family, + base_line_size = base_line_size, + base_rect_size = base_rect_size) + + + theme(line = element_line(colour = base_colour), + rect = element_rect(colour = base_colour), + text = element_text(colour = base_colour), + title = element_text(colour = base_colour), + axis.text = element_text(colour = base_colour), + complete = TRUE) + } + + +## ----themes-32a--------------------------------------------------------------------------------------------------------- +my_theme_grey <- my_theme_gray + + +## ----themes-33---------------------------------------------------------------------------------------------------------- +p.base + my_theme_gray(15, base_colour = "darkred") + + +## ----patchwork-01------------------------------------------------------------------------------------------------------- +p1 <- ggplot(mpg, aes(displ, cty, colour = factor(cyl))) + + geom_point() + + theme(legend.position = "top") +p2 <- ggplot(mpg, aes(displ, cty, colour = factor(year))) + + geom_point() + + theme(legend.position = "top") +p3 <- ggplot(mpg, aes(factor(model), cty)) + + geom_point() + + theme(axis.text.x = + element_text(angle = 90, hjust = 1, vjust = 0.5)) + + +## ----patchwork-00a, echo=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_fig_very_wide_square) + + +## ----patchwork-02, eval=eval_plots_all---------------------------------------------------------------------------------- +p1 | (p2 / p3) + + +## ----patchwork-02a, eval=eval_plots_all--------------------------------------------------------------------------------- +(p1 | p2) / p3 + + +## ----patchwork-03------------------------------------------------------------------------------------------------------- +((p1 | p2) / p3) + + plot_annotation(title = "Fuel use in city traffic:", tag_levels = 'a') + + +## ----patchwork-00b, echo=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_fig_wide) + + +## ----plotmath-00-------------------------------------------------------------------------------------------------------- +p1 + labs(y = expression("Fuel use"~~(m~g^{-1})), + x = "Engine displacement (L)", + colour = "Engine\ncylinders") + + theme(legend.position = "right") + + +## ----plotmath-01-------------------------------------------------------------------------------------------------------- +set.seed(54321) # make sure we always generate the same data +my.data <- + data.frame(x = 1:5, + y = rnorm(5), + greek.label = paste("alpha[", 1:5, "]", sep = "")) + + +## ----plotmath-02-------------------------------------------------------------------------------------------------------- +ggplot(my.data, aes(x, y, label = greek.label)) + + geom_point() + + geom_text(angle = 45, hjust = 1.2, parse = TRUE) + + labs(x = expression(alpha[i]), + y = expression(Speed~~(m~s^{-1})), + title = "Using expressions", + subtitle = expression(sqrt(alpha[1] + frac(beta, gamma)))) + + +## ----plotmath-02a------------------------------------------------------------------------------------------------------- +my_eq.char <- "alpha[i]" +ggplot(my.data, aes(x, y)) + + geom_point() + + labs(title = parse(text = my_eq.char)) + + scale_x_continuous(name = expression(alpha[i]), + breaks = c(1,3,5), + labels = expression(alpha[1], alpha[3], alpha[5])) + + +## ----expr-parse-box-01, eval=eval_plots_all----------------------------------------------------------------------------- +ggplot(cars, aes(speed, dist)) + + geom_point() + + xlab(expression(x[1]*" test")) + + +## ----expr-parse-box-02, eval=eval_plots_all----------------------------------------------------------------------------- +ggplot(cars, aes(speed, dist)) + + geom_point() + + xlab(parse(text = "x[1]*\" test\"")) + + +## ----expr-parse-box-03, eval=eval_plots_all----------------------------------------------------------------------------- +ggplot(cars, aes(speed, dist)) + + geom_point() + + xlab(parse(text = "x[1]*italic(\" test\")")) + + +## ----expr-parse-box-06, eval=eval_plots_all----------------------------------------------------------------------------- +ggplot(cars, aes(speed, dist)) + + geom_point() + + xlab(expression(x[1], " test")) + + +## ----expr-parse-box-07, eval=eval_plots_all, echo=3--------------------------------------------------------------------- +ggplot(cars, aes(speed, dist)) + + geom_point() + + xlab(parse(text = "x[1]~~~~\"test\"")) + + +## ----expr-parse-box-08, eval=eval_plots_all, echo=3--------------------------------------------------------------------- +ggplot(cars, aes(speed, dist)) + + geom_point() + + xlab(parse(text = "x[1]~~~~plain(test)")) + + +## ----expr-parse-box-09, eval=eval_plots_all, echo=3--------------------------------------------------------------------- +ggplot(cars, aes(speed, dist)) + + geom_point() + + xlab(parse(text = "x[1]*plain( test)")) + + +## ----sprintf-01--------------------------------------------------------------------------------------------------------- +sprintf("log(%.3f) = %.3f", 5, log(5)) +sprintf("log(%.3g) = %.3g", 5, log(5)) + + +## ----expr-bquote-01----------------------------------------------------------------------------------------------------- +ggplot(cars, aes(speed, dist)) + + geom_point() + + labs(title = bquote(Time~zone: .(Sys.timezone())), + subtitle = bquote(Date: .(as.character(today()))) + ) + + +## ----expr-substitute-01------------------------------------------------------------------------------------------------- +ggplot(cars, aes(speed, dist)) + + geom_point() + + labs(title = substitute(Time~zone: tz, list(tz = Sys.timezone())), + subtitle = substitute(Date: date, list(date = as.character(today()))) + ) + + +## ----expr-deparse-01---------------------------------------------------------------------------------------------------- +deparse_test <- function(x) { + print(deparse(substitute(x))) +} + +a <- "saved in variable" + +deparse_test("constant") +deparse_test(1 + 2) +deparse_test(a) + + +## ----plot_composition-01, eval=eval_plots_all--------------------------------------------------------------------------- +p.base <- ggplot(data = mtcars, + aes(x = disp, y = mpg, + colour = factor(cyl))) + + geom_point() + +p.labels <- labs(x = "Engine displacement)", + y = "Gross horsepower", + colour = "Number of\ncylinders", + shape = "Number of\ncylinders") + + +## ----plot_composition-02, eval=eval_plots_all--------------------------------------------------------------------------- +p.base +p.base + p.labels + theme_bw(16) +p.base + p.labels + theme_bw(16) + ylim(0, NA) + + +## ----plot_composition-03, eval=eval_plots_all--------------------------------------------------------------------------- +p.log <- p.base + scale_y_log10(limits=c(8,55)) +p.log + p.labels + theme_bw(16) + + +## ----plot_composition-11, eval=eval_plots_all--------------------------------------------------------------------------- +p.parts <- list(p.labels, theme_bw(16)) +p1 + p.parts + + +## ----plot_composition-21, eval=eval_plots_all--------------------------------------------------------------------------- +bw_ggplot <- function(...) { + ggplot(...) + + theme_bw() +} + + +## ----plot_composition-22, eval=eval_plots_all--------------------------------------------------------------------------- +bw_ggplot(data = mtcars, + mapping = aes(x = disp, y = mpg, + colour = factor(cyl))) + + geom_point() + + +## ----plot-file-01, eval=eval_plots_all---------------------------------------------------------------------------------- +fig1 <- ggplot(data.frame(x = -3:3), aes(x = x)) + + stat_function(fun = dnorm) +pdf(file = "fig1.pdf", width = 8, height = 6) +print(fig1) +dev.off() + + +## ----plot-file-02, eval=eval_plots_all---------------------------------------------------------------------------------- +postscript(file = "fig1.eps", width = 8, height = 6) +print(fig1) +dev.off() + + +## ----plot-file-03, eval=eval_plots_all---------------------------------------------------------------------------------- +tiff(file = "fig1.tiff", width = 1000, height = 800) +print(fig1) +dev.off() + + +## ----ggplot-debug-01---------------------------------------------------------------------------------------------------- +ggplot(data = iris, mapping = aes(x = Petal.Length, y = Species)) + + stat_summary(geom = "debug") + + +## ----ggplot-debug-02---------------------------------------------------------------------------------------------------- +ggplot(data = iris, mapping = aes(x = Petal.Length)) + + stat_bin(geom = "debug") + + +## ----echo=FALSE--------------------------------------------------------------------------------------------------------- +try(detach(package:learnrbook)) +try(detach(package:ggbeeswarm)) +try(detach(package:ggpmisc)) +try(detach(package:ggpp)) +try(detach(package:gginnards)) +try(detach(package:ggrepel)) +try(detach(package:ggplot2)) +try(detach(package:scales)) +try(detach(package:lubridate)) +try(detach(package:dplyr)) +try(detach(package:tibble)) + + +## ----eval=eval_diag, include=eval_diag, echo=eval_diag, cache=FALSE----------------------------------------------------- +## knitter_diag() +## R_diag() +## other_diag() + diff --git a/learn-r-2ed-purl/R.scripts.R b/learn-r-2ed-purl/R.scripts.R new file mode 100644 index 00000000..c13f01ad --- /dev/null +++ b/learn-r-2ed-purl/R.scripts.R @@ -0,0 +1,836 @@ +## ----echo=FALSE, cache=FALSE-------------------------------------------------------------------------------------------- +set_parent('r4p.main.Rnw') +opts_knit$set(concordance=TRUE) +opts_knit$set(unnamed.chunk.label = 'scripts-chunk') + + +## ----setup-scripts, include=FALSE, cache=FALSE-------------------------------------------------------------------------- +show.results <- FALSE + + +## ----evaluate=FALSE----------------------------------------------------------------------------------------------------- +source("my.first.script.r") + + +## ----eval=eval_playground----------------------------------------------------------------------------------------------- +a <- 2 # height +b <- 4 # length +C <- + a * +b +C -> variable + print( +"area: ", variable +) + + +## ----compound-1--------------------------------------------------------------------------------------------------------- +print("...") +{ + print("A") + print("B") +} +print("...") + + +## ----compound-2--------------------------------------------------------------------------------------------------------- +{1 + 2; 3 + 4} + + +## ----compound-3, eval=eval_playground----------------------------------------------------------------------------------- +{1 + 2; {a <- 3 + 4; a + 1}} + + +## ----fun-calls-01------------------------------------------------------------------------------------------------------- +a <- log10(100) +print(a) + + +## ----fun-calls-02------------------------------------------------------------------------------------------------------- +print(log10(100)) + + +## stdin | grep("abc") | more + +## ----pipes-r-02--------------------------------------------------------------------------------------------------------- +sum(sqrt(1:10)) + + +## ----pipes-r-03--------------------------------------------------------------------------------------------------------- +data.in <- 1:10 +data.tmp <- sqrt(data.in) +sum(data.tmp) +rm(data.tmp) # clean up! + + +## ----pipes-r-04--------------------------------------------------------------------------------------------------------- +1:10 |> sqrt() |> sum() + + +## ----pipes-r-04a-------------------------------------------------------------------------------------------------------- +1:10 |> sqrt() |> sum() -> my_rhs.var +my_rhs.var + + +## ----pipes-r-04b-------------------------------------------------------------------------------------------------------- +my_lhs.var <- 1:10 |> sqrt() |> sum() +my_lhs.var + + +## ----pipes-r-05--------------------------------------------------------------------------------------------------------- +1:10 |> sqrt(x = _) |> sum(x = _) + + +## ----pipes-r-05a-------------------------------------------------------------------------------------------------------- +1:10 |> sqrt(x = _) |> _[2:8] |> sum(x = _) + + +## ----pipes-box-pipes-02------------------------------------------------------------------------------------------------- +obj.name <- "data.out" +1:10 |> sqrt() |> sum() |> assign(x = obj.name, value = _) + + +## ----pipes-box-pipes-03------------------------------------------------------------------------------------------------- +value_assign <- function(value, x, ...) { + assign(x = x, value = value, ...) +} +obj.name <- "data.out" +1:10 |> sqrt() |> sum() |> value_assign(obj.name) + + +## ----pipes-r-06--------------------------------------------------------------------------------------------------------- +data.frame(x = 1:10, y = rnorm(10)) |> + within(data = _, + { + x4 <- x^4 + is.large <- x^4 > 1000 + }) |> + subset(x = _, is.large) + + +## ----pipes-r-06aa, eval=eval_playground--------------------------------------------------------------------------------- +data.frame(x = 1:10, y = rnorm(10)) |> + within({x4 <- x^4; is.large <- x^4 > 1000}) |> + subset(is.large) + + +## ----pipes-r-06a-------------------------------------------------------------------------------------------------------- +data.frame(x = 1:10, y = rnorm(10)) |> + within(data = _, + { + x4 <- x^4 + is.large <- x^4 > 1000 + }) |> + subset(x = _, is.large, select = -x) + + +## ----pipes-r-06b-------------------------------------------------------------------------------------------------------- +data.frame(x = 1:10, y = rnorm(10)) |> + within(data = _, + { + x4 <- x^4 + is.large <- x^4 > 1000 + }) |> + subset(x = _, select = c(y, x4)) + + +## ----pipes-r-07--------------------------------------------------------------------------------------------------------- +data.frame(group = factor(rep(c("T1", "T2", "Ctl"), each = 4)), + y = rnorm(12)) |> + subset(x = _, group %in% c("T1", "T2")) |> + aggregate(data = _, y ~ group, mean) + + +## ----pipes-r-09--------------------------------------------------------------------------------------------------------- +data.frame(group = factor(rep(c("T1", "T2", "Ctl"), each = 4)), + y = rnorm(12)) |> + subset(x = _, group %in% c("T1", "T2")) |> + aggregate(data = _, y ~ group, mean) |> + _[["y"]] + + +## ----if-1z-------------------------------------------------------------------------------------------------------------- +flag <- TRUE +if (flag) print("Hello!") + + +## ----if-1--------------------------------------------------------------------------------------------------------------- +if (FALSE) print("Hello!") + + +## ----if-2--------------------------------------------------------------------------------------------------------------- +printing <- TRUE +if (printing) { + print("A") + print("B") +} + + +## ----if-3--------------------------------------------------------------------------------------------------------------- +a <- 10 +if (a < 0) print("'a' is negative") else print("'a' is not negative") +print("This is always printed") + + +## ----auxiliary, echo=FALSE, include = FALSE, eval=TRUE------------------------------------------------------------------ +show.results <- TRUE +if (show.results) eval.if.4 <- c(1:4) else eval.if.4 <- FALSE +# eval.if.4 +show.results <- FALSE +if (show.results) eval.if.4 <- c(1:4) else eval.if.4 <- FALSE +#eval.if.4 + + +## ----if-4--------------------------------------------------------------------------------------------------------------- +# 1 +a <- 1 +if (a < 0) print("'a' is negative") else print("'a' is not negative") + + +## ----if-4a, eval=FALSE-------------------------------------------------------------------------------------------------- +## # 2 (not evaluated here) +## if (a < 0) print("'a' is negative") +## else print("'a' is not negative") + + +## ----if-4b-------------------------------------------------------------------------------------------------------------- +# 1 +a <- 1 +if (a < 0) { + print("'a' is negative") + } else { + print("'a' is not negative") + } + + +## ----if-4c-------------------------------------------------------------------------------------------------------------- +a <- 1 +my.message <- + if (a < 0) "'a' is negative" else "'a' is not negative" +print(my.message) + + +## ----if-explain_conv---------------------------------------------------------------------------------------------------- +message <- "abc" +if (length(message)) print(message) + + +## ----if-PG-01, eval=FALSE----------------------------------------------------------------------------------------------- +## if (0) print("hello") +## if (-1) print("hello") +## if (0.01) print("hello") +## if (1e-300) print("hello") +## if (1e-323) print("hello") +## if (1e-324) print("hello") +## if (1e-500) print("hello") +## if (as.logical("true")) print("hello") +## if (as.logical(as.numeric("1"))) print("hello") +## if (as.logical("1")) print("hello") +## if ("1") print("hello") + + +## ----------------------------------------------------------------------------------------------------------------------- +my.object <- "two" +b <- switch(my.object, + one = 1, + two = 1 / 2, + four = 1 / 4, + 0 +) +b + + +## ----------------------------------------------------------------------------------------------------------------------- +my.object <- "two" +b <- switch(my.object, + one =, uno = 1, + two =, dos = 1 / 2, + four =, cuatro = 1 / 4, + 0 +) +b + + +## ----------------------------------------------------------------------------------------------------------------------- +my.number <- 2 +b <- switch(my.number, + 1, + 1 / 2, + 1 / 4, + 0 +) +b + + +## ----explain-switch-01-------------------------------------------------------------------------------------------------- +my.object <- "ten" +b <- switch(my.object, + one = 1, + two = 1 / 2, + three = 1 / 4, + {print("No match! Using default"); 0} +) +b + + +## ----explain-switch-11-------------------------------------------------------------------------------------------------- +my.object <- "two" +if (my.object == "one") { + b <- 1 +} else if (my.object == "two") { + b <- 1 / 2 +} else if (my.object == "four") { + b <- 1 / 4 +} else { + b <- 0 +} +b + + +## ----ifelse-0----------------------------------------------------------------------------------------------------------- +my.test <- c(TRUE, FALSE, TRUE, TRUE) +ifelse(test = my.test, yes = 1, no = -1) + + +## ----ifelse-0a---------------------------------------------------------------------------------------------------------- +nums <- -3:+3 +ifelse(nums < 0, -nums, nums) + + +## ----------------------------------------------------------------------------------------------------------------------- +ifelse(TRUE, 1:5, -5:-1) +ifelse(FALSE, 1:5, -5:-1) +ifelse(c(TRUE, FALSE), 1:5, -5:-1) +ifelse(c(FALSE, TRUE), 1:5, -5:-1) +ifelse(c(FALSE, TRUE), 1:5, 0) + + +## ----ifelse-1, eval=eval_playground------------------------------------------------------------------------------------- +a <- 1:10 +ifelse(a > 5, 1, -1) +ifelse(a > 5, a + 1, a - 1) +ifelse(any(a > 5), a + 1, a - 1) # tricky +ifelse(logical(0), a + 1, a - 1) # even more tricky +ifelse(NA, a + 1, a - 1) # as expected + + +## ----ifelse-2, eval=eval_playground------------------------------------------------------------------------------------- +a <- -10:-1 +b <- +1:10 +c <- c(rep("a", 5), rep("b", 5)) +# your code + + +## ----for-00------------------------------------------------------------------------------------------------------------- +b <- 0 # variable needs to set to a valid numeric value! +for (a in 1:5) b <- b + a +b + + +## ----for-unrolled------------------------------------------------------------------------------------------------------- +b <- 0 +# start of loop +# first iteration +a <- 1 +b <- b + a +# second iteration +a <- 2 +b <- b + a +# third iteration +a <- 3 +b <- b + a +# fourth iteration +a <- 4 +b <- b + a +# fifth iteration +a <- 5 +b <- b + a +# end of loop +b + + +## ----for-replaced-by-sum------------------------------------------------------------------------------------------------ +sum(1:5) + + +## ----for-00a------------------------------------------------------------------------------------------------------------ +b <- 0 +for (a in numeric()) b <- b + a +print(b) + + +## ----for-01------------------------------------------------------------------------------------------------------------- +a <- c(1, 4, 3, 6, 8) +for(x in a) { + b <- x*2 + print(b) + } + + +## ----for-02------------------------------------------------------------------------------------------------------------- +b <- for(x in a) x*2 +x +b + + +## ----for-03a------------------------------------------------------------------------------------------------------------ +b <- numeric() # an empty vector +for(i in seq(along.with = a)) { + b[i] <- a[i]^2 +} +b + + +## ----for-03d, eval=eval_playground-------------------------------------------------------------------------------------- +b <- numeric() # an empty vector +for(i in seq(along.with = a)) { + b[i] <- a[i]^2 + print(i) + print(a) + print(b) +} +b + + +## ----for-04, eval=eval_playground--------------------------------------------------------------------------------------- +A <- -5:5 # assign different numeric vector to A +B <- numeric(length(A)) +for(i in seq(along.with = A)) { + B[i] <- A[i]^2 +} +B + +C <- numeric(length(A)) +for(i in 1:length(A)) { + C[i] <- A[i]^2 +} +C + + +## ----for-03c------------------------------------------------------------------------------------------------------------ +b <- a^2 +b + + +## ----for-05------------------------------------------------------------------------------------------------------------- +b <- 0 +a <- -10:100 +idxs <- seq_along(a) +for(i in idxs) { + if (a[i] < 0) next() + b <- b + a[i] + if (b > 100) break() +} +b +i +a[i] + + +## ----while-02----------------------------------------------------------------------------------------------------------- +a <- 2 +while (a < 50) { + print(a) + a <- a^2 +} +print(a) + + +## ----while-03, eval=eval_playground------------------------------------------------------------------------------------- +a <- 2 +print(a) +while (a < 50) print(a <- a^2) + + +## ----while-04, eval=eval_playground------------------------------------------------------------------------------------- +a <- b <- c <- 1:5 +a + + +## ----while-05----------------------------------------------------------------------------------------------------------- +a <- c(1, 4, 3, 6, 8) +b <- numeric() # an empty vector +i <- 1 +while(i <= length(a)) { + b[i] <- a[i]^2 + print(b) + i <- i + 1 +} +b + + +## ----repeat-1----------------------------------------------------------------------------------------------------------- +a <- 2 +repeat{ + print(a) + if (a > 50) break() + a <- a^2 +} + + +## ----nested-1----------------------------------------------------------------------------------------------------------- +A <- matrix(1:50, nrow = 10) +A + + +## ----nested-22---------------------------------------------------------------------------------------------------------- +row.sum <- numeric() +for (i in 1:nrow(A)) { + row.sum[i] <- 0 + for (j in 1:ncol(A)) + row.sum[i] <- row.sum[i] + A[i, j] +} +print(row.sum) + + +## ----apply-00----------------------------------------------------------------------------------------------------------- +set.seed(123456) # so that vct1 does not change +vct1 <- runif(6) # A short vector as input to keep output short +str(vct1) + + +## ----apply-01a---------------------------------------------------------------------------------------------------------- +z <- lapply(X = vct1, FUN = log) +str(z) + + +## ----apply-02----------------------------------------------------------------------------------------------------------- +z <- sapply(X = vct1, FUN = log) +str(z) + + +## ----apply-03, eval=eval_playground------------------------------------------------------------------------------------- +z <- sapply(X = vct1, FUN = log, simplify = FALSE) +str(z) + + +## ----apply-01b---------------------------------------------------------------------------------------------------------- +z <- sapply(X = vct1, FUN = log, base = 10) +str(z) + + +## ----apply-04----------------------------------------------------------------------------------------------------------- +z <- sapply(X = vct1, FUN = function(x) {log10(x + 1)}) +str(z) + + +## ----apply-05a---------------------------------------------------------------------------------------------------------- +sapply(X = cars, FUN = mean) + + +## ----apply-07----------------------------------------------------------------------------------------------------------- +mean_and_sd <- + function(x, na.rm = FALSE) { + c(mean = mean(x, na.rm = na.rm), sd = sd(x, na.rm = na.rm)) + } + + +## ----apply-07b---------------------------------------------------------------------------------------------------------- +values <- vapply(X = cars, + FUN = mean_and_sd, + FUN.VALUE = c(mean = 0, sd = 0), + na.rm = TRUE) +class(values) +values + + +## ----apply-06----------------------------------------------------------------------------------------------------------- +set.seed(123456) +ls1 <- lapply(X = c(v1 = 2, v2 = 5, v3 = 3, v4 = 1, v5 = 4), + FUN = rnorm, mean = 10, sd = 1) +str(ls1) + + +## ----apply-10----------------------------------------------------------------------------------------------------------- +mat1 <- matrix(rnorm(6, mean = 10, sd = 1), ncol = 2) +mat1 <- round(mat1, digits = 1) +dimnames(mat1) <- # add row and column names + list(paste("row", 1:nrow(mat1)), paste("col", 1:ncol(mat1))) +mat1 + + +## ----apply-08----------------------------------------------------------------------------------------------------------- +apply(mat1, MARGIN = 2, FUN = mean) + + +## ----apply-11----------------------------------------------------------------------------------------------------------- +z <- apply(X = mat1, MARGIN = 2, FUN = I) +dim(z) +z + + +## ----apply-12----------------------------------------------------------------------------------------------------------- +z <- apply(X = mat1, MARGIN = 1, FUN = I) +dim(z) +z + + +## ----apply-13----------------------------------------------------------------------------------------------------------- +z <- apply(X = mat1, MARGIN = 2, FUN = summary) +z + + +## ----apply-14----------------------------------------------------------------------------------------------------------- +z <- apply(X = mat1, MARGIN = 1, FUN = summary) +z + + +## ----apply-15----------------------------------------------------------------------------------------------------------- +set.seed(123456) # so that vct1 does not change +vct1 <- runif(10) +z <- sapply(X = vct1, FUN = `+`, e2 = 5) +str(z) + + +## ----loops-function-names----------------------------------------------------------------------------------------------- +vct1 <- rnorm(10) +results <- numeric() +fun.names <- c("mean", "max", "min") +for (f.name in fun.names) { + results[[f.name]] <- do.call(f.name, list(vct1)) +} +results + + +## ----loops-functions-1-------------------------------------------------------------------------------------------------- +results <- numeric() +funs <- list(mean, max, min) +for (f in funs) { + results <- c(results, f(x)) +} +results + + +## ----loops-functions-2-------------------------------------------------------------------------------------------------- +results <- numeric() +funs <- list(average = mean, maximum = max, minimum = min) +for (f in names(funs)) { + results[[f]] <- funs[[f]](x) +} +results + + +## ----loops-formulas-1--------------------------------------------------------------------------------------------------- +my.data <- data.frame(x = 1:10, y = 1:10 + rnorm(10, 1, 0.1)) +results <- list() +models <- list(linear = y ~ x, linear.orig = y ~ x - 1, quadratic = y ~ x + I(x^2)) +for (m in names(models)) { + results[[m]] <- lm(models[[m]], data = my.data) +} +str(results, max.level = 1) +do.call(anova, unname(results)) + + +## ----loops-formulas-2--------------------------------------------------------------------------------------------------- +results <- list() +models <- list(y ~ x, y ~ x - 1, y ~ x + I(x^2)) +for (i in seq(along.with = models)) { + results[[i]] <- lm(models[[i]], data = my.data) +} +str(results, max.level = 1) +do.call(anova, results) + + +## ----include=FALSE, cache=FALSE----------------------------------------------------------------------------------------- +opts_chunk$set(opts_fig_wide_square) + + +## ----bench-diff-01, echo=FALSE------------------------------------------------------------------------------------------ +library(scales) +library(ggplot2) +library(patchwork) + +load("benchmarks.pantera.Rda") + +fig.seconds <- + ggplot(summaries, + aes(x = size, y = median*1e-3, + color = loop, shape = loop)) + + geom_point() + + geom_line() + + scale_x_log10(name = "Vector length (n)", + breaks = trans_breaks("log10", function(x) 10^x), + labels = trans_format("log10", math_format(10^.x))) + + scale_y_log10(name = "Time (s)", + breaks = trans_breaks("log10", function(x) 10^x), + labels = trans_format("log10", math_format(10^.x))) + + scale_color_discrete(name = "Iteration\napproach") + + scale_shape(name = "Iteration\napproach", solid = TRUE) + + expand_limits(y = 1e-6) + + theme_bw(14) + +fig.rel <- + ggplot(rel.summaries, + aes(x = size, y = median, color = loop, shape = loop)) + + geom_point() + + geom_line() + + scale_x_log10(name = "Vector length (n)", + breaks = trans_breaks("log10", function(x) 10^x), + labels = trans_format("log10", math_format(10^.x))) + + scale_y_log10(name = "Time (relative to shortest)", + breaks = c(1, 2, 5, 10, 20, 50, 100, 200, 500, 1000)) + + scale_color_discrete(name = "Iteration\napproach") + + scale_shape(name = "Iteration\napproach", solid = TRUE) + + theme_bw(14) + +print(fig.seconds / fig.rel + plot_layout(guides = "collect")) + + +## ----include=FALSE, cache=FALSE----------------------------------------------------------------------------------------- +opts_chunk$set(opts_fig_narrow) + + +## ----loops-while-0, eval=FALSE------------------------------------------------------------------------------------------ +## b <- numeric() # do not pre-allocate memory +## i <- 1 +## while (i < length(a)) { +## b[i] <- a[i+1] - a[i] +## i <- i + 1 +## } + + +## ----loops-while-1, eval=FALSE------------------------------------------------------------------------------------------ +## b <- numeric(length(a)-1) # pre-allocate memory +## i <- 1 +## while (i < length(a)) { +## b[i] <- a[i+1] - a[i] +## i <- i + 1 +## } + + +## ----loops-for-2, eval=FALSE-------------------------------------------------------------------------------------------- +## b <- numeric() # do not pre-allocate memory +## for(i in seq(along.with = b)) { +## b[i] <- a[i+1] - a[i] +## } + + +## ----loops-for-2a, eval=FALSE------------------------------------------------------------------------------------------- +## b <- numeric(length(a)-1) # pre-allocate memory +## for(i in seq(along.with = b)) { +## b[i] <- a[i+1] - a[i] +## } + + +## ----loops-vectorised-, eval=FALSE-------------------------------------------------------------------------------------- +## # vectorised using extraction operators +## b <- a[2:length(a)] - a[1:length(a)-1] + + +## ----loops-r-function-2, eval=FALSE------------------------------------------------------------------------------------- +## # vectorised function diff() +## b <- diff(a) + + +## ----include=FALSE, cache=FALSE----------------------------------------------------------------------------------------- +opts_chunk$set(opts_fig_wide_square) + + +## ----bench-rowsums-01, echo=FALSE--------------------------------------------------------------------------------------- +#library(scales) +#library(ggplot2) +#library(patchwork) + +load("benchmarks-rowSums-pantera.Rda") + +fig.seconds <- + ggplot(summaries, + aes(x = size, y = median*1e-3, color = loop, shape = loop)) + + geom_point() + + geom_line() + + scale_x_log10(name = "Matrix size (n)", + breaks = trans_breaks("log10", function(x) 10^x), + labels = trans_format("log10", math_format(10^.x))) + + scale_y_log10(name = "Time (s)", + breaks = trans_breaks("log10", function(x) 10^x), + labels = trans_format("log10", math_format(10^.x))) + + scale_color_discrete(name = "Iteration\napproach") + + scale_shape(name = "Iteration\napproach", solid = TRUE) + + expand_limits(y = 1e-6) + + theme_bw(14) + +fig.rel <- + ggplot(rel.summaries, + aes(x = size, y = median, color = loop, shape = loop)) + + geom_point() + + geom_line() + + scale_x_log10(name = "Matrix size (n)", + breaks = trans_breaks("log10", function(x) 10^x), + labels = trans_format("log10", math_format(10^.x))) + + scale_y_log10(name = "Time (relative to shortest)", + breaks = c(1, 2, 5, 10, 20, 50, 100, 200, 500, 1000)) + + scale_color_discrete(name = "Iteration\napproach") + + scale_shape(name = "Iteration\napproach", solid = TRUE) + + theme_bw(14) + +print(fig.seconds / fig.rel + plot_layout(guides = "collect")) + + +## ----include=FALSE, cache=FALSE----------------------------------------------------------------------------------------- +opts_chunk$set(opts_fig_narrow) + + +## ----nested-22, eval=FALSE---------------------------------------------------------------------------------------------- +## row.sum <- numeric() +## for (i in 1:nrow(A)) { +## row.sum[i] <- 0 +## for (j in 1:ncol(A)) +## row.sum[i] <- row.sum[i] + A[i, j] +## } +## print(row.sum) + + +## ----nested-3, eval=FALSE----------------------------------------------------------------------------------------------- +## row.sum <- numeric(nrow(A)) # faster +## for (i in 1:nrow(A)) { +## row.sum[i] <- sum(A[i, ]) +## } + + +## ----nested-4, eval=FALSE----------------------------------------------------------------------------------------------- +## row.sum <- apply(A, MARGIN = 1, sum) # MARGIN=1 indicates rows + + +## ----nested-5, eval=FALSE----------------------------------------------------------------------------------------------- +## rowSums(A) + + +## ----assignx-01--------------------------------------------------------------------------------------------------------- +assign("a", 9.99) +a + + +## ----assignx-01a-------------------------------------------------------------------------------------------------------- +name.of.var <- "b" +assign(name.of.var, 9.99) +b + + +## ----assignx-02--------------------------------------------------------------------------------------------------------- +for (i in 1:5) { + assign(paste("square_of_", i, sep = ""), i^2) +} +ls(pattern = "square_of_*") + + +## ----assignx-03--------------------------------------------------------------------------------------------------------- +get("a") +get("b") + + +## ----assignx-04--------------------------------------------------------------------------------------------------------- +obj_names <- ls(pattern = "square_of_*") +obj_lst <- mget(obj_names) +str(obj_lst) + + +## ----on-exit-01--------------------------------------------------------------------------------------------------------- +file.create("temp.file") +on.exit(file.remove("temp.file")) +# code that makes use of the file goes here + + +## ----cleanup-02--------------------------------------------------------------------------------------------------------- +detach(package:patchwork) +detach(package:ggplot2) +detach(package:scales) + diff --git a/learn-r-2ed-purl/R.stats.R b/learn-r-2ed-purl/R.stats.R new file mode 100644 index 00000000..80267263 --- /dev/null +++ b/learn-r-2ed-purl/R.stats.R @@ -0,0 +1,695 @@ +## ----echo=FALSE, include=FALSE------------------------------------------------------------------------------------------ +opts_chunk$set(opts_fig_narrow_square) +opts_knit$set(concordance=TRUE) +opts_knit$set(unnamed.chunk.label = 'functions-chunk') +rm(plot) + + +## ----stat-fun-1--------------------------------------------------------------------------------------------------------- +x <- c(1:20, NA) +mean(x) +mean(x, na.rm = TRUE) + + +## ----stat-fun-2--------------------------------------------------------------------------------------------------------- +x <- c(1:20, 100) +mean(x) +mean(x, trim = 0.05) + + +## ----distrib-01--------------------------------------------------------------------------------------------------------- +dnorm(x = 1.5, mean = 1, sd = 0.5) + + +## ----distrib-01a-------------------------------------------------------------------------------------------------------- +dnorm(x = seq(from = -1, to = 1, length.out = 5), mean = 1, sd = 0.5) + + +## ----distrib-01b-------------------------------------------------------------------------------------------------------- +vct1 <- seq(from = -1, to = 3, length.out = 50) + +df1 <- data.frame(x = vct1, + y = dnorm(x = vct1, mean = 1, sd = 1)) +plot(y~x, data = df1, type = "l", xlab = "z", ylab = "f(z)") +points(x = 2, y = dnorm(x = 2, mean = 1, sd = 1)) + + +## ----distrib-02--------------------------------------------------------------------------------------------------------- +pnorm(q = 2, mean = 1, sd = 1) +pnorm(q = 2, mean = 1, sd = 1, lower.tail = FALSE) +pnorm(q = 2, mean = 1, sd = 4, lower.tail = FALSE) +pnorm(q = c(2, 4), mean = 1, sd = 1, lower.tail = FALSE) + + +## ----distrib-03--------------------------------------------------------------------------------------------------------- +qnorm(p = 0.01, mean = 0, sd = 1) +qnorm(p = 0.05, mean = 0, sd = 1) +qnorm(p = 0.05, mean = 0, sd = 1, lower.tail = FALSE) + + +## ----distrib-WB01------------------------------------------------------------------------------------------------------- +qnorm(p = 0.025, mean = 0, sd = 1) +qnorm(p = 0.025, mean = 0, sd = 1, lower.tail = FALSE) + + +## ----distrib-WB02------------------------------------------------------------------------------------------------------- +pnorm(q = 4, mean = 0, sd = 1) * 2 + + +## ----distrib-04,echo=2:3------------------------------------------------------------------------------------------------ +set.seed(12234) +rnorm(5) +rnorm(n = 10, mean = 10, sd = 2) + + +## ----density-01--------------------------------------------------------------------------------------------------------- +d <- density(faithful$waiting, bw = "sj") + + +## ----density-02--------------------------------------------------------------------------------------------------------- +str(d) + + +## ----density-03--------------------------------------------------------------------------------------------------------- +d$n + + +## ----desnity-04--------------------------------------------------------------------------------------------------------- +plot(d) + + +## ----sampling-01-------------------------------------------------------------------------------------------------------- +sample(x = LETTERS) +sample(x = LETTERS, size = 12) +sample(x = LETTERS, size = 12, replace = TRUE) + + +## ----sampling-02-------------------------------------------------------------------------------------------------------- +cars[sample(x = 1:nrow(cars), size = 4), ] + + +## ----sampling-03, eval=eval_playground---------------------------------------------------------------------------------- +cars[sample(x = nrow(cars), size = 4), ] + + +## ----cor-01------------------------------------------------------------------------------------------------------------- +cor(x = cars$speed, y = cars$dist) + + +## ----cor-02------------------------------------------------------------------------------------------------------------- +cor(cars) + + +## ----cor-02a------------------------------------------------------------------------------------------------------------ +mat1 <- matrix(rnorm(54), ncol = 3, + dimnames = list(rows = 1:18, cols = c("A", "B", "C"))) +cor(mat1) + + +## ----cor-03------------------------------------------------------------------------------------------------------------- +cor.test(x = cars$speed, y = cars$dist) + + +## ----cor-03a, eval=eval_playground-------------------------------------------------------------------------------------- +cor.test(formula = ~ speed + dist, data = cars) + + +## ----cor-PG01, eval=eval_playground------------------------------------------------------------------------------------- +MAT1 <- cor(cars) +class(MAT1) +attributes(MAT1) +str(MAT1) + + +## ----cor-04------------------------------------------------------------------------------------------------------------- +cor(x = cars$speed, y = cars$dist, method = "kendall") +cor(x = cars$speed, y = cars$dist, method = "spearman") + + +## ----ttest-01----------------------------------------------------------------------------------------------------------- +df1 <- data.frame(some.size = c(rnorm(10, mean = 2.5), rnorm(10, mean = 2.0)), + group = factor(rep(c("A", "B"), each = 10))) + + +## ----ttest-02----------------------------------------------------------------------------------------------------------- +t.test(some.size ~ 1, mu = 2, data = df1) + + +## ----ttest-02a---------------------------------------------------------------------------------------------------------- +sem = sqrt(var(df1$some.size) / nrow(df1)) +t.value = (mean(df1$some.size) - 2) / sem # Ho: mu = 2 +p.value <- pt(t.value, df = nrow(df1) - 1, lower.tail = FALSE) * 2 # two tails +signif(c(t = t.value, df = nrow(df1) - 1, P = p.value), 4) # 4 digits + + +## ----ttest-03----------------------------------------------------------------------------------------------------------- +t.test(some.size ~ group, data = df1) + + +## ----ttest-03a---------------------------------------------------------------------------------------------------------- +t.test(some.size ~ group, var.equal = TRUE, data = df1) + + +## ----models-1----------------------------------------------------------------------------------------------------------- +fm1 <- lm(dist ~ 1 + speed, data=cars) +class(fm1) + + +## ----models-1a---------------------------------------------------------------------------------------------------------- +plot(fm1, which = 2) + + +## ----models-1b---------------------------------------------------------------------------------------------------------- +summary(fm1) + + +## ----models-2----------------------------------------------------------------------------------------------------------- +fm2 <- lm(dist ~ speed - 1, data = cars) +summary(fm2) + + +## ----models-3, eval=eval_playground------------------------------------------------------------------------------------- +fm3 <- lm(dist ~ speed + I(speed^2), data = cars) +plot(fm3, which = 3) +summary(fm3) + + +## ----models-3a, eval=eval_playground------------------------------------------------------------------------------------ +fm3a <- lm(dist ~ poly(speed, 2), data = cars) +summary(fm3a) + + +## ----models-4, eval=eval_playground------------------------------------------------------------------------------------- +anova(fm2, fm1) + + +## ----models-5, eval=eval_playground------------------------------------------------------------------------------------- +anova(fm2, fm3, fm3a) +anova(fm2, fm3a, fm3) + + +## ----models-5a, eval=eval_playground------------------------------------------------------------------------------------ +BIC(fm2, fm1, fm3, fm3a) +AIC(fm2, fm1, fm3, fm3a) + + +## ----models-EB2ca------------------------------------------------------------------------------------------------------- +class(summary(fm1)) + + +## ----models-EB3--------------------------------------------------------------------------------------------------------- +str(summary(fm1), no.list = TRUE, give.attr = FALSE, vec.len = 2) + + +## ----models-EB4--------------------------------------------------------------------------------------------------------- +summary(fm1)$adj.r.squared + + +## ----models-EB3b-------------------------------------------------------------------------------------------------------- +coef(fm1) +str(fm1$coefficients) +print(summary(fm1)$coefficients) +str(summary(fm1)$coefficients) + + +## ----models-EB2--------------------------------------------------------------------------------------------------------- +anova(fm1) + + +## ----models-EB2a-------------------------------------------------------------------------------------------------------- +class(anova(fm1)) + + +## ----models-EB2b-------------------------------------------------------------------------------------------------------- +str(anova(fm1)) + + +## ----models-EB5--------------------------------------------------------------------------------------------------------- +est.slope.value <- summary(fm1)$coefficients["speed", "Estimate"] +est.slope.se <- summary(fm1)$coefficients["speed", "Std. Error"] +degrees.of.freedom <- summary(fm1)$df[2] + + +## ----models-EB6--------------------------------------------------------------------------------------------------------- +hyp.null <- 1 +t.value <- (est.slope.value - hyp.null) / est.slope.se +p.value <- 2 * pt(q = t.value, df = degrees.of.freedom, lower.tail = FALSE) +cat("slope =", signif(est.slope.value, 3), + "with s.e. =", signif(est.slope.se, 3), + "\nt.value =", signif(t.value, 3), + "and P-value =", signif(p.value, 3)) + + +## ----models-EB0--------------------------------------------------------------------------------------------------------- +class(fm1) +names(fm1) + + +## ----models-EB1--------------------------------------------------------------------------------------------------------- +str(fm1, no.list = TRUE, give.attr = FALSE, vec.len = 2) + + +## ----models-EB1b-------------------------------------------------------------------------------------------------------- +str(fm1$call) + + +## ----models-6z---------------------------------------------------------------------------------------------------------- +data(InsectSprays) +is.numeric(InsectSprays$spray) +is.factor(InsectSprays$spray) +levels(InsectSprays$spray) + + +## ----models-6----------------------------------------------------------------------------------------------------------- +fm4 <- lm(count ~ spray, data = InsectSprays) + + +## ----model-6a----------------------------------------------------------------------------------------------------------- +plot(fm4, which = 2) + + +## ----model-6b----------------------------------------------------------------------------------------------------------- +anova(fm4) + + +## ----contrasts-01------------------------------------------------------------------------------------------------------- +options("contrasts") + + +## ----models-6c---------------------------------------------------------------------------------------------------------- +fm4trea <- lm(count ~ spray, data = InsectSprays, + contrasts = list(spray = contr.treatment)) +fm4sum <- lm(count ~ spray, data = InsectSprays, + contrasts = list(spray = contr.sum)) + + +## ----contrasts-02------------------------------------------------------------------------------------------------------- +contr.treatment(length(levels(InsectSprays$spray))) + + +## ----models-6d---------------------------------------------------------------------------------------------------------- +summary(fm4trea) + + +## ----contrasts-03------------------------------------------------------------------------------------------------------- +contr.sum(length(levels(InsectSprays$spray))) + + +## ----models-6e---------------------------------------------------------------------------------------------------------- +summary(fm4sum) + + +## ----models-update-01--------------------------------------------------------------------------------------------------- +fm4 <- lm(count ~ spray, data = InsectSprays) +anova(fm4) +fm4a <- update(fm4, formula = log10(count + 1) ~ spray) +anova(fm4a) + + +## ----models-update-02--------------------------------------------------------------------------------------------------- +fm4b <- update(fm4, subset = !spray %in% c("A", "B")) +anova(fm4b) + + +## ----models-update-03, eval=eval_playground----------------------------------------------------------------------------- +fm4c <- update(fm4, subset = !grepl("[AB]", spray)) +anova(fm4c) +identical(fm4b, fm4c) + + +## ----models-step-01----------------------------------------------------------------------------------------------------- +fm3 <- lm(dist ~ speed + I(speed^2), data = cars) +fm3a <- step(fm3) + + +## ----models-step-01a---------------------------------------------------------------------------------------------------- +summary(fm3) +summary(fm3a) + + +## ----models-step-02----------------------------------------------------------------------------------------------------- +fm3b <- + step(fm3, + scope = dist ~ speed + I(speed^2) + I(speed^3) + I(speed^4)) +summary(fm3b) + + +## ----models-step-02a, eval=eval_playground------------------------------------------------------------------------------ +fm3c <- + step(fm3, + scope = list(lower = dist ~ speed, + upper = dist ~ speed + I(speed^2) + I(speed^3) + I(speed^4))) +summary(fm3c) + + +## ----model-10----------------------------------------------------------------------------------------------------------- +fm10 <- glm(count ~ spray, data = InsectSprays, family = quasipoisson) + + +## ----model-11----------------------------------------------------------------------------------------------------------- +plot(fm10, which = 2) + + +## ----model-10a---------------------------------------------------------------------------------------------------------- +anova(fm10, test = "F") + + +## ----model-12----------------------------------------------------------------------------------------------------------- +class(fm10) +summary(fm10) +head(residuals(fm10)) +head(fitted(fm10)) + + +## ----model-13----------------------------------------------------------------------------------------------------------- +class(fm10) +names(fm10) +fm10$converged +fm10$iter + + +## ----nls-01------------------------------------------------------------------------------------------------------------- +data(Puromycin) +names(Puromycin) + + +## ----nls-02------------------------------------------------------------------------------------------------------------- +fm21 <- nls(rate ~ SSmicmen(conc, Vm, K), data = Puromycin, + subset = state == "treated") + + +## ----nls-03------------------------------------------------------------------------------------------------------------- +class(fm21) +summary(fm21) +residuals(fm21) +fitted(fm21) + + +## ----nls-04------------------------------------------------------------------------------------------------------------- +class(fm21) +names(fm21) + + +## ----nls-04a------------------------------------------------------------------------------------------------------------ +fm21$convInfo + + +## ----spline-01---------------------------------------------------------------------------------------------------------- +fs1 <- smooth.spline(x = cars$speed, y = cars$dist) +print(fs1) +plot(fs1, type = "l") +points(x = cars$speed, y = cars$dist) + + +## ----spline-02---------------------------------------------------------------------------------------------------------- +floc <- loess(dist ~ speed, data = cars) +class(floc) +summary(floc) + + +## ----mformulas-intro-1-------------------------------------------------------------------------------------------------- +class(y ~ x) +mode(y ~ x) + + +## ----mformulas-intro-2, eval=FALSE-------------------------------------------------------------------------------------- +## my.formula <- dist ~ 1 + speed +## fm1 <- lm(my.formula, data=cars) + + +## ----mformulas-intro-5-------------------------------------------------------------------------------------------------- +class(~ x + y) +mode(~ x + y) +is.empty.model(~ x + y) + + +## ----mformulas-intro-3-------------------------------------------------------------------------------------------------- +class(formula()) +mode(formula()) +length(formula()) +# is.empty.model(formula()) + + +## ----mformulas-intro-4-------------------------------------------------------------------------------------------------- +class(y ~ 0) +mode(y ~ 0) +is.empty.model(y ~ 0) +is.empty.model(y ~ 1) +is.empty.model(y ~ x) + + +## ----mformulas-intro-6-------------------------------------------------------------------------------------------------- +length(formula()) +length(y ~ 0) +length(y ~ 1) +length(y ~ x) +length(c(y ~ 1, y ~ x)) +length(list(y ~ 1)) +length(list(y ~ 1, y ~ x)) + + +## ----mformulas-intro-6b------------------------------------------------------------------------------------------------- +sapply(list(y ~ 0, y ~ 1, y ~ x), length) +sapply(list(y ~ 0, y ~ 1, y ~ x), is.empty.model) + + +## ----mformulas-intro-7,eval=FALSE--------------------------------------------------------------------------------------- +## y ~ I(x1 + x2) +## y ~ log(x1 + x2) + + +## ----mformulas-intro-8,eval=FALSE--------------------------------------------------------------------------------------- +## y ~ x1 + x2 + x3 + x1:x2 + x1:x3 + x2:x3 + x1:x2:x3 + + +## ----mformulas-00,eval=FALSE-------------------------------------------------------------------------------------------- +## y ~ x1 * x2 * x3 + + +## ----mformulas-01, eval=FALSE------------------------------------------------------------------------------------------- +## y ~ x1 + (x2 * x3) +## y ~ x1 + x2 + x3 + x2:x3 + + +## ----mformulas-02,eval=FALSE-------------------------------------------------------------------------------------------- +## y ~ x1 * (x2 + x3) +## y ~ x1 + x2 + x3 + x1:x2 + x1:x3 + + +## ----mformulas-03,eval=FALSE-------------------------------------------------------------------------------------------- +## y ~ (x1 + x2 + x3)^2 +## y ~ x1 + x2 + x3 + x1:x2 + x1:x3 + x2:x3 + + +## ----mformulas-04,eval=FALSE-------------------------------------------------------------------------------------------- +## y ~ x1 + x2 + x1 %in% x2 + + +## ----mformulas-01a, eval=FALSE------------------------------------------------------------------------------------------ +## terms(y ~ x1 + (x2 * x3)) +## terms(y ~ x1 * (x2 + x3)) +## terms(y ~ (x1 + x2 + x3)^2) +## terms(y ~ x1 + x2 + x1 %in% x2) + + +## ----mformulas-PG01, eval=eval_playground------------------------------------------------------------------------------- +y ~ (x1 + x2 + x3)^2 +y ~ (x1 * x2 * x3)^2 + + +## ----mformulas-PG02, eval=FALSE----------------------------------------------------------------------------------------- +## data(npk) +## anova(lm(yield ~ N * P * K, data = npk)) +## anova(lm(yield ~ (N + P + K)^2, data = npk)) +## anova(lm(yield ~ N + P + K + P %in% N + K %in% N, data = npk)) +## anova(lm(yield ~ N + P + K + N %in% P + K %in% P, data = npk)) + + +## ----mformulas-W01,eval=FALSE------------------------------------------------------------------------------------------- +## y ~ A + B + A:B + A:C + B:C +## y ~ A + B + C + A:B + A:C + A:B:C + + +## ----mformulas-W02,eval=FALSE------------------------------------------------------------------------------------------- +## y ~ A + B + C + A:B + A:C + B:C +## y ~ (A + B + C)^2 +## y ~ A + B + C + B:C +## y ~ A + B * C + + +## ----mformulas-EB03----------------------------------------------------------------------------------------------------- +as.formula("y ~ x") + + +## ----mformulas-EB04----------------------------------------------------------------------------------------------------- +paste("y", "x", sep = "~") |> as.formula() + + +## ----mformulas-EB05----------------------------------------------------------------------------------------------------- +as.character(y ~ x) +format(y ~ x) + + +## ----mformulas-EB05a---------------------------------------------------------------------------------------------------- +format(y ~ x) |> gsub("x", "x + z", x = _) |> as.formula() + + +## ----mformulas-EB06----------------------------------------------------------------------------------------------------- +my.formula <- y ~ x1 + x2 +update(my.formula, . ~ . + x3) +update(my.formula, . ~ . - x1) +update(my.formula, . ~ x3) +update(my.formula, z ~ .) +update(my.formula, . + z ~ .) + + +## ----ts-00-------------------------------------------------------------------------------------------------------------- +my.ts <- ts(1:10, start = 2019, deltat = 1/12) + + +## ----ts-00aa------------------------------------------------------------------------------------------------------------ +print(my.ts) + + +## ----ts-00a------------------------------------------------------------------------------------------------------------- +mode(my.ts) +class(my.ts) +is.ts(my.ts) +str(my.ts) +attributes(my.ts) + + +## ----ts-04-------------------------------------------------------------------------------------------------------------- +nottem.celcius <- (nottem - 32) * 5 / 9 + + +## ----ts-03-------------------------------------------------------------------------------------------------------------- +is.ts(nottem.celcius) + + +## ----echo=FALSE, cache=FALSE-------------------------------------------------------------------------------------------- +opts_chunk$set(opts_fig_wide) + + +## ----ts-03plot---------------------------------------------------------------------------------------------------------- +plot(nottem.celcius) + + +## ----ts-03a, eval=eval_playground--------------------------------------------------------------------------------------- +str(nottem.celcius) +attributes(nottem.celcius) + + +## ----echo=FALSE, cache=FALSE-------------------------------------------------------------------------------------------- +opts_chunk$set(opts_fig_wide_square) + + +## ----ts-05, fig.height=5------------------------------------------------------------------------------------------------ +nottem.stl <- stl(nottem.celcius, s.window = 7) +plot(nottem.stl) + + +## ----ts-06-------------------------------------------------------------------------------------------------------------- +class(nottem.stl) +str(nottem.stl, no.list = TRUE, give.attr = FALSE, vec.len = 2) + + +## ----ts-07-------------------------------------------------------------------------------------------------------------- +summary(nottem.stl) + + +## ----echo=FALSE, cache=FALSE-------------------------------------------------------------------------------------------- +opts_chunk$set(opts_fig_narrow) + + +## ----ts-08-------------------------------------------------------------------------------------------------------------- +acf(nottem) + + +## ----manova-02---------------------------------------------------------------------------------------------------------- +mmf2 <- manova(cbind(Petal.Length, Petal.Width) ~ Species, data = iris) +anova(mmf2) +summary(mmf2) + + +## ----pca-01------------------------------------------------------------------------------------------------------------- +pc <- prcomp(iris[c("Sepal.Length", "Sepal.Width", + "Petal.Length", "Petal.Width")], + center = TRUE, + scale = TRUE) + + +## ----pca-02------------------------------------------------------------------------------------------------------------- +class(pc) +pc + + +## ----pca-03------------------------------------------------------------------------------------------------------------- +summary(pc) + + +## ----pca-04------------------------------------------------------------------------------------------------------------- +plot(pc) + + +## ----include=FALSE, cache=FALSE----------------------------------------------------------------------------------------- +opts_chunk$set(opts_fig_wide_square) + + +## ----pca-05------------------------------------------------------------------------------------------------------------- +biplot(pc) + + +## ----include=FALSE, cache=FALSE----------------------------------------------------------------------------------------- +opts_chunk$set(opts_fig_narrow) + + +## ----pca-EB-01, eval=eval_playground------------------------------------------------------------------------------------ +class(pc) +str(pc, max.level = 1) + + +## ----mds-01------------------------------------------------------------------------------------------------------------- +loc <- cmdscale(eurodist) + + +## ----mds-02------------------------------------------------------------------------------------------------------------- +class(loc) +dim(loc) +head(loc, 8) + + +## ----include=FALSE, cache=FALSE----------------------------------------------------------------------------------------- +opts_chunk$set(opts_fig_wide_square) + + +## ----mds-03------------------------------------------------------------------------------------------------------------- +x <- loc[, 1] +y <- -loc[, 2] # change sign so North is at the top +plot(x, y, type = "n", asp = 1, + main = "cmdscale(eurodist)") +text(x, y, rownames(loc), cex = 0.6) + + +## ----cluster-01--------------------------------------------------------------------------------------------------------- +hc <- hclust(eurodist) +print(hc) + + +## ----cluster-02--------------------------------------------------------------------------------------------------------- +plot(hc) + + +## ----cluster-03--------------------------------------------------------------------------------------------------------- +cutree(hc, k = 5) + + +## ----cluster-04--------------------------------------------------------------------------------------------------------- +str(hc) + + +## ----eval=eval_diag, include=eval_diag, echo=eval_diag, cache=FALSE----------------------------------------------------- +## knitter_diag() +## R_diag() +## other_diag() + diff --git a/learn-r-2ed-purl/call-purl.R b/learn-r-2ed-purl/call-purl.R new file mode 100644 index 00000000..90076f68 --- /dev/null +++ b/learn-r-2ed-purl/call-purl.R @@ -0,0 +1,28 @@ +library(knitr) + +eval_diag <- FALSE +incl_all <- TRUE +eval_plots_all <- TRUE +eval_playground <- TRUE +eval_online_data <- TRUE +eval_yoctopuce <- TRUE + +purl(input = "R.as.calculator.Rnw", output = "learn-r-2ed-purl/R.as.calculator.R") + +purl(input = "R.data.containers.Rnw", output = "learn-r-2ed-purl/R.data.containers.R") + +purl(input = "R.data.io.Rnw", output = "learn-r-2ed-purl/R.data.io.R") + +purl(input = "R.data.Rnw", output = "learn-r-2ed-purl/R.data.R") + +purl(input = "R.functions.Rnw", output = "learn-r-2ed-purl/R.functions.R") + +purl(input = "R.plotting.Rnw", output = "learn-r-2ed-purl/R.plotting.R") + +purl(input = "R.scripts.Rnw", output = "learn-r-2ed-purl/R.scripts.R") + +purl(input = "R.stats.Rnw", output = "learn-r-2ed-purl/R.stats.R") + +# purl(input = "R.intro.Rnw", output = "learn-r-2ed-purl/R.intro.R") + +purl(input = "R.learning.Rnw", output = "learn-r-2ed-purl/R.learning.R") diff --git a/learnrbook-purl-output.zip b/learnrbook-purl-output-1ed.zip similarity index 100% rename from learnrbook-purl-output.zip rename to learnrbook-purl-output-1ed.zip diff --git a/learnrbook-purl-output/R.as.calculator.R b/learnrbook-purl-output-1ed/R.as.calculator.R similarity index 100% rename from learnrbook-purl-output/R.as.calculator.R rename to learnrbook-purl-output-1ed/R.as.calculator.R diff --git a/learnrbook-purl-output/R.data.R b/learnrbook-purl-output-1ed/R.data.R similarity index 100% rename from learnrbook-purl-output/R.data.R rename to learnrbook-purl-output-1ed/R.data.R diff --git a/learnrbook-purl-output/R.data.io.R b/learnrbook-purl-output-1ed/R.data.io.R similarity index 100% rename from learnrbook-purl-output/R.data.io.R rename to learnrbook-purl-output-1ed/R.data.io.R diff --git a/learnrbook-purl-output/R.functions.R b/learnrbook-purl-output-1ed/R.functions.R similarity index 100% rename from learnrbook-purl-output/R.functions.R rename to learnrbook-purl-output-1ed/R.functions.R diff --git a/learnrbook-purl-output/R.intro.R b/learnrbook-purl-output-1ed/R.intro.R similarity index 100% rename from learnrbook-purl-output/R.intro.R rename to learnrbook-purl-output-1ed/R.intro.R diff --git a/learnrbook-purl-output/R.plotting.R b/learnrbook-purl-output-1ed/R.plotting.R similarity index 100% rename from learnrbook-purl-output/R.plotting.R rename to learnrbook-purl-output-1ed/R.plotting.R diff --git a/learnrbook-purl-output/R.scripts.R b/learnrbook-purl-output-1ed/R.scripts.R similarity index 100% rename from learnrbook-purl-output/R.scripts.R rename to learnrbook-purl-output-1ed/R.scripts.R