xiangyunhuang / msg Goto Github PK
View Code? Open in Web Editor NEW:book: 现代统计图形(人民邮电出版社出版) Modern Statistical Graphics
Home Page: https://bookdown.org/xiangyun/msg
:book: 现代统计图形(人民邮电出版社出版) Modern Statistical Graphics
Home Page: https://bookdown.org/xiangyun/msg
一些明显的,我替换掉了,在搬迁完主要内容后,对参考文献中的链接、正文中的链接进行检查
@pzhaonet 关于图片短标题,多个图并排等都有完整设置说明见 https://yihui.name/knitr/options/#plots
第6章 数据案例之背景地图
demo("eqMaps", package = "MSG")
其实是如下一段代码
library(RgoogleMaps)
data(eq2010, package = "MSG")
boundingbox = qbbox(lon = eq2010[, "long"], lat = eq2010[, "lat"])
EQMap = GetMap.bbox(boundingbox$lonR, boundingbox$latR, destfile = "EQ.png", maptype = "satellite", zoom = 6)
PlotOnStaticMap(EQMap,
lon = eq2010$long, lat = eq2010$lat, pch = 20, col = "red",
verbose = 0
)
bgcol = rgb(255, 0, 0, alpha = 95, maxColorValue = 255)
PlotOnStaticMap(EQMap,
lon = eq2010$lon, lat = eq2010$lat, pch = 21, bg = bgcol,
col = "darkred", cex = 1.25 * eq2010$ms, verbose = 0
)
默认情形下,地图是基于 OSM 的,即 OpenStreetMap,Google Map 的卫星图像出不来 @pzhaonet @yihui 你们在国外 Google Map 应该用起来很方便,不妨抽空查找下原因
下面是目前代码执行后输出的结果
初次编译,输出为 pdf 出错:
tlmgr search --file --global "/AdobeSongStd-Light[.](tfm|afm|mf|otf)"
tlmgr search --file --global "/AdobeSongStd-Light/OT[.](tfm|afm|mf|otf)"
! Package fontspec Error: The font "AdobeSongStd-Light" cannot be found.
! name = AdobeSongStd-Light/OT, rootname = AdobeSongStd-Light/OT, pointsize =
! mktexmf: empty or non-existent rootfile!
! mktexmf: empty or non-existent rootfile!
! kpathsea: Running mktexmf AdobeSongStd-Light/OT.mf
! The command name is C:\Users\peng.zhao\AppData\Roaming\TinyTeX\bin\win32\mktexmf
! Cannot find AdobeSongStd-Light/OT.mf.
! Cannot find AdobeSongStd-Light/OT.mf.
! kpathsea: Running mktextfm AdobeSongStd-Light/OT
! The command name is C:\Users\peng.zhao\AppData\Roaming\TinyTeX\bin\win32\mktextfm
! kpathsea: Appending font creation commands to missfont.log.
Error: Failed to compile _main.tex. See https://yihui.name/tinytex/r/#debugging for debugging tips. See _main.log for more info.
In addition: Warning messages:
1: In parse_packages(logfile, quiet = c(TRUE, FALSE, FALSE)) :
Failed to find a package that contains AdobeSongStd-Light[.](tfm|afm|mf|otf)
2: In parse_packages(logfile, quiet = c(TRUE, FALSE, FALSE)) :
Failed to find a package that contains AdobeSongStd-Light/OT[.](tfm|afm|mf|otf)
Execution halted
Error: Failed to compile the book to bookdown::pdf_book
Execution halted
Error in render_book_script(output_format, envir, quiet) :
Error 1 attempting to render book
Calls: <Anonymous> -> <Anonymous> -> in_dir -> render_book_script
Execution halted
Exited with status 1.
环境信息:
> devtools::session_info()
- Session info ---------------------------------------------------------------------------------------
setting value
version R version 3.6.1 (2019-07-05)
os Windows 10 x64
system x86_64, mingw32
ui RStudio
language (EN)
collate English_United States.1252
ctype English_United States.1252
tz Europe/Berlin
date 2019-08-15
- Packages -------------------------------------------------------------------------------------------
package * version date lib source
assertthat 0.2.1 2019-03-21 [1] CRAN (R 3.6.1)
backports 1.1.4 2019-04-10 [1] CRAN (R 3.6.0)
bookdown 0.12 2019-07-11 [1] CRAN (R 3.5.3)
callr 3.3.1 2019-07-18 [1] CRAN (R 3.6.1)
cli 1.1.0 2019-03-19 [1] CRAN (R 3.6.1)
crayon 1.3.4 2017-09-16 [1] CRAN (R 3.6.1)
desc 1.2.0 2018-05-01 [1] CRAN (R 3.6.1)
devtools 2.1.0 2019-07-06 [1] CRAN (R 3.5.3)
digest 0.6.20 2019-07-04 [1] CRAN (R 3.6.1)
evaluate 0.14 2019-05-28 [1] CRAN (R 3.6.1)
fs 1.3.1 2019-05-06 [1] CRAN (R 3.5.3)
glue 1.3.1 2019-03-12 [1] CRAN (R 3.6.1)
htmltools 0.3.6 2017-04-28 [1] CRAN (R 3.6.1)
knitr 1.24 2019-08-08 [1] CRAN (R 3.6.1)
magrittr 1.5 2014-11-22 [1] CRAN (R 3.6.1)
memoise 1.1.0 2017-04-21 [1] CRAN (R 3.5.2)
MSG * 0.3 2016-02-13 [1] CRAN (R 3.6.1)
packrat 0.5.0 2018-11-14 [1] CRAN (R 3.5.3)
pkgbuild 1.0.4 2019-08-05 [1] CRAN (R 3.6.1)
pkgload 1.0.2 2018-10-29 [1] CRAN (R 3.6.1)
prettyunits 1.0.2 2015-07-13 [1] CRAN (R 3.6.1)
processx 3.4.1 2019-07-18 [1] CRAN (R 3.6.1)
ps 1.3.0 2018-12-21 [1] CRAN (R 3.6.1)
R6 2.4.0 2019-02-14 [1] CRAN (R 3.6.1)
RColorBrewer 1.1-2 2014-12-07 [1] CRAN (R 3.6.0)
Rcpp 1.0.2 2019-07-25 [1] CRAN (R 3.6.1)
remotes 2.1.0 2019-06-24 [1] CRAN (R 3.6.1)
rlang 0.4.0 2019-06-25 [1] CRAN (R 3.6.1)
rmarkdown 1.14 2019-07-12 [1] CRAN (R 3.6.1)
rprojroot 1.3-2 2018-01-03 [1] CRAN (R 3.6.1)
rsconnect 0.8.15 2019-07-22 [1] CRAN (R 3.6.1)
rstudioapi 0.10 2019-03-19 [1] CRAN (R 3.6.1)
sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.5.2)
testthat 2.2.1 2019-07-25 [1] CRAN (R 3.6.1)
usethis 1.5.1 2019-07-04 [1] CRAN (R 3.5.3)
withr 2.1.2 2018-03-15 [1] CRAN (R 3.6.1)
xfun 0.8 2019-06-25 [1] CRAN (R 3.6.1)
yaml 2.2.0 2018-07-25 [1] CRAN (R 3.6.0)
3.1.3 特定颜色主题调色板,介绍三类调色板时使用了描述型列表,不知道该如何转化 @yihui
https://pandoc.org/MANUAL.html#pandocs-markdown 没有找到对应LaTeX的描述型列表
代码详见 https://github.com/fyemath/A-titlepage-for-books 感谢 Fei Ye @fyemath
讨论详见 https://d.cosx.org/d/420857-r/89
第五章 5.1.6 位置调整 举例说明 geom_jitter()
ggplot(aes(x = Petal.Length, y = Petal.Width), data = iris) +
geom_point() +
geom_jitter()
这里实际上把散点画了两次。为了和下面的没有 jitter 的散点图对比,实际上只需要
ggplot(aes(x = Petal.Length, y = Petal.Width), data = iris) +
geom_jitter()
不然第一张图(实际上是点的数目 x 2)和下面的图其实够不成比较。
个人觉得其实只需要类似于
ggplot(aes(x = Petal.Length, y = Petal.Width), data = iris) +
geom_point() +
geom_jitter(color = "red" )
就足以说明 jitter 的作用了,这样其实也不需要下面单独又去画只有 geom_point()
的图了,两个例子糅合到一张图里,既直观又简洁。
举个例子,由于 rggobi
引入对 GGobi
的依赖,在比较新的 macOS 系统里配置安装将相当复杂。简单的做法也许是加个注释说明一下?
实际上我还没有完成安装,目前看来需要先配置好X11和Gtk+ https://zhiyzuo.github.io/installation-rattle/,GGobi 本身倒是很简单。
library(maps) map_data("world", "china") %>% group_by(group) %>% plot_geo(x = ~long, y = ~lat) %>% add_markers(size = I(1))
建议更改为 map_data("world", "antarctica")
等任何其他地区。
animation 包内的两个动画运行起来报错了 @yihui @pzhaonet 帮忙看下能否在其它平台上运行
我计划能在网页中插入动画的尽量插入,而不是放一个截图,能动态生成最好
demo("rgl_animation", package = "animation")
demo(rgl_animation)
---- ~~~~~~~~~~~~~
Type <Return> to start :
> ## how to use saveLatex() with the rgl package
>
> library(animation)
> if (require('rgl')) {
+ saveLatex({
+ ## ajust the view
+ uM =
+ matrix(c(-0.37, -0.51, -0.77, 0, -0.73, 0.67,
+ -0.10, 0, 0.57, 0.53, -0.63, 0, 0, 0, 0, 1),
+ nrow = 4, ncol = 4)
+
+ ## note the width and height are 500px respectively
+ open3d(userMatrix = uM, windowRect = c(30, 30, 530, 530))
+ plot3d(pollen[, 1:3])
+ zm = seq(1, .05, length = 20)
+ par3d(zoom = 1)
+
+ ## the most important argument is use.dev = FALSE!
+ ## and the file extension should be 'png', since we are using rgl.snapshot()
+ for (i in 1:length(zm)) {
+ par3d(zoom = zm[i])
+ rgl.snapshot(sprintf(ani.options('img.fmt'), i))
+ }
+ rgl.close()
+ }, img.name='pollen_demo', use.dev=FALSE,
+ ani.type='png', interval = .2,
+ ani.opts = 'controls,width=3in',
+ latex.filename='rgl_pollen_animation.tex',
+ caption = 'Zoom in a 3D scatter plot to see the truth.',
+ overwrite = FALSE)
+
+ } else warning('You have to install the rgl package first: install.packages("rgl")')
Loading required package: rgl
Error in rgl.snapshot(sprintf(ani.options("img.fmt"), i)) :
pixmap save format not supported in this build
在 RGUI 里运行能画出图来,同时伴随上述警告,而放在 bookdown 里运行,出不来图
demo('ChinaHeart3D', package = 'fun')
demo(ChinaHeart3D)
---- ~~~~~~~~~~~~
> ## 3D heart with a Chinese five-star flag
> ## Author: Yixuan Qiu <[email protected]>
> ## original code in a Chinese forum: http://cos.name/cn/topic/16743
> ## this code was written to celebrate China's 60 anniversary
> if (!require("rgl")) stop("You need the rgl package to generate the 3D heart!")
> xtheta = function(x, theta, y, w = 0, tt = 0) {
+ (x^2 + (x * tan(theta))^2 + 2 * y^2 + 0.1 * cos(w * tt) -
+ 0.9)^3 - (x^2 + y^2/9) * (x * tan(theta))^3
+ }
> fz = function(z, x, y, w = 0, tt = 0) {
+ (x^2 + 2 * y^2 + z^2 + 0.1 * cos(w * tt) - 0.9)^3 - (x^2 + y^2/9) * z^3
+ }
> n = 100
> y = seq(-2, 2, length.out = n)
> y0 = xx = zz = NULL
> for (i in 1:length(y)) {
+ theta = seq(-pi/2, 1.5 * pi, length.out = n)
+ solvex = function(theta, y) {
+ if (theta == -pi/2 | theta == pi/2 | theta == 1.5 * pi) {
+ return(0)
+ } else if (theta > -pi/2 & theta < pi/2) {
+ interval = c(0, 2)
+ } else {
+ interval = c(-2, 0)
+ }
+ x.root = uniroot(xtheta, interval, theta, y)$root
+ return(x.root)
+ }
+ if (xtheta(0, pi/4, y[i]) * xtheta(2, pi/4, y[i]) > 0)
+ next
+ y0 = c(y0, y[i])
+ x = sapply(theta, solvex, y[i])
+ zplus = uniroot(fz, c(0, 2), 0, y[i])$root
+ zminus = uniroot(fz, c(-2, 0), 0, y[i])$root
+ z = numeric(n)
+ z[x != 0] = x[x != 0] * tan(theta[x != 0])
+ z[x == 0] = (theta[x == 0] == pi/2) * zplus + (theta[x == 0] != pi/2) * zminus
+ xx = cbind(xx, x)
+ zz = cbind(zz, z)
+ }
> yy = matrix(rep(y0, n), n, length(y0), byrow = TRUE)
> library(rgl)
> persp3d(zz, xx, yy, col = "red", xlim = c(-1, 1), ylim = c(-1, 1), zlim = c(-1, 1),
+ axes = FALSE, box = FALSE, xlab = "", ylab = "", zlab = "")
> fy = function(y, pars) {
+ z = pars[1]
+ x = pars[2]
+ w = pars[3]
+ tt = pars[4]
+ (x^2 + 2 * y^2 + z^2 + 0.1 * cos(w * tt) - 0.9)^3 - (x^2 + y^2/9) * z^3
+ }
> gety = function(z, x, interval = c(0.01, 1), w = 0, tt = 0) {
+ mpars = cbind(z, x, w, tt)
+ solvey = function(pars) {
+ if (fy(interval[1], pars) * fy(interval[2], pars) > 0) {
+ return(NA)
+ } else {
+ y = uniroot(fy, interval, pars)$root
+ }
+ }
+ y = apply(mpars, 1, solvey)
+ return(y)
+ }
> x0 = z0 = seq(-1, 1, length.out = n)
> y0 = outer(z0, x0, gety)
> persp3d(x = z0, y = x0, z = y0, zlim = c(-1, 1), col = "white",
+ texture = system.file("img", "flag.png", package = "fun"), add = TRUE)
> persp3d(x = z0, y = x0, z = -y0, zlim = c(-1, 1), col = "red", add = TRUE)
Warning messages:
1: In rgl.material(...) : RGL: Pixmap load: file format unsupported
2: In rgl.material(...) : RGL: Pixmap load: failed
在 RGUI 里没有图,只有上述警告
Warning messages:
1: In rgl.material(...) : RGL: Pixmap load: file format unsupported
2: In rgl.material(...) : RGL: Pixmap load: failed
sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8.1 x64 (build 9600)
Matrix products: default
locale:
[1] LC_COLLATE=Chinese (Simplified)_China.936
[2] LC_CTYPE=Chinese (Simplified)_China.936
[3] LC_MONETARY=Chinese (Simplified)_China.936
[4] LC_NUMERIC=C
[5] LC_TIME=Chinese (Simplified)_China.936
attached base packages:
[1] grid stats graphics grDevices utils datasets methods
[8] base
other attached packages:
[1] animation_2.6 htmlwidgets_1.3 rgl_0.100.26
loaded via a namespace (and not attached):
[1] Rcpp_1.0.2 digest_0.6.20 later_0.8.0
[4] mime_0.7 R6_2.4.0 xtable_1.8-4
[7] jsonlite_1.6 magrittr_1.5 evaluate_0.14
[10] miniUI_0.1.1.1 promises_1.0.1 rmarkdown_1.14
[13] webshot_0.5.1 tools_3.6.1 manipulateWidget_0.10.0
[16] crosstalk_1.0.0 shiny_1.3.2 yaml_2.2.0
[19] xfun_0.8 httpuv_1.5.1 compiler_3.6.1
[22] htmltools_0.3.6 knitr_1.24
根据 Pandoc 官方手册 https://pandoc.org/MANUAL.html#tables 这个多行表格我从原稿的 tex 文件,经
pandoc table.tex -o table.md
转化为
Table: (\#tab:plot-summary) 各种类型的数据对应的统计图形概览
---------- --------------- ---------------- ------------------------ --------
一维 二维 高维 矩阵
分类数据 条形图 马赛克图 马赛克图
关联图、四瓣图
连续数据 直方图 散点图 平行坐标图 颜色图
箱线图 散点图矩阵、三维散点图 热图
Cleveland 点图 三维透视图、平滑散点图 等高图
一维散点图 星状图、符号图、脸谱图
混合数据 条件密度图 条件分割图
棘状图
---------- --------------- ---------------- ------------------------ --------
转化后的md表格插入书籍正文后,在本地和 Travis 编译时,都会伴随一个警告
说是我们重复给这个表格打标签,去掉最后一行,警告消失,但是 HTML 格式和 PDF 格式表格都乱了
---------- --------------- ---------------- ------------------------ --------
我不清楚是哪里出问题了,因为我没在其它地方找到 tab:plot-summary
这个标签,并且我将此 md 表格插入到 bookdown-chinese 模板 中一切正常。求助 @yihui @pzhaonet
测试环境: Pandoc 版本 2.7.3
今天添加了图表的目录后发现此问题,图和表的目录应该显示到几级,目前我设定为2级,即深度为2
有些图和子图的短标题缺失,可能是搬迁过程中遗漏,也可能部分没有,还有短标题控制在一行显示完,不然不能叫短标题了,我们需要添加一下
具体情况见 PDF 格式预览版 https://msg-book.netlify.com/_main.pdf 中图的目录
@pzhaonet 我们目前的修改不同步推送到网页版,但是希望能通过测试,以保证书籍内容是可复现的。
目前看到的 Travis 报错信息 https://travis-ci.com/github/XiangyunHuang/MSG-Book/builds/178914761 如下
意思是说少一个图片文件
par(par(mar = c(4.5, 4, 0.2, 0.2)), mfrow = c(2, 1))
chippy <- function(x) sin(cos(x) * exp(-x / 2))
curve(chippy, -8, 7, n = 2008)
curve(sin(x) / x, from = -20, to = 20, n = 200,
xlab = expression(t),
ylab = expression(varphi[X](t)))
sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8.1 x64 (build 9600)
Matrix products: default
locale:
[1] LC_COLLATE=Chinese (Simplified)_China.936
[2] LC_CTYPE=Chinese (Simplified)_China.936
[3] LC_MONETARY=Chinese (Simplified)_China.936
[4] LC_NUMERIC=C
[5] LC_TIME=Chinese (Simplified)_China.936
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_3.6.1
举例如下,
library(grid)
pushViewport(viewport(h = 0.8, w = 0.8, angle = 15))
grid.multipanel(newpage = FALSE)
popViewport()
在 RStudio 内运行
xfun::session_info()
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8.1 x64 (build 9600), RStudio 1.2.1335
Locale:
LC_COLLATE=Chinese (Simplified)_China.936
LC_CTYPE=Chinese (Simplified)_China.936
LC_MONETARY=Chinese (Simplified)_China.936
LC_NUMERIC=C
LC_TIME=Chinese (Simplified)_China.936
Package version:
base64enc_0.1.3 bookdown_0.12 compiler_3.6.1 digest_0.6.20
evaluate_0.14 glue_1.3.1 graphics_3.6.1 grDevices_3.6.1
highr_0.8 htmltools_0.3.6 jsonlite_1.6 knitr_1.24
magrittr_1.5 markdown_1.1 methods_3.6.1 mime_0.7
packrat_0.5.0 Rcpp_1.0.2 rmarkdown_1.14 stats_3.6.1
stringi_1.4.3 stringr_1.4.0 tinytex_0.15 tools_3.6.1
utils_3.6.1 xfun_0.8 yaml_2.2.0
在 R Gui 中运行
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8.1 x64 (build 9600)
Locale:
LC_COLLATE=Chinese (Simplified)_China.936
LC_CTYPE=Chinese (Simplified)_China.936
LC_MONETARY=Chinese (Simplified)_China.936
LC_NUMERIC=C
LC_TIME=Chinese (Simplified)_China.936
Package version:
compiler_3.6.1 graphics_3.6.1 grDevices_3.6.1 stats_3.6.1
tools_3.6.1 utils_3.6.1 xfun_0.8
Travis 上编译出来的效果和 RStudio 内一样,不知问题出在哪? @yihui
在线编译的效果见 https://msg-book.netlify.com/system.html#section-25
以下是任务分配表。认领人一栏为空的,表示暂时无人认领,等待有志之士加入。
章节序号 | 章节标题 | 认领人 | 当前进度 |
---|---|---|---|
0 | 序言 | XiangyunHuang | 完成。 |
1 | 历史 | XiangyunHuang | 完成。 |
2 | 工具 | pzhaonet | 完工。 |
3 | 元素 | XiangyunHuang | 完成。 |
4 | 图库 | pzhaonet | 完工。 |
5 | 系统 | XiangyunHuang | 完工。 |
6 | 数据 | XiangyunHuang | 完成。 |
7 | 原则 | XiangyunHuang | 完成。 |
A | 程序初步 | XiangyunHuang | 完成。 |
B | 细节技巧 | XiangyunHuang | 完成。 |
C | 图形界面 | XiangyunHuang | 完成。 |
D | 本书 R 包 | XiangyunHuang | 完成。 |
搬迁工作完成,即宣告替换/修复完书中不能运行的代码,接下来是代码升级,指的是书中的代码可以运行,但是代码中包含的 R 包作者已经不维护或者停止维护,这类代码可以更新升级,考虑到本书风格偏向 Base R, 我们可以将数据操作尽量限定在这个范围,当然可以有 tidyverse 风格,但是我想把它放在第二版的现代统计图形中,这个有点类似于第一版的 Advanced R 和第二版的 Advanced R 的做法 --- 在讲同一个东西,但是基于不同的工具在讲
现在举例解释何为第一版现代统计图形的代码升级,比如这一段代码(位于第4章第29小节交互效应图)
# ddply 在 plyr 包中,cast 在 reshape 包中
library(plyr)
library(reshape)
tbl <- ddply(esoph, agegp ~ alcgp, summarise, mean(ncases / (ncases + ncontrols)))
cast(tbl, agegp ~ alcgp)
其中 plyr 和 reshape 包都处于维护模式,不再更新了,作者也不推荐使用了,这个升级工作一个走 dplyr 或者 data.table,另一个走 Base R,这样做的目的是希望代码能在相当一段时间内是稳定的,减少后期维护成本。就这本书而言,没有什么大数据, Base R 提供的数据操作足以应付,文风也能统一,我倾向于这个 Base R
事实上,引入的两个依赖可去掉,替换后代码如下
tbl <- aggregate(ncases / (ncases + ncontrols) ~ agegp * alcgp, data = esoph, mean)
colnames(tbl) <- c("agegp", "alcgp", "mean")
reshape(tbl, timevar = "alcgp", idvar = "agegp", direction = "wide", sep = "_")
https://github.com/XiangyunHuang/MSG-Book/blob/edition1/foreword.Rmd
目前的致谢包含了 @boltomli @JackieMium @yiluheihei 请问各位是否愿意实名?愿意的话,还请告知大名。
还有,我们有没有遗漏重要的需要致谢的贡献者? @XiangyunHuang @pzhaonet
我暂接下这口锅,慢慢背
介绍 R 包 plotly
plotly::orca
第 7.1.3 节提到:
Cleveland (1985) 中给了一个省略数据的例子,可以看作是 1986 年美国挑战者号航天飞机失事的原因之一,大意是航天局的工程师们在发射之前研究了 O 型环的故障与温度的关系,他们看的是一幅散点图。
下文就这幅散点图谈了很多,然而这幅图并没有展示。我觉得应该展示出来,不然的话,读者看不到这幅图,读文字的时候没法直观理解。
Cleveland (1985) 的原书我这里找不到。下面是 Maindonald (1997) 给出的图示(全文):
caption 里介绍:Total O-ring incidents versus temperature, in shuttle flights prior to the Challenger disaster of January 28 1986. Points have been jittered slightly, to separate noverlapping points. Circles points are for data that were presented in one of the 13 pre-launch charts.
我猜应该是这幅图,不过我又找到另一幅以及文字描述:
该文章说用的是 Tufte (1997) 的数据。不确定这组数据跟咱们书稿里所说的是不是同一组。
这两幅图明显不同。那么,我们的书稿里指的是哪幅图?
此外,书稿里说 Cleveland (1985) 给出了 1986 年失事的图,这里两个年份是不是有错误?85年何以给出下一年出事故的分析图呢?查了一下,失事确实是 1986 年,而 The Elements of Graphing Data 第一版也确实在 1985 年出版。
目前的预览版(截至修复一大堆交叉引用),图中的中文都显示为乱码。
第 4 章,无论是图片尺寸,还是图里的文字大小,看上去都太大了,比我在本地预览时扩大了一些。我在迁移时,绝大部分图片都在 chunk options 里保留了原稿的长宽绝对尺寸。是不是网页版的图片宽度设成了 100%?
原稿第 4.2 节“茎叶图”的图 4.3 和 4.4,本身的内容不是图,而是一段代码运行的结果。原稿里强行作为插图处理,添加了 caption 和 label,并在其他地方交叉引用。
这个功能在 bookdown 里如何实现?
第一章历史第一节饼图和线图的起源,这里有两幅图,每幅图的 caption 标题比较长,所以我用了文本交叉引用的方式,当我把参考文献加入的时候,出现了错乱,当我把参考文献的引用去掉后显示正常
@yihui 关于这一点,不知道是哪一环节有问题?也许是 bookdown 或 pandoc 的 bug
\begin{center}\animategraphics[width=0.7\linewidth,controls,loop]{1}{system_files/figure-latex/unnamed-chunk-12-}{1}{2}\end{center}
layout(matrix(1:2, 2), heights = c(3, 1))
plot(sunspots)
plot(sunspots, asp = .1)
在 R GUI 中运行上述命令,报如下错误
Error in plot.new() : figure margins too large
受 https://plotly-r.com/embedding-images.html 的启发,我们似乎也可以这样搞,正文呈现静态图,点击这个静态图弹出一个网页,这个弹出网页为动画、交互图独占,不受正文空间限制,随意旋转把玩,特别是 3D 图形,把玩起来会有种解放的感觉
另一个好处是编译的时候可以方便地生成 PDF 了,正文仍然插入静态图嘛!
对弹窗敏感的人慎入,而且这种方式不适合插入太多的同类图形,造成每个 HTML 文件重复加载一堆 JS 库
受 @yihui 和 @XiangyunHuang 委托,我来接管《现代统计图形》一书的出版事宜。修订方案如下。
基本原则:审校文字与格式。如无必要,现有文字不做改动,尽最大可能保持原貌。项目的 issues 里有很多好主意,只是难以近期落实,只得作为远期目标,将来时机成熟后以第二版的形式面世。所以,计划出版的第一版书稿将以稳定版(v1.4)为出发点,也就是分支 edition1,尽量落实 issues 里容易实现的想法。
以下为进度表。已打叉:已完成;未打叉的斜体:正在进行中;未打叉的正体:尚未开工。
开头:
调整全书结构:
增改内容:
文字和格式调整:
收尾:
可选内容
关于书稿需要增加 ggplot2 篇幅(摘自我发给出版社的邮件):
其实该书稿有个分支,叫做"现代统计图形之极乐净土",是在原书稿基础上做的一个尝试,即把原书稿篇幅最大的第4章“图库”里的基础作图全部用 ggplot2 重做一遍,已经大部分完成。曾经想过是不是可以把本书全盘 ggplot 化,可是,本书实质是系统介绍“R 基础作图”(例如第3章“元素”),如果全盘替换,则需要用第 5.1 节“ggplot2 图形”来代替第3章,要修订的工作量很大。同时,我意识到,本书让人读起来过瘾的部分正是对基础作图的讲解。全盘替换的话,此书恐怕会面目全非,而市面上会损失一本很多人期待的基础作图参考书。
基础作图是本书亮点,ggplot2 不可小觑,两者应该兼顾。
估计有相当一部分人到时候买这本书纯粹是为了怀旧了或者满足一下“活久见”的心愿。
以下摘自我发给出版社的邮件:
此书虽是以 R 语言为载体,然而其中的**完全不限于 R 语言,是“通过 R 基础作图来呈现统计**”。在以前的讨论中,我提出一个想法,就是"图形为体,R语言为用",把书稿里所有"术"的内容删掉,只论"道",以便让更大的读者群从书中受益。或者说,把此书的灵魂作为芭比娃娃的身体,而外在表现无论是 R 语言还是别的什么作图工具,都是芭比娃娃的衣服。每换一套衣服,都可以出一本书。不过,大概是这个想法太过激进,实现起来难度大,要修订的工作量太多,就仅仅作为谈资丢在那里了。
综合上面的考虑,此书的目标读者群是:
如果说原书稿的灵魂是“通过 R 基础作图来呈现统计**”,那么此次修订,将书稿的灵魂将调整为:“通过 R 基础作图和 ggplot2 作图来呈现统计**”。
开源社区为本书的写作贡献灵感和素材,出版社为本书的出版贡献人力和物力。二者需要平衡。方案如下:
欢迎大家补充。众口难调,大家的意见不一定会采纳,但是一定会被慎重考虑。
保存高质量的 png 文件,同时文件比较小,pdf 文档实际是 png 图片
# install.packages('devtools')
devtools::install_github("r-lib/ragg")
devtools::install_github("ilarischeinin/rasterpdf")
library(ggplot2)
library(rasterpdf)
raster_pdf("diamonds-agg.pdf", res = 300, width = 6, height = 5,
png_function = ragg::agg_png)
ggplot(diamonds, aes(carat, price)) + geom_point()
dev.off()
有时候,他人先于你一步推送到主分支,这个时候首先需要更新本地分支,拉取远程的新提交
git pull
# 解决冲突后,添加新的提交
git commit -m "新提交的说明"
git rebase # 清理多余的合并信息
git push # 将修改推送到远程分支
多人协作开发,经常出现合并冲突的问题,最好还是每次更新 master 主分支后,从主分支新建一个新的分支,然后在新分支上开始新的修改内容
运行过程如下
demo("SongWordsNet", package = "MSG")
demo(SongWordsNet)
---- ~~~~~~~~~~~~
Type <Return> to start :
> require("igraph")
Loading required package: igraph
Attaching package: ‘igraph’
The following objects are masked from ‘package:stats’:
decompose, spectrum
The following object is masked from ‘package:base’:
union
> load(system.file('extdata','HighFreq100.rda',package='MSG'))
> g <- graph.adjacency((HighFreq100>0.05)*HighFreq100,mode="undirected",weighted=T, diag=F)
> cg <- clusters(g)
> colbar <- as.numeric(as.factor(cg$csize[cg$membership+1]))
> V(g)$color <- rev(heat.colors(9))[colbar]
> ff <- as.numeric(cut(g[[9]][[4]]$weight,breaks=c(0.05,0.1,0.2,0.3,0.4)),right=F)
Error in g[[9]][[4]] : subscript out of bounds
Error in g[[9]][[4]] : subscript out of bounds
sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8.1 x64 (build 9600)
Matrix products: default
locale:
[1] LC_COLLATE=Chinese (Simplified)_China.936
[2] LC_CTYPE=Chinese (Simplified)_China.936
[3] LC_MONETARY=Chinese (Simplified)_China.936
[4] LC_NUMERIC=C
[5] LC_TIME=Chinese (Simplified)_China.936
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] igraph_1.2.4.1
loaded via a namespace (and not attached):
[1] compiler_3.6.1 magrittr_1.5 tools_3.6.1 pkgconfig_2.0.2
原稿使用了大量的 usage()
函数来展示函数用法,然而基本都无法正常运行了。
例如,原文的 usage(boxplot,'default')
,现在要改成 usage(boxplot.default)
才行。
那么,usage(boxplot,'formula')
,现在该怎么写?
这一类问题应该不会少,单开一个 issues 大家把遇到的有问题的链接,贴在下方
这个问题主要请 @yihui 修复
符号含义:3.1.4 表示 第3章第1节第4小节,紧跟问题链接,如果链接已被修复请勾选
3.1.4 https://www.stat.auckland.ac.nz/ihaka/Graphics/warming.html
3.2 图3.4下方 http://yihui.name/cn/?p=1440 -> https://yihui.name/cn/2010/08/art-of-points-in-r/
6.2 图 6.15 下方 http://yihui.name/cn/?p=1172 -> https://yihui.name/cn/2009/12/statistics-in-their-eyes/
6.2.10 图 6.18 下方 http://bit.ly/fq2jq8 这个链接有效,但 bit.ly 在墙外。它指向的是 https://github.com/tidyverse/ggplot2/wiki/Crime-in-Downtown-Houston,-Texas-:-Combining-ggplot2-and-Google-Maps 你可以用国内的短链接服务重新缩一下。
6.3.3 图 6.24 下方 http://yihui.name/cn/?p=173 -> https://yihui.name/cn/2008/09/multiple-outliers-detection/
可能是导致以下warnings的原因
···
[WARNING] Citeproc: citation fun not found
[WARNING] Citeproc: citation heatmaply not found
[WARNING] Citeproc: citation hexbin not found
[WARNING] Citeproc: citation iplots not found
[WARNING] Citeproc: citation leaflet not found
[WARNING] Citeproc: citation rggobi not found
[WARNING] Citeproc: citation tuneR not found
···
代码格式下,如果字符串是中文,那么在 LaTeX 输出的 pdf 里,字符串的引号和第一个中文字符之间会出现一个冗余空格。
比如书稿里下面这一段:
https://github.com/XiangyunHuang/MSG-Book/blob/master/elements.Rmd#L480-L495
会出来 " 八月第一周"
这样 的代码,“八”字前面多了个空格。
我看了一下 @yihui 的原文(即用 LyX 制作的 2011 年4 月2 日 版),也同样存在这个问题。
不光是代码块,如果正文中使用行内代码,也会出现同样的问题。
不知怎么破?
参考文献无论是书籍类还是文章类,最好补充给出原始链接,方便读者直接去查看原始材料
本书前言里给出了本书的永久链接:
建议将https://yihui.name/cn/publication/换个更方便和稳定的域名,指向 https://bookdown.org/xiangyun/MSG-Book/。眼见修订版影响力越来越大,此事宜早。
@pzhaonet 在另一个issues 提及了搬迁和修改原稿的事情#11 (comment),我觉得很重要,为了防止我用力过猛 https://github.com/XiangyunHuang/MSG-Book/issues/1 和统一大家的搬迁节奏,控制搬迁进程。特开一贴说明
R 包超过了 2500 个
如果真是简单到这种程度,改了也无妨,当然也完全可以保留。因为它属于轻微更新的范围。
涉及风格的地方,比如第5章系统之ggplot2图形一节全部基于 qplot
画图,我就没有改到 ggplot
里来,虽然官方书籍 《ggplot2: Elegant Graphics for Data Analysis》 在第二版(现在已经进行到第三版)已经将 qplot
的介绍去掉了,并且理由很充分的样子:
Switched from
qplot()
toggplot()
in the introduction, Chapter 2. Feedback indicated thatqplot()
was a crutch: it makes simple plots a little easier, but it doesn’t help with mastering the grammar.
这一类属于中等程度的更新,可以是个人喜好,也可以算是内容过时,但并不妨碍代码运行和出图
我在搬迁第五章系统的时候, ggplot2 的变动稍多一些,如果运行不了(原有的函数现在没有了),我就找了现在对应的函数,比如 opts
-> theme
和 ggpcp
-> GGally::ggparcoord
。这是属于严重更新的范围,不更新的话,图出不来,代码运行不了。
这一类属于丰富原来的图形呈现形式,我们知道 R Markdown 支持丰富的图形表现形式,静态图, GIF 动图, Shiny 嵌入,视频嵌入,甚至基于 JS 库的 (如 plotly) 交互式图形嵌入和 rgl 真三维动态交互图形的嵌入。
从 LyX 搬迁到 bookdown 的一大优势就是丰富图形展现手段,增加书籍和读者的互动,最终实现文学编程的终极目的 @yihui
这一类更新比较难操作,可暂时不操作,据我观察,这类操作也主要集中在第5章,我目前做的程度是能动态生成并嵌入的都做了,有限于能力和源代码可能存在的 BUG 的地方就没有更新
animation 包的 saveHTML 可否将动画保存为一个独立的文件 @yihui
这个保存得到的 HTML 文件,伴随有 images 目录和 CSS 目录,使用起来不太便携
saveHTML({
ani.options(interval = 0.05, nmax = 50)
par(mar = c(4, 4, .1, 0.1), mgp = c(2, 0.7, 0))
brownian.motion(pch = 21, cex = 5, col = "red", bg = "yellow")
},
img.name = "bm-plot", title = "Demonstration of the Brownian Motion",
description = c(
"Random walk", "on the 2D plane: for each point",
"(x, y), x = x + rnorm(1) and y = y + rnorm(1)."
), ani.height = 300, ani.width = 550
)
编译会失败,去掉才行
The extension ascii_identifiers is not supported for markdown
错误: pandoc document conversion failed with error 23
停止执行
错误: Failed to compile the book to bookdown::gitbook
停止执行
make: *** [gitbook] Error 1
Noto 安装部署好像容易一些,而且实际上和 Adobe 没什么区别。
例如 6.2 数据案例,指向链接为 https://msg-book.netlify.app/history.html#section-3
之前自己在使用bookdown时也遇到类似问题,更新bookdown之后问题解决。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.