最直观的方法
本书插图除了外部引用的图片外,均使用 R 语言绘制。读者可以很方便地获取作图代码,来重现书中插图,方法如下:
install.packages("MSG") # 初次使用时安装 MSG 包
library("MSG") # 加载包
msg('4.1') # 重现插图 4.1 和获取作图代码
这是最直观的方式,即运行 msg('图的编号')。注意:图的编号要放在一对引号里,它是个字符串。如果忘记了输入引号,那么得到的源代码和图形可能不是你想要得到的,如 msg(4.10) 将得到图 4.1 而非图 4.10。
可能遇到的问题
附加包支持
有些图形需要额外的附加包支持,初次使用时需要读者自行安装。
如果觉得逐个安装太麻烦,读者可以运行:
msg('0')
就可一次性安装本书示例代码中所需的全部附加包。
中文支持
如果读者用的是 Windows 操作系统,而区域语言的设置不是中文(这种情况在本书的读者群里应该很少见),则可能在重现图形时遇到如下警告:
Warning message:
In readLines(file, warn = FALSE) :
invalid input found on input connection '......'
这种情况下,只需在作图前需运行下面一行来支持中文字符:
Sys.setlocale("LC_CTYPE", "Chinese")
基础作图与 ggplot2
很多图形我们都给出了 R 基础作图和 ggplot2 作图两种解决方案,在这种情况下,msg('图的编号')得到的一般是基础作图代码和图形。如果想得到 ggplot2 图形,只需在图形编号前面增加一个 2 就可以了,例如:
msg('24.1')
就可得到图 4.1 的 ggplot2 版本,包括图示和源代码。
注意,并非所有的图都有两个版本;支持编号前有 2的图见附表。
有些图形还可以在编号前面增加个 3。至于用处是什么,你猜。
图的英文代号
除了 msg('图的编号') 外,也可以运行 msg('图的英文代号')。例如,图 4.1 的英文代号是 “vadeaths-barplot”,那么可运行 msg('vadeaths-barplot'),等价于 msg('4.1')。
图的编号和英文代号如附表所示。
| 编号 | 英文代号 |
|---|---|
| 0 | install-packages |
| 2.2 | ggplot2-minard |
| 3.2 | pork-price |
| 23.2 | pork-price2 |
| 3.4 | canabalt-boxplot |
| 3.7 | tvearn |
| 3.8 | music-andrews |
| 33.14 | music-andrews-tikz |
| 3.9 | music-3d-pcp |
| 3.10 | assists-sna |
| 3.11 | basketball |
| 3.12 | basketball-fourfold |
| 3.13 | gov-cn-pct |
| 23.19 | gov-cn-pct2 |
| 3.14 | PlantCounts |
| 23.14 | PlantCounts2 |
| 3.15 | song-words-hclust |
| 3.16 | song-words-corr |
| 3.17 | song-high-freq |
| 3.18 | multireg-scatter |
| 23.18 | multireg-scatter2 |
| 33.18 | multireg-scatter2-tikz |
| 3.19 | interaction-bubble |
| 33.19 | interaction-bubble-tikz |
| 3.20 | OLS-LMS |
| 23.20 | OLS-LMS2 |
| 33.20 | OLS-LMS2-tikz |
| 3.21 | outlier-resample |
| 23.21 | outlier-resample2 |
| 33.21 | outlier-resample2-tikz |
| 4.1 | vadeaths-barplot |
| 24.1 | vadeaths-barplot2 |
| 4.2 | dotchart |
| 4.3 | dotchart2 |
| 4.4 | hist-geyser |
| 24.4 | hist-geyser2 |
| 4.5 | hist-density |
| 24.5 | hist-density2 |
| 4.6 | stem-islands |
| 4.7 | stem-poisson |
| 4.8 | insects-boxplot |
| 24.8 | insects-boxplot2 |
| 4.9 | rnorm-boxplot |
| 24.9 | rnorm-boxplot2 |
| 34.9 | rnorm-boxplot2-tikz |
| 4.10 | vioplot |
| 24.10 | vioplot2 |
| 4.11 | rug |
| 24.11 | rug2 |
| 4.12 | stripchart |
| 4.13 | stripchart2 |
| 4.14 | pie-chart |
| 24.14 | pie-chart2 |
| 4.15 | floating-pie |
| 4.16 | qqnorm |
| 24.16 | qqnorm2 |
| 4.17 | nightingale-ggplot2 |
| 5.1 | scatter-alpha |
| 25.1 | scatter-alpha2 |
| 5.2 | curve |
| 25.2 | curve2 |
| 35.2 | curve-tikz |
| 5.3 | sunflowerplot |
| 5.4 | smoothScatter |
| 25.4 | smoothScatter2 |
| 5.5 | windrose |
| 5.6 | survival |
| 25.6 | survival2 |
| 5.7 | cdplot |
| 25.7 | cdplot2 |
| 5.8 | spineplot |
| 5.9 | bagplot |
| 25.9 | bagplot2 |
| 6.1 | pairs |
| 6.2 | pairs2 |
| 6.3 | coplot |
| 6.4 | ChinaPop |
| 6.5 | symbols_all |
| 6.6 | stars |
| 6.7 | faces2 |
| 6.8 | murcia-ternary |
| 6.9 | mosaicplot |
| 6.10 | plot-design |
| 6.11 | interaction-plot |
| 6.12 | rpart |
| 6.13 | ggparcoord |
| 6.14 | andrews-curve |
| 36.14 | andrews-curve-tikz |
| 7.1 | contour-grid |
| 27.1 | contour-grid2 |
| 37.1 | contour-grid2-tikz |
| 7.2 | contour-pop |
| 27.2 | contour-pop2 |
| 7.3 | density-2d-filled |
| 7.4 | filled-contour |
| 7.5 | image24 |
| 27.5 | image242 |
| 37.5 | image242-tikz |
| 7.6 | image |
| 27.6 | image2 |
| 7.7 | persp |
| 7.8 | persp-pop |
| 37.8 | persp-pop-tikz |
| 7.9 | matplot |
| 7.10 | matplot2 |
| 37.10 | matplot2-tikz |
| 7.11 | heatmap |
| 7.12 | assocplot |
| 7.13 | fourfoldplot |
| 7.14 | fig-contour-persp |
| 7.15 | assocplot2 |
| 8.1 | discrete-var |
| 8.2 | pay-rating-points |
| 38.2 | pay-rating-points-tikz |
| 8.3 | pay-rating-log |
| 8.4 | music-mds |
| 8.5 | tufte1997 |
| 8.6 | cut-plot |
| 38.6 | cut-plot-tikz |
| 8.8 | sunspots-asp |
| 8.9 | optical-illusion |
| 8.10 | kmeans-cluster |
| 38.10 | kmeans-cluster-tikz |
| 8.11 | error-bars |
| 9.1 | lines-demo |
| 9.2 | graphicsEvent |
| 9.3 | par2 |
| 9.4 | plot-region |
| 9.5 | ColorBrewer-all |
| 9.6 | grad-color |
| 9.7 | point-type |
| 9.8 | point-iris |
| 9.9 | point-random |
| 9.10 | plot-type |
| 9.11 | xspline-demo |
| 9.12 | polygon-kaleidoscope |
| 9.13 | polygon-demo |
| 9.14 | text |
| 9.15 | axis-demo |
| 9.16 | plotmath |
| 39.16 | plotmath-tikz |
| 9.17 | layout |
| 9.18 | layout-margin |
| 9.19 | split-screen |
| 9.20 | par1 |
| 9.21 | waterfall-chart |
| 9.22 | gradArrows |
| 9.23 | sign-stat |
| 10.1 | qplot |
| 10.2 | mtcars-smooth |
| 10.3 | diamonds-hex |
| 10.4 | ggplot2-violin |
| 10.5 | iris-ggplot2 |
| 10.6 | quake6-bubbles |
| 310.6 | quake6-bubbles-tikz |
| 10.7 | diamonds-cut |
| 10.8 | diamonds-polar |
| 10.9 | diamonds-carat |
| 10.10 | geom-jitter-demo |
| 10.11 | ggplot2-theme |
| 10.12 | ggplot2-layers |
| 11.1 | grid-subplot |
| 311.1 | grid-subplot-tikz |
| 11.2 | grid-rotate |
| 11.3 | grid-princomp |
| 11.4 | lattice-iris-hist |
| 11.5 | lattice-vadeaths |
| 11.6 | lattice-data-par |
| 311.6 | lattice-data-par-tikz |
| 11.7 | lattice-panel-function |
| 11.8 | rgl-ch4 |
| 11.9 | rgl-beta |
| 11.11 | animation-pollen |