Debian Codegraph @

inst/examples/knitr-graphics.lyx

raw
1 #LyX 2.1 created this file. For more info see http://www.lyx.org/
2 \lyxformat 474
3 \begin_document
4 \begin_header
5 \textclass tufte-handout
6 \begin_preamble
7 \renewcommand{\textfraction}{0.05}
8 \renewcommand{\topfraction}{0.8}
9 \renewcommand{\bottomfraction}{0.8}
10 \renewcommand{\floatpagefraction}{0.75}
11
12 \usepackage[buttonsize=1em]{animate}
13 \end_preamble
14 \options justified
15 \use_default_options true
16 \begin_modules
17 knitr
18 \end_modules
19 \maintain_unincluded_children false
20 \language english
21 \language_package none
22 \inputencoding default
23 \fontencoding global
24 \font_roman default
25 \font_sans default
26 \font_typewriter default
27 \font_math auto
28 \font_default_family default
29 \use_non_tex_fonts false
30 \font_sc false
31 \font_osf false
32 \font_sf_scale 100
33 \font_tt_scale 100
34 \graphics default
35 \default_output_format default
36 \output_sync 0
37 \bibtex_command default
38 \index_command default
39 \paperfontsize default
40 \spacing single
41 \use_hyperref true
42 \pdf_bookmarks true
43 \pdf_bookmarksnumbered true
44 \pdf_bookmarksopen true
45 \pdf_bookmarksopenlevel 2
46 \pdf_breaklinks true
47 \pdf_pdfborder false
48 \pdf_colorlinks false
49 \pdf_backref false
50 \pdf_pdfusetitle true
51 \pdf_quoted_options "pdfstartview=FitH"
52 \papersize default
53 \use_geometry true
54 \use_package amsmath 1
55 \use_package amssymb 1
56 \use_package cancel 1
57 \use_package esint 1
58 \use_package mathdots 1
59 \use_package mathtools 1
60 \use_package mhchem 1
61 \use_package stackrel 1
62 \use_package stmaryrd 1
63 \use_package undertilde 1
64 \cite_engine basic
65 \cite_engine_type default
66 \biblio_style plain
67 \use_bibtopic false
68 \use_indices false
69 \paperorientation portrait
70 \suppress_date false
71 \justification true
72 \use_refstyle 1
73 \index Index
74 \shortcut idx
75 \color #008000
76 \end_index
77 \secnumdepth 2
78 \tocdepth 2
79 \paragraph_separation indent
80 \paragraph_indentation default
81 \quotes_language english
82 \papercolumns 1
83 \papersides 1
84 \paperpagestyle default
85 \tracking_changes false
86 \output_changes false
87 \html_math_output 0
88 \html_css_as_file 0
89 \html_be_strict false
90 \end_header
91
92 \begin_body
93
94 \begin_layout Standard
95 \begin_inset ERT
96 status open
97
98 \begin_layout Plain Layout
99
100 <<setup, include=FALSE, cache=FALSE>>=
101 \end_layout
102
103 \begin_layout Plain Layout
104
105 library(knitr)
106 \end_layout
107
108 \begin_layout Plain Layout
109
110 options(formatR.arrow=TRUE,width=50)
111 \end_layout
112
113 \begin_layout Plain Layout
114
115 opts_chunk$set(fig.path='figure/graphics-', cache.path='cache/graphics-',
116 fig.align='center', dev='tikz', fig.width=5, fig.height=5, fig.show='hold',
117 cache=TRUE, par=TRUE)
118 \end_layout
119
120 \begin_layout Plain Layout
121
122 knit_hooks$set(par=function(before, options, envir){
123 \end_layout
124
125 \begin_layout Plain Layout
126
127 if (before && options$fig.show!='none') par(mar=c(4,4,.1,.1),cex.lab=.95,cex.axis=.9,mg
128 p=c(2,.7,0),tcl=-.3)
129 \end_layout
130
131 \begin_layout Plain Layout
132
133 }, crop=hook_pdfcrop)
134 \end_layout
135
136 \begin_layout Plain Layout
137
138 @
139 \end_layout
140
141 \end_inset
142
143
144 \end_layout
145
146 \begin_layout Title
147 knitr Graphics Manual
148 \end_layout
149
150 \begin_layout Author
151 Yihui Xie
152 \end_layout
153
154 \begin_layout Abstract
155 This manual shows features of graphics in the
156 \series bold
157 knitr
158 \series default
159 package (version
160 \begin_inset ERT
161 status collapsed
162
163 \begin_layout Plain Layout
164
165
166 \backslash
167 Sexpr{packageVersion('knitr')}
168 \end_layout
169
170 \end_inset
171
172 ) in detail, including the graphical devices, plot recording, plot rearrangement
173 , control of plot sizes, the tikz device, figure captions, animations and
174 other types of plots such as
175 \series bold
176 rgl
177 \series default
178 or GGobi plots.
179 \end_layout
180
181 \begin_layout Standard
182 Before reading this specific manual
183 \begin_inset Foot
184 status open
185
186 \begin_layout Plain Layout
187 \begin_inset Flex URL
188 status collapsed
189
190 \begin_layout Plain Layout
191
192 http://bit.ly/knitr-graphics-src
193 \end_layout
194
195 \end_inset
196
197 (Rnw source)
198 \end_layout
199
200 \end_inset
201
202 , you must have finished the main manual
203 \begin_inset Foot
204 status open
205
206 \begin_layout Plain Layout
207 \begin_inset Flex URL
208 status collapsed
209
210 \begin_layout Plain Layout
211
212 http://bit.ly/knitr-main
213 \end_layout
214
215 \end_inset
216
217
218 \end_layout
219
220 \end_inset
221
222 .
223 \end_layout
224
225 \begin_layout Section
226 Graphical Devices
227 \end_layout
228
229 \begin_layout Standard
230 The
231 \series bold
232 knitr
233 \series default
234 package comes with more than 20 built-in graphical devices, and you can
235 specify them through the
236 \family typewriter
237 dev
238 \family default
239 option.
240 This document uses the global option
241 \family typewriter
242 dev='tikz'
243 \family default
244 , i.e., the plots are recorded by the tikz device by default, but we can change
245 the device locally.
246 Since tikz will be used extensively throughout this manual and you will
247 see plenty of tikz graphics later, now we first show a few other devices.
248 \end_layout
249
250 \begin_layout Standard
251 \begin_inset ERT
252 status open
253
254 \begin_layout Plain Layout
255
256 <<test-plot, eval=FALSE>>=
257 \end_layout
258
259 \begin_layout Plain Layout
260
261 with(trees, symbols(Height, Volume, circles = Girth/16, inches = FALSE,
262 bg = 'deeppink', fg = 'gray30'))
263 \end_layout
264
265 \begin_layout Plain Layout
266
267 @
268 \end_layout
269
270 \end_inset
271
272
273 \end_layout
274
275 \begin_layout Standard
276 \begin_inset Float marginfigure
277 wide false
278 sideways false
279 status open
280
281 \begin_layout Plain Layout
282 \begin_inset ERT
283 status open
284
285 \begin_layout Plain Layout
286
287 <<pdf-dev, ref.label='test-plot', dev='pdf', out.width='
288 \backslash
289
290 \backslash
291 linewidth', echo=FALSE>>=
292 \end_layout
293
294 \begin_layout Plain Layout
295
296 @
297 \end_layout
298
299 \end_inset
300
301
302 \end_layout
303
304 \begin_layout Plain Layout
305 \begin_inset Caption Standard
306
307 \begin_layout Plain Layout
308 The default PDF device.
309 \begin_inset CommandInset label
310 LatexCommand label
311 name "mar:pdf-dev"
312
313 \end_inset
314
315
316 \end_layout
317
318 \end_inset
319
320
321 \end_layout
322
323 \end_inset
324
325
326 \end_layout
327
328 \begin_layout Standard
329 \begin_inset Float marginfigure
330 wide false
331 sideways false
332 status open
333
334 \begin_layout Plain Layout
335 \begin_inset ERT
336 status open
337
338 \begin_layout Plain Layout
339
340 <<png-dev, ref.label='test-plot', dev='png', out.width='
341 \backslash
342
343 \backslash
344 linewidth', echo=FALSE>>=
345 \end_layout
346
347 \begin_layout Plain Layout
348
349 @
350 \end_layout
351
352 \end_inset
353
354
355 \end_layout
356
357 \begin_layout Plain Layout
358 \begin_inset Caption Standard
359
360 \begin_layout Plain Layout
361 The PNG device.
362 \begin_inset CommandInset label
363 LatexCommand label
364 name "mar:png-dev"
365
366 \end_inset
367
368
369 \end_layout
370
371 \end_inset
372
373
374 \end_layout
375
376 \end_inset
377
378
379 \end_layout
380
381 \begin_layout Standard
382 Figure
383 \begin_inset CommandInset ref
384 LatexCommand ref
385 reference "mar:pdf-dev"
386
387 \end_inset
388
389 and
390 \begin_inset CommandInset ref
391 LatexCommand ref
392 reference "mar:png-dev"
393
394 \end_inset
395
396 show two standard devices in the
397 \series bold
398 grDevices
399 \series default
400 package.
401 We can also use devices in the
402 \series bold
403 Cairo
404 \series default
405 or
406 \series bold
407 cairoDevice
408 \series default
409 package, e.g., the chunk below uses the
410 \emph on
411 CairoPNG()
412 \emph default
413 device in the
414 \series bold
415 Cairo
416 \series default
417 package.
418 \end_layout
419
420 \begin_layout Standard
421 \begin_inset ERT
422 status open
423
424 \begin_layout Plain Layout
425
426 <<cairo-png-dev, ref.label='test-plot', dev='CairoPNG', out.width='.5
427 \backslash
428
429 \backslash
430 linewidth', echo=FALSE>>=
431 \end_layout
432
433 \begin_layout Plain Layout
434
435 @
436 \end_layout
437
438 \end_inset
439
440
441 \end_layout
442
443 \begin_layout Section
444 Plot Recording
445 \end_layout
446
447 \begin_layout Standard
448 As mentioned in the main manual,
449 \series bold
450 knitr
451 \series default
452 uses the
453 \series bold
454 evaluate
455 \series default
456 package to record plots.
457 There are two sources of plots: first, whenever
458 \emph on
459 plot.new()
460 \emph default
461 or
462 \emph on
463 grid.newpage()
464 \emph default
465 is called,
466 \series bold
467 evaluate
468 \series default
469 will try to save a snapshot of the current plot
470 \begin_inset Foot
471 status open
472
473 \begin_layout Plain Layout
474 For technical details, see
475 \family typewriter
476 ?setHook
477 \family default
478 and
479 \family typewriter
480 ?recordPlot
481 \end_layout
482
483 \end_inset
484
485 ; second, after each complete expression is evaluated, a snapshot is also
486 saved.
487 To speed up recording, the null graphical device
488 \family typewriter
489 pdf(file = NULL)
490 \family default
491 is used.
492 Figure
493 \begin_inset CommandInset ref
494 LatexCommand ref
495 reference "fig:two-high"
496
497 \end_inset
498
499 shows two expressions producing two high-level plots.
500 \end_layout
501
502 \begin_layout Standard
503 \begin_inset Float figure
504 wide false
505 sideways false
506 status open
507
508 \begin_layout Plain Layout
509 \begin_inset ERT
510 status open
511
512 \begin_layout Plain Layout
513
514 <<two-high, fig.width=3, fig.height=2.5, out.width='.49
515 \backslash
516
517 \backslash
518 linewidth'>>=
519 \end_layout
520
521 \begin_layout Plain Layout
522
523 plot(cars)
524 \end_layout
525
526 \begin_layout Plain Layout
527
528 boxplot(cars$dist,xlab='dist')
529 \end_layout
530
531 \begin_layout Plain Layout
532
533 @
534 \end_layout
535
536 \end_inset
537
538
539 \end_layout
540
541 \begin_layout Plain Layout
542 \begin_inset Caption Standard
543
544 \begin_layout Plain Layout
545 Two high-level plots are captured.
546 The key to arrange two plots side by side is to specify the
547 \family typewriter
548 out.width
549 \family default
550 option so that each plot takes less than half of the line width.
551 We do not have to use the
552 \family typewriter
553 par(mfrow)
554 \family default
555 trick, and it may not work in some cases (e.g.
556 to put base graphics and
557 \series bold
558 ggplot2
559 \series default
560 side by side; recall Figure 1 in the main manual).
561 \begin_inset CommandInset label
562 LatexCommand label
563 name "fig:two-high"
564
565 \end_inset
566
567
568 \end_layout
569
570 \end_inset
571
572
573 \end_layout
574
575 \end_inset
576
577
578 \end_layout
579
580 \begin_layout Standard
581 Figure
582 \begin_inset CommandInset ref
583 LatexCommand ref
584 reference "fig:low-plot-loop"
585
586 \end_inset
587
588 shows another example of two R expressions, but the second expression only
589 involves with low-level plotting changes.
590 By default, low-level plot changes are discarded, but you can retain them
591 with the option
592 \family typewriter
593 fig.keep='all'
594 \family default
595 .
596 \end_layout
597
598 \begin_layout Standard
599 \begin_inset Float figure
600 wide false
601 sideways false
602 status open
603
604 \begin_layout Plain Layout
605 \begin_inset ERT
606 status open
607
608 \begin_layout Plain Layout
609
610 <<low-plot-loop, fig.width=3, fig.height=2.5, out.width='.49
611 \backslash
612
613 \backslash
614 linewidth', fig.keep='all'>>=
615 \end_layout
616
617 \begin_layout Plain Layout
618
619 plot(0,0,type='n',ann=FALSE)
620 \end_layout
621
622 \begin_layout Plain Layout
623
624 for(i in seq(0, 2*pi,length=20)) {points(cos(i),sin(i))}
625 \end_layout
626
627 \begin_layout Plain Layout
628
629 @
630 \end_layout
631
632 \end_inset
633
634
635 \end_layout
636
637 \begin_layout Plain Layout
638 \begin_inset Caption Standard
639
640 \begin_layout Plain Layout
641 Two complete R expressions will produce at most two plots, as long as there
642 are not multiple high-level plotting calls in each expression; option
643 \family typewriter
644 fig.keep='all'
645 \family default
646 here.
647 \begin_inset CommandInset label
648 LatexCommand label
649 name "fig:low-plot-loop"
650
651 \end_inset
652
653
654 \end_layout
655
656 \end_inset
657
658
659 \end_layout
660
661 \end_inset
662
663
664 \end_layout
665
666 \begin_layout Standard
667 Together with
668 \family typewriter
669 fig.show='asis'
670 \family default
671 , we can show the process of plotting step by step like Figure
672 \begin_inset CommandInset ref
673 LatexCommand ref
674 reference "fig:high-with-low"
675
676 \end_inset
677
678 .
679 \end_layout
680
681 \begin_layout Standard
682 \begin_inset Float figure
683 wide false
684 sideways false
685 status open
686
687 \begin_layout Plain Layout
688 \begin_inset ERT
689 status open
690
691 \begin_layout Plain Layout
692
693 <<high-with-low, fig.width=3, fig.height=2.5, out.width='.5
694 \backslash
695
696 \backslash
697 linewidth', fig.keep='all', fig.show='asis'>>=
698 \end_layout
699
700 \begin_layout Plain Layout
701
702 plot(cars)
703 \end_layout
704
705 \begin_layout Plain Layout
706
707 abline(lm(dist~speed, data=cars)) # a regression line
708 \end_layout
709
710 \begin_layout Plain Layout
711
712 @
713 \end_layout
714
715 \end_inset
716
717
718 \end_layout
719
720 \begin_layout Plain Layout
721 \begin_inset Caption Standard
722
723 \begin_layout Plain Layout
724 Low-level plot changes in base graphics can be recorded separately, and
725 plots can be put in the places where they were produced.
726 \begin_inset CommandInset label
727 LatexCommand label
728 name "fig:high-with-low"
729
730 \end_inset
731
732
733 \end_layout
734
735 \end_inset
736
737
738 \end_layout
739
740 \end_inset
741
742
743 \end_layout
744
745 \begin_layout Standard
746 A further note on plot recording:
747 \series bold
748 knitr
749 \series default
750 examines all recorded plots (as R objects) and compares them sequentially;
751 if the previous plot is a
752 \begin_inset Quotes eld
753 \end_inset
754
755 subset
756 \begin_inset Quotes erd
757 \end_inset
758
759 of the next plot (= previous plot + low-level changes), the previous plot
760 will be removed when
761 \family typewriter
762 fig.keep='high'
763 \family default
764 .
765 If two successive plots are identical, the second one will be removed by
766 default, so it might be a little bit surprising that the following chunk
767 will only produce one plot by default
768 \begin_inset Foot
769 status open
770
771 \begin_layout Plain Layout
772 adapted from
773 \begin_inset Flex URL
774 status collapsed
775
776 \begin_layout Plain Layout
777
778 https://github.com/yihui/knitr/issues/41
779 \end_layout
780
781 \end_inset
782
783
784 \end_layout
785
786 \end_inset
787
788 :
789 \end_layout
790
791 \begin_layout Standard
792 \begin_inset ERT
793 status open
794
795 \begin_layout Plain Layout
796
797 <<identical-plots,eval=FALSE>>=
798 \end_layout
799
800 \begin_layout Plain Layout
801
802 m = matrix(1:100, ncol = 10)
803 \end_layout
804
805 \begin_layout Plain Layout
806
807 image(m)
808 \end_layout
809
810 \begin_layout Plain Layout
811
812 image(m*2) # exactly the same as previous plot
813 \end_layout
814
815 \begin_layout Plain Layout
816
817 @
818 \end_layout
819
820 \end_inset
821
822
823 \end_layout
824
825 \begin_layout Section
826 Plot Rearrangement
827 \end_layout
828
829 \begin_layout Standard
830 We can rearrange the plots in chunks in several ways.
831 They can be inserted right after the line(s) of R code which produced them,
832 or accumulated till the end of the chunk.
833 There is an example in the main manual demonstrating
834 \family typewriter
835 fig.show='asis'
836 \family default
837 for two high-level plots, and Figure
838 \begin_inset CommandInset ref
839 LatexCommand ref
840 reference "fig:high-with-low"
841
842 \end_inset
843
844 in this manual also demonstrates this option for a high-level plot followed
845 by a low-level change.
846 \end_layout
847
848 \begin_layout Standard
849 Here is an example demonstrating the option
850 \family typewriter
851 fig.keep='last'
852 \family default
853 (only the last plot is kept):
854 \end_layout
855
856 \begin_layout Standard
857 \begin_inset Float figure
858 wide false
859 sideways false
860 status open
861
862 \begin_layout Plain Layout
863 \begin_inset ERT
864 status open
865
866 \begin_layout Plain Layout
867
868 <<fig-last, fig.width=5.2, fig.height=3, out.width='.7
869 \backslash
870
871 \backslash
872 linewidth', fig.keep='last', message=FALSE>>=
873 \end_layout
874
875 \begin_layout Plain Layout
876
877 library(ggplot2)
878 \end_layout
879
880 \begin_layout Plain Layout
881
882 pie <- ggplot(diamonds, aes(x = factor(1), fill = cut)) + xlab('cut') +
883 geom_bar(width = 1)
884 \end_layout
885
886 \begin_layout Plain Layout
887
888 pie + coord_polar(theta = "y") # a pie chart
889 \end_layout
890
891 \begin_layout Plain Layout
892
893 pie + coord_polar() # the bullseye chart
894 \end_layout
895
896 \begin_layout Plain Layout
897
898 @
899 \end_layout
900
901 \end_inset
902
903
904 \end_layout
905
906 \begin_layout Plain Layout
907 \begin_inset Caption Standard
908
909 \begin_layout Plain Layout
910 Two plots were produced in this chunk, but only the last one is kept.
911 This can be useful when we experiment with many plots, but only want the
912 last result.
913 (Adapted from the
914 \series bold
915 ggplot2
916 \series default
917 website)
918 \end_layout
919
920 \end_inset
921
922
923 \end_layout
924
925 \end_inset
926
927
928 \end_layout
929
930 \begin_layout Standard
931 When multiple plots are produced by a code chunk, we may want to show them
932 as an animation with the option
933 \family typewriter
934 fig.show='animate'
935 \family default
936 .
937 Figure
938 \begin_inset CommandInset ref
939 LatexCommand ref
940 reference "fig:clock-animation"
941
942 \end_inset
943
944 shows a simple clock animation; you may compare the code to Figure
945 \begin_inset CommandInset ref
946 LatexCommand ref
947 reference "fig:high-with-low"
948
949 \end_inset
950
951 to understand that high-level plots are always recorded, regardless of
952 where they appeared.
953 \end_layout
954
955 \begin_layout Standard
956 \begin_inset Float figure
957 wide false
958 sideways false
959 status open
960
961 \begin_layout Plain Layout
962 \begin_inset ERT
963 status open
964
965 \begin_layout Plain Layout
966
967 <<clock-animation, fig.width=3, fig.height=3, out.width='.4
968 \backslash
969
970 \backslash
971 linewidth', fig.show='animate', tidy=FALSE, crop=TRUE>>=
972 \end_layout
973
974 \begin_layout Plain Layout
975
976 par(mar = rep(3, 4))
977 \end_layout
978
979 \begin_layout Plain Layout
980
981 for (i in seq(pi/2, -4/3 * pi, length = 12)) {
982 \end_layout
983
984 \begin_layout Plain Layout
985
986 plot(0, 0, pch = 20, ann = FALSE, axes = FALSE)
987 \end_layout
988
989 \begin_layout Plain Layout
990
991 arrows(0, 0, cos(i), sin(i))
992 \end_layout
993
994 \begin_layout Plain Layout
995
996 axis(1, 0, "VI"); axis(2, 0, "IX")
997 \end_layout
998
999 \begin_layout Plain Layout
1000
1001 axis(3, 0, "XII"); axis(4, 0, "III"); box()
1002 \end_layout
1003
1004 \begin_layout Plain Layout
1005
1006 }
1007 \end_layout
1008
1009 \begin_layout Plain Layout
1010
1011 @
1012 \end_layout
1013
1014 \end_inset
1015
1016
1017 \end_layout
1018
1019 \begin_layout Plain Layout
1020 \begin_inset Caption Standard
1021
1022 \begin_layout Plain Layout
1023 A clock animation.
1024 You have to view it in Adobe Reader: click to play/pause; there are also
1025 buttons to speed up or slow down the animation.
1026 \begin_inset CommandInset label
1027 LatexCommand label
1028 name "fig:clock-animation"
1029
1030 \end_inset
1031
1032
1033 \end_layout
1034
1035 \end_inset
1036
1037
1038 \end_layout
1039
1040 \end_inset
1041
1042
1043 \end_layout
1044
1045 \begin_layout Standard
1046 We can also set the alignment of plots easily with the
1047 \family typewriter
1048 fig.align
1049 \family default
1050 option; this document uses
1051 \family typewriter
1052 fig.align='center'
1053 \family default
1054 as a global option, and we can also set plots to be left/right-aligned.
1055 Figure
1056 \begin_inset CommandInset ref
1057 LatexCommand ref
1058 reference "fig:maruko-plot"
1059
1060 \end_inset
1061
1062 is an example of a left-aligned plot.
1063 \end_layout
1064
1065 \begin_layout Standard
1066 \begin_inset Float figure
1067 wide false
1068 sideways false
1069 status open
1070
1071 \begin_layout Plain Layout
1072 \begin_inset ERT
1073 status open
1074
1075 \begin_layout Plain Layout
1076
1077 <<maruko-plot, fig.width=7, fig.height=4, out.width='.45
1078 \backslash
1079
1080 \backslash
1081 linewidth', fig.align='left', crop=TRUE>>=
1082 \end_layout
1083
1084 \begin_layout Plain Layout
1085
1086 stars(cbind(1:16,10*(16:1)),draw.segments=TRUE)
1087 \end_layout
1088
1089 \begin_layout Plain Layout
1090
1091 @
1092 \end_layout
1093
1094 \end_inset
1095
1096
1097 \end_layout
1098
1099 \begin_layout Plain Layout
1100 \begin_inset Caption Standard
1101
1102 \begin_layout Plain Layout
1103 A left-aligned plot adapted from
1104 \family typewriter
1105 ?stars
1106 \family default
1107 (I call this the
1108 \begin_inset Quotes eld
1109 \end_inset
1110
1111 Maruko
1112 \begin_inset Quotes erd
1113 \end_inset
1114
1115 plot, and it is one of my favorites; see
1116 \begin_inset Flex URL
1117 status collapsed
1118
1119 \begin_layout Plain Layout
1120
1121 http://en.wikipedia.org/wiki/Chibi_Maruko-chan
1122 \end_layout
1123
1124 \end_inset
1125
1126 ).
1127 \begin_inset CommandInset label
1128 LatexCommand label
1129 name "fig:maruko-plot"
1130
1131 \end_inset
1132
1133
1134 \end_layout
1135
1136 \end_inset
1137
1138
1139 \end_layout
1140
1141 \end_inset
1142
1143
1144 \end_layout
1145
1146 \begin_layout Section
1147 Plot Size
1148 \end_layout
1149
1150 \begin_layout Standard
1151 We have seen several examples in which two or more plots can be put side
1152 by side, and this is because the plots were resized in the output document;
1153 with the chunk option
1154 \family typewriter
1155 out.width
1156 \family default
1157 less than half of the line width, LaTeX will arrange two plots in one line;
1158 if it is less than
1159 \begin_inset Formula $1/3$
1160 \end_inset
1161
1162 of the line width, three plots can be put in one line.
1163 Of course we can also set it to be an absolute width like
1164 \family typewriter
1165 3in
1166 \family default
1167 (3 inches).
1168 This option is used extensively in this document to control the size of
1169 plots in the output document.
1170 \end_layout
1171
1172 \begin_layout Section
1173 The tikz Device
1174 \end_layout
1175
1176 \begin_layout Standard
1177 The main advantage of using tikz graphics is the consistency of styles between
1178 texts in plots and those in the main document.
1179 Since we can use native LaTeX commands in plots, the styles of texts in
1180 plots can be very sophisticated (see Figure
1181 \begin_inset CommandInset ref
1182 LatexCommand ref
1183 reference "fig:math-formula-tt"
1184
1185 \end_inset
1186
1187 for an example).
1188 \end_layout
1189
1190 \begin_layout Standard
1191 \begin_inset Float figure
1192 wide false
1193 sideways false
1194 status open
1195
1196 \begin_layout Plain Layout
1197 \begin_inset ERT
1198 status open
1199
1200 \begin_layout Plain Layout
1201
1202 <<math-formula-tt, fig.width=6, fig.height=2, out.width='
1203 \backslash
1204
1205 \backslash
1206 linewidth'>>=
1207 \end_layout
1208
1209 \begin_layout Plain Layout
1210
1211 plot(0:1,0:1,type='n', ylab='origin of statistics', xlab='statistical presentati
1212 on rocks with
1213 \backslash
1214
1215 \backslash
1216 LaTeX{}')
1217 \end_layout
1218
1219 \begin_layout Plain Layout
1220
1221 text(.5,c(.8,.5,.2), c('
1222 \backslash
1223
1224 \backslash
1225 texttt{lm(y
1226 \backslash
1227
1228 \backslash
1229 textasciitilde{} x)}', '$
1230 \backslash
1231
1232 \backslash
1233 hat{
1234 \backslash
1235
1236 \backslash
1237 beta}=(X^{
1238 \backslash
1239
1240 \backslash
1241 prime}X)^{-1}X^{
1242 \backslash
1243
1244 \backslash
1245 prime}y$', '$(
1246 \backslash
1247
1248 \backslash
1249 Omega,
1250 \backslash
1251
1252 \backslash
1253 mathcal{F},
1254 \backslash
1255
1256 \backslash
1257 mu)$'))
1258 \end_layout
1259
1260 \begin_layout Plain Layout
1261
1262 @
1263 \end_layout
1264
1265 \end_inset
1266
1267
1268 \end_layout
1269
1270 \begin_layout Plain Layout
1271 \begin_inset Caption Standard
1272
1273 \begin_layout Plain Layout
1274 A plot created by
1275 \series bold
1276 tikzDevice
1277 \series default
1278 with math expressions and typewriter fonts.
1279 Note the font style consistency -- we write the same expressions in LaTeX
1280 here:
1281 \begin_inset Formula $\hat{\beta}=(X^{\prime}X)^{-1}X^{\prime}y$
1282 \end_inset
1283
1284 and
1285 \begin_inset Formula $(\Omega,\mathcal{F},\mu)$
1286 \end_inset
1287
1288 ; also
1289 \family typewriter
1290 lm(y ~ x)
1291 \family default
1292 .
1293 \begin_inset CommandInset label
1294 LatexCommand label
1295 name "fig:math-formula-tt"
1296
1297 \end_inset
1298
1299
1300 \end_layout
1301
1302 \end_inset
1303
1304
1305 \end_layout
1306
1307 \end_inset
1308
1309
1310 \end_layout
1311
1312 \begin_layout Standard
1313 When using XeLaTeX instead of PDFLaTeX to compile the document, we need
1314 to tell the
1315 \series bold
1316 tikzDevice
1317 \series default
1318 package by setting the
1319 \family typewriter
1320 tikzDefaultEngine
1321 \family default
1322 option before all plot chunks (preferably in the first chunk):
1323 \end_layout
1324
1325 \begin_layout Standard
1326 \begin_inset ERT
1327 status open
1328
1329 \begin_layout Plain Layout
1330
1331 <<xetex-tikz, eval=FALSE>>=
1332 \end_layout
1333
1334 \begin_layout Plain Layout
1335
1336 options(tikzDefaultEngine='xetex')
1337 \end_layout
1338
1339 \begin_layout Plain Layout
1340
1341 @
1342 \end_layout
1343
1344 \end_inset
1345
1346
1347 \end_layout
1348
1349 \begin_layout Standard
1350 This is useful and often necessary to compile tikz plots which contain (UTF8)
1351 multi-byte characters.
1352 \end_layout
1353
1354 \begin_layout Section
1355 Figure Caption
1356 \end_layout
1357
1358 \begin_layout Standard
1359 If the chunk option
1360 \family typewriter
1361 fig.cap
1362 \family default
1363 is not
1364 \family typewriter
1365 NULL
1366 \family default
1367 or
1368 \family typewriter
1369 NA
1370 \family default
1371 , the plots will be put in a
1372 \family typewriter
1373 figure
1374 \family default
1375 environment when the output format is LaTeX, and this option is used to
1376 write a caption in this environment using
1377 \family typewriter
1378
1379 \backslash
1380 caption{}
1381 \family default
1382 .
1383 The other two related options are
1384 \family typewriter
1385 fig.scap
1386 \family default
1387 and
1388 \family typewriter
1389 fig.lp
1390 \family default
1391 which set the short caption and a prefix string for the figure label.
1392 The default short caption is extracted from the caption by truncating it
1393 at the first period or colon or semi-colon.
1394 The label is a combination of
1395 \family typewriter
1396 fig.lp
1397 \family default
1398 and the chunk label.
1399 Because
1400 \family typewriter
1401 figure
1402 \family default
1403 is a float environment, it can float away from the chunk output to other
1404 places such as the top or bottom of a page when the TeX document is compiled.
1405 \end_layout
1406
1407 \begin_layout Section
1408 Other Features
1409 \end_layout
1410
1411 \begin_layout Standard
1412 The
1413 \series bold
1414 knitr
1415 \series default
1416 package can be extended with hook functions, and here I give a few examples
1417 illustrating the flexibility.
1418 \end_layout
1419
1420 \begin_layout Subsection
1421 Cropping PDF Graphics
1422 \end_layout
1423
1424 \begin_layout Standard
1425 Some R users may have been suffering from the extra margins in R plots,
1426 especially in base graphics (
1427 \series bold
1428 ggplot2
1429 \series default
1430 is much better in this aspect).
1431 The default graphical option
1432 \family typewriter
1433 mar
1434 \family default
1435 is about
1436 \family typewriter
1437 c(5, 4, 4, 2)
1438 \family default
1439 (see
1440 \family typewriter
1441 ?par
1442 \family default
1443 ), which is often too big.
1444 Instead of endlessly setting
1445 \family typewriter
1446 par(mar)
1447 \family default
1448 , you may consider the program
1449 \family typewriter
1450 pdfcrop
1451 \family default
1452 , which can crop the white margin automatically
1453 \begin_inset Foot
1454 status open
1455
1456 \begin_layout Plain Layout
1457 \begin_inset Flex URL
1458 status collapsed
1459
1460 \begin_layout Plain Layout
1461
1462 http://www.ctan.org/pkg/pdfcrop
1463 \end_layout
1464
1465 \end_inset
1466
1467
1468 \end_layout
1469
1470 \end_inset
1471
1472 .
1473 In
1474 \series bold
1475 knitr
1476 \series default
1477 , we can set up the hook
1478 \emph on
1479 hook_pdfcrop()
1480 \emph default
1481 to work with a chunk option, say,
1482 \family typewriter
1483 crop
1484 \family default
1485 .
1486 \end_layout
1487
1488 \begin_layout Standard
1489 \begin_inset ERT
1490 status open
1491
1492 \begin_layout Plain Layout
1493
1494 <<crop-hook, cache=FALSE>>=
1495 \end_layout
1496
1497 \begin_layout Plain Layout
1498
1499 knit_hooks$set(crop=hook_pdfcrop)
1500 \end_layout
1501
1502 \begin_layout Plain Layout
1503
1504 @
1505 \end_layout
1506
1507 \end_inset
1508
1509
1510 \end_layout
1511
1512 \begin_layout Standard
1513 Now we compare two plots below.
1514 The first one is not cropped (Figure
1515 \begin_inset CommandInset ref
1516 LatexCommand ref
1517 reference "fig:pdf-nocrop"
1518
1519 \end_inset
1520
1521 ); then the same plot is produced but with a chunk option
1522 \family typewriter
1523 crop=TRUE
1524 \family default
1525 which will call the cropping hook (Figure
1526 \begin_inset CommandInset ref
1527 LatexCommand ref
1528 reference "fig:pdf-crop"
1529
1530 \end_inset
1531
1532 ).
1533 \end_layout
1534
1535 \begin_layout Standard
1536 \begin_inset Float figure
1537 wide false
1538 sideways false
1539 status open
1540
1541 \begin_layout Plain Layout
1542 \begin_inset ERT
1543 status open
1544
1545 \begin_layout Plain Layout
1546
1547
1548 \backslash
1549 begin{kframe}
1550 \end_layout
1551
1552 \begin_layout Plain Layout
1553
1554 <<pdf-nocrop, fig.width=3.2, fig.height=3.2, out.width='.5
1555 \backslash
1556
1557 \backslash
1558 linewidth'>>=
1559 \end_layout
1560
1561 \begin_layout Plain Layout
1562
1563 par(mar=c(5,4,4,2),bg='white') # large margin
1564 \end_layout
1565
1566 \begin_layout Plain Layout
1567
1568 plot(lat~long,data=quakes,pch=20,col=rgb(0,0,0,.2))
1569 \end_layout
1570
1571 \begin_layout Plain Layout
1572
1573 @
1574 \end_layout
1575
1576 \begin_layout Plain Layout
1577
1578
1579 \backslash
1580 end{kframe}
1581 \end_layout
1582
1583 \end_inset
1584
1585
1586 \end_layout
1587
1588 \begin_layout Plain Layout
1589 \begin_inset Caption Standard
1590
1591 \begin_layout Plain Layout
1592 The original plot produced in R, with a large margin.
1593 \begin_inset CommandInset label
1594 LatexCommand label
1595 name "fig:pdf-nocrop"
1596
1597 \end_inset
1598
1599
1600 \end_layout
1601
1602 \end_inset
1603
1604
1605 \end_layout
1606
1607 \end_inset
1608
1609
1610 \end_layout
1611
1612 \begin_layout Standard
1613 \begin_inset Float figure
1614 wide false
1615 sideways false
1616 status open
1617
1618 \begin_layout Plain Layout
1619 \begin_inset ERT
1620 status open
1621
1622 \begin_layout Plain Layout
1623
1624
1625 \backslash
1626 begin{kframe}
1627 \end_layout
1628
1629 \begin_layout Plain Layout
1630
1631 <<pdf-crop, ref.label='pdf-nocrop', echo=FALSE, fig.width=3.2, fig.height=3.2,
1632 out.width='.5
1633 \backslash
1634
1635 \backslash
1636 linewidth', crop=TRUE>>=
1637 \end_layout
1638
1639 \begin_layout Plain Layout
1640
1641 @
1642 \end_layout
1643
1644 \begin_layout Plain Layout
1645
1646
1647 \backslash
1648 end{kframe}
1649 \end_layout
1650
1651 \end_inset
1652
1653
1654 \end_layout
1655
1656 \begin_layout Plain Layout
1657 \begin_inset Caption Standard
1658
1659 \begin_layout Plain Layout
1660 The cropped plot; it fits better in the document.
1661 \begin_inset CommandInset label
1662 LatexCommand label
1663 name "fig:pdf-crop"
1664
1665 \end_inset
1666
1667
1668 \end_layout
1669
1670 \end_inset
1671
1672
1673 \end_layout
1674
1675 \end_inset
1676
1677
1678 \end_layout
1679
1680 \begin_layout Standard
1681 As we can see, the white margins are gone.
1682 If we use
1683 \emph on
1684 par()
1685 \emph default
1686 , it might be hard and tedious to figure out a reasonable amount of margin
1687 in order that neither is any label cropped nor do we get a too large margin.
1688 My experience is that
1689 \family typewriter
1690 pdfcrop
1691 \family default
1692 works well with base graphics, but barely works with
1693 \series bold
1694 grid
1695 \series default
1696 graphics (therefore
1697 \series bold
1698 lattice
1699 \series default
1700 and
1701 \series bold
1702 ggplot2
1703 \series default
1704 are ruled out).
1705 \end_layout
1706
1707 \begin_layout Subsection
1708 Manually Saved Plots
1709 \end_layout
1710
1711 \begin_layout Standard
1712 We have explained how R plots are recorded before.
1713 In some cases, it is not possible to capture plots by
1714 \emph on
1715 recordPlot()
1716 \emph default
1717 (such as
1718 \series bold
1719 rgl
1720 \series default
1721 plots), but we can save them using other functions.
1722 To insert these plots into the output, we need to set up a hook first like
1723 this (see
1724 \family typewriter
1725 ?hook_plot_custom
1726 \family default
1727 for details):
1728 \end_layout
1729
1730 \begin_layout Standard
1731 \begin_inset ERT
1732 status open
1733
1734 \begin_layout Plain Layout
1735
1736 <<custom-plot-hook, cache=FALSE>>=
1737 \end_layout
1738
1739 \begin_layout Plain Layout
1740
1741 knit_hooks$set(custom.plot = hook_plot_custom)
1742 \end_layout
1743
1744 \begin_layout Plain Layout
1745
1746 @
1747 \end_layout
1748
1749 \end_inset
1750
1751
1752 \end_layout
1753
1754 \begin_layout Standard
1755 Then we set the chunk option
1756 \family typewriter
1757 custom.plot=TRUE
1758 \family default
1759 , and manually write plot files in the chunk.
1760 Here we show an example of capturing GGobi plots using the function
1761 \emph on
1762 ggobi_display_save_picture()
1763 \emph default
1764 in the
1765 \series bold
1766 rggobi
1767 \series default
1768 package:
1769 \end_layout
1770
1771 \begin_layout Standard
1772 \begin_inset ERT
1773 status open
1774
1775 \begin_layout Plain Layout
1776
1777 <<ggobi-plot, custom.plot=TRUE, fig.ext='png', out.width='2.5in', results='hide',
1778 warning=FALSE>>=
1779 \end_layout
1780
1781 \begin_layout Plain Layout
1782
1783 library(rggobi)
1784 \end_layout
1785
1786 \begin_layout Plain Layout
1787
1788 ggobi(ggobi_find_file('data', 'flea.csv'))
1789 \end_layout
1790
1791 \begin_layout Plain Layout
1792
1793 Sys.sleep(1) # wait for snapshot
1794 \end_layout
1795
1796 \begin_layout Plain Layout
1797
1798 ggobi_display_save_picture(path=fig_path('.png'))
1799 \end_layout
1800
1801 \begin_layout Plain Layout
1802
1803 @
1804 \end_layout
1805
1806 \end_inset
1807
1808
1809 \end_layout
1810
1811 \begin_layout Standard
1812 One thing to note here is we have to make sure the plot filename is from
1813
1814 \emph on
1815 fig_path()
1816 \emph default
1817 , which is a convenience function to return the figure path for the current
1818 chunk.
1819 \end_layout
1820
1821 \begin_layout Standard
1822 We can do whatever normal R plots can do with this hook, and we give another
1823 example below to show how to work with animations.
1824 \end_layout
1825
1826 \begin_layout Standard
1827 \begin_inset ERT
1828 status open
1829
1830 \begin_layout Plain Layout
1831
1832 <<rgl-animation, custom.plot=TRUE, fig.ext='png', out.width='2.5in', fig.show='animat
1833 e', fig.num=20, interval=.4, aniopts='controls,autoplay', results='hide',
1834 warning=FALSE>>=
1835 \end_layout
1836
1837 \begin_layout Plain Layout
1838
1839 library(animation) # adapted from demo('rgl_animation')
1840 \end_layout
1841
1842 \begin_layout Plain Layout
1843
1844 data(pollen)
1845 \end_layout
1846
1847 \begin_layout Plain Layout
1848
1849 uM = matrix(c(-0.37, -0.51, -0.77, 0, -0.73, 0.67, -0.1, 0, 0.57, 0.53, -0.63, 0,
1850 0, 0, 0, 1), 4, 4)
1851 \end_layout
1852
1853 \begin_layout Plain Layout
1854
1855 library(rgl)
1856 \end_layout
1857
1858 \begin_layout Plain Layout
1859
1860 open3d(userMatrix = uM, windowRect = c(0, 0, 400, 400))
1861 \end_layout
1862
1863 \begin_layout Plain Layout
1864
1865 plot3d(pollen[, 1:3])
1866 \end_layout
1867
1868 \begin_layout Plain Layout
1869
1870 zm = seq(1, 0.05, length = 20)
1871 \end_layout
1872
1873 \begin_layout Plain Layout
1874
1875 par3d(zoom = 1) # change the zoom factor gradually later
1876 \end_layout
1877
1878 \begin_layout Plain Layout
1879
1880 for (i in 1:length(zm)) {
1881 \end_layout
1882
1883 \begin_layout Plain Layout
1884
1885 par3d(zoom = zm[i]); Sys.sleep(.05)
1886 \end_layout
1887
1888 \begin_layout Plain Layout
1889
1890 rgl.snapshot(fig_path('png', number = i))
1891 \end_layout
1892
1893 \begin_layout Plain Layout
1894
1895 }
1896 \end_layout
1897
1898 \begin_layout Plain Layout
1899
1900 @
1901 \end_layout
1902
1903 \end_inset
1904
1905
1906 \end_layout
1907
1908 \begin_layout Subsection
1909 rgl Plots
1910 \end_layout
1911
1912 \begin_layout Standard
1913 With the hook
1914 \emph on
1915 hook_rgl()
1916 \emph default
1917 , we can easily save snapshots from the
1918 \series bold
1919 rgl
1920 \series default
1921 package.
1922 We have shown an example in the main manual, and here we add some details.
1923 The rgl hook is a good example of taking care of details by carefully using
1924 the
1925 \family typewriter
1926 options
1927 \family default
1928 argument in the hook; for example, we cannot directly set the width and
1929 height of rgl plots in
1930 \emph on
1931 rgl.snapshot()
1932 \emph default
1933 or
1934 \emph on
1935 rgl.postscript()
1936 \emph default
1937 , so we make use of the options
1938 \family typewriter
1939 fig.width
1940 \family default
1941 ,
1942 \family typewriter
1943 fig.height
1944 \family default
1945 and
1946 \family typewriter
1947 dpi
1948 \family default
1949 to calculate the expected size of the window, then resize the current window
1950 by
1951 \emph on
1952 par3d()
1953 \emph default
1954 , and finally save the plot.
1955 \end_layout
1956
1957 \begin_layout Standard
1958 This hook is actually built upon
1959 \emph on
1960 hook_plot_custom()
1961 \emph default
1962 -- first it saves the
1963 \series bold
1964 rgl
1965 \series default
1966 snapshot, then it calls
1967 \emph on
1968 hook_plot_custom()
1969 \emph default
1970 to write the output code.
1971 \end_layout
1972
1973 \begin_layout Section
1974 \start_of_appendix
1975 How to Compile This Manual
1976 \end_layout
1977
1978 \begin_layout Standard
1979 This manual has a long chain of dependencies, so it may not be easy to compile.
1980 These packages are required (all of them are free software):
1981 \end_layout
1982
1983 \begin_layout Description
1984 R Cairo, ggplot2, tikzDevice, rgl, rggobi, animation (all available on CRAN
1985 except tikzDevice which is on R-Forge for the time being)
1986 \end_layout
1987
1988 \begin_layout Description
1989 LaTeX animate, hyperref and the tufte-handout class
1990 \end_layout
1991
1992 \begin_layout Description
1993 Other GGobi, pdfcrop
1994 \end_layout
1995
1996 \end_body
1997 \end_document