在matlib包中,https://github.com/friendly/matlib/,我有一个函数,vectors3d()用于绘制几何矢量图.
下面的代码给出了一个示例图,显示了单位矢量"J"及其在X,Y,Z轴上的一些投影.在调用中segments3d,每个参数都是一个2 x 3矩阵,给出了开始/结束坐标.
if (!require(matlib)) devtools::install_github(friendly/matlib)
library(matlib)
library(rgl)
vec <- rbind(diag(3), c(1,1,1))
rownames(vec) <- c("X", "Y", "Z", "J")
open3d()
vectors3d(vec, col=c(rep("black",3), "red"), lwd=2)
# draw the XZ plane, whose equation is Z=0
planes3d(0, 0, 1, 0, col="gray", alpha=0.2)
# show projections of the unit vector J
segments3d(v1 <- rbind( c(1,1,1), c(1, 1, 0)))
segments3d(v2 <- rbind( c(0,0,0), c(1, 1, 0)))
segments3d(v3 <- rbind( c(1,0,0), c(1, 1, 0)))
segments3d(v4 <- rbind( c(0,1,0), …Run Code Online (Sandbox Code Playgroud) 在.Rmd下面带有标题的文件中,我想要包含一个摘要,所以我尝试了标准的LateX文章表单,
\abstract{This paper explores a variety of topics related to the question of testing the equality of
covariance matrices in multivariate linear models, particularly in the MANOVA setting.
The main focus is on graphical methods that can be used to understand features of data related
to this question.}
Run Code Online (Sandbox Code Playgroud)
但是,令人惊讶的是(我知道这看起来很奇怪),我的References部分中的引用变得格式错误 - 引用之间没有间距,奇数缩进.那么,我怎样才能包含一些看似抽象的东西呢?
我的YAML标题是:
---
title: "Notes on Testing Equality of Covariance Matrices"
author: "Michael Friendly"
date: '`r format(Sys.time(), "%B %d, %Y")`'
output:
pdf_document:
fig_caption: yes
keep_tex: yes
number_sections: yes …Run Code Online (Sandbox Code Playgroud) 出于教学目的,我希望能够并排打印或显示矩阵和向量,通常用于说明矩阵方程的结果,如$ A x = b $.
我可以使用SAS/IML执行此操作,其中print语句采用(空格分隔)表达式的任意集合,计算它们并打印结果,例如,
print A ' * ' x '=' (A * x) '=' b;
A X #TEM1001 B
1 1 -4 * 0.733 = 2 = 2
1 -2 1 -0.33 1 1
1 1 1 -0.4 0 0
Run Code Online (Sandbox Code Playgroud)
请注意,引用的字符串按原样打印.
我已经搜索过了,但在R中却找不到这样的东西.我想这样的事情可以通过一个函数来完成,showObj(object, ...)它取一个参数列表,将每一个格式化为一个字符块,并将它们并排加入.
这种方法的另一个用途是将3D阵列显示为其切片的并排集合的紧凑方式.
这是响铃还是有人有建议开始?
在一个包中,我有几个使用数学符号的小插曲,例如:
This vignette illustrates the ideas behind solving systems of linear equations of the form $\mathbf{A x = b}$
where
- $\mathbf{A}$ is an $m \times n$ matrix of coefficients for $m$ equations in $n$ unknowns
- $\mathbf{x}$ is an $n \times 1$ vector unknowns, $x_1, x_2, \dots x_n$
- $\mathbf{b}$ is an $m \times 1$ vector of constants, the "right-hand sides" of the equations
Run Code Online (Sandbox Code Playgroud)
当我使用R Studio(Ctrl+ Shift+ K)编译小插图时,无论在R Studio查看器中还是在浏览器中查看,小插图都可以在HTML文件中正确呈现。
然而,当包被提交给CRAN和插图都建有从源码包,数学没有正确渲染,但显示为标记,
我需要在YAML标头中添加一些内容,以便在R Studio外部构建的小插图能够正确呈现数学吗?当前,我只使用一个标准头文件:
--- …Run Code Online (Sandbox Code Playgroud) 在许多脚本中,我首先在屏幕上开发一个图形,然后需要将其保存为具有特定高度/宽度/分辨率的多种文件格式。使用png(), pdf(), svg(), ... 打开一个设备,然后dev.off()关闭它,我被迫将所有设备打开调用放入我的脚本中并将它们注释掉并一次一个设备重新运行代码。
我知道对于 ggplot 图形,ggsave()使这更容易。对于base-R和点阵图形,我可以做些什么来简化它?
一个例子:
png(filename="myplot.png", width=6, height=5, res=300, units="in")
# svg(filename="myplot.svg", width=6, height=5)
# pdf(filename="myplot.pdf", width=6, height=5)
op <- par() # set graphics parameters
plot() # do the plot
par(op)
dev.off()
Run Code Online (Sandbox Code Playgroud) [仅供参考:这个问题与 rmarkdown 有关:如何为文档使用多个参考书目
在 LaTeX 文档甚至 Rmarkdown 中.Rnw,我可以简单地使用类似的东西
\bibliography{graphics, statistics, timeref}
Run Code Online (Sandbox Code Playgroud)
让 BibTeX
在我的本地目录下搜索文件graphics.bib、statistics.bib、 和。timeref.bibtexmf
在.Rmd使用yaml标题的文件中,我被迫使用绝对路径(不可移植)或相对路径(笨重、容易出错)列出每个参考书目文件。这是最近的一个例子:
---
title: "My Cool Paper"
author: "Me"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
html_document
bibliography:
- "../../../localtexmf/bibtex/bib/graphics.bib"
- "../../../localtexmf/bibtex/bib/statistics.bib"
- "../../../localtexmf/bibtex/bib/timeref.bib"
---
Run Code Online (Sandbox Code Playgroud)
问题:就像我可以r format(Sys.time(), '%d %B, %Y')使用 R 来填写日期一样,我可以使用一些 R 表达式来查找/填写 .bib 文件的路径吗bibliography:?
好的,从前面的问题来看,我尝试使用
bibliography:
- "`r system('kpsewhich graphics.bib')`"
- "`r system('kpsewhich statistics.bib')`" …Run Code Online (Sandbox Code Playgroud) 在一个...用于提供参数列表的简单函数中,该函数是否可以找到从调用环境传递的对象的名称?如果是这样,怎么样?
这出现在问题的背景下,并排打印矩阵和向量,但可能更为一般.
在该上下文中,参数...还可以包括不需要名称的字符串.这是我的MWE,我尝试过使用deparse(substitute()),但无济于事.
test_names <- function(...) {
# get arguments
args <- list(...)
chars <- sapply(args, is.character)
names <- sapply(args, function(x) if(is.character(x)) " " else deparse(substitute(x)))
names
}
Run Code Online (Sandbox Code Playgroud)
测试:
A = matrix(c(0.5, 1, 3, 0.75, 2.8, 4), nrow = 2)
x = c(0.5, 3.7, 2.3)
y = c(0.7, -1.2)
b = A %*% x - y
> test_names(A, " * ", x, " - ", y, " = …Run Code Online (Sandbox Code Playgroud) 为了演示3D中线性变换的效果x -> A x,我想绘制一个立方体并在其下显示其变换A.为此,我需要分别为每个面着色,还要显示顶点和每个面的轮廓线.
我无法弄清楚如何为面部使用不同的颜色,以及如何使这更加通用,所以我不必重复转换结果的所有步骤.
我试过的:
library(rgl)
c3d <- cube3d(color=rainbow(6), alpha=0.5)
open3d()
shade3d(c3d)
points3d(t(c3d$vb), size=5)
for (i in 1:6)
lines3d(t(c3d$vb)[c3d$ib[,i],])
Run Code Online (Sandbox Code Playgroud)
这给出了下面的图像.但我不明白脸是如何着色的.并且,我似乎必须使用points3d和形状lines3d的组件c3d,并没有我可以转换的单个对象.
A下面的矩阵给出了一个特殊的变换,这是我如何将它添加到场景中,
A <- matrix(c( 1, 0, 1, 0, 2, 0, 1, 0, 2), 3, 3)
c3d_trans <- transform3d(c3d, A)
shade3d( c3d_trans )
points3d(t(c3d_trans$vb), size=5)
Run Code Online (Sandbox Code Playgroud)
这给出了:
有没有办法简化这个并使它更普遍有用?
下图是Michael Clark使用的概念图, https://m-clark.github.io/docs/lord/index.html ,用于解释洛德悖论以及回归中的相关现象。
我的问题是在这种背景下提出并使用的ggplot2,但它在几何和图形方面更广泛。
我想重现这样的数字,但使用实际数据。我需要知道:
y-xy-x投影到该轴上的值的其他表示形式。我的最小基本示例使用ggplot2,
library(ggplot2)
set.seed(1234)
N <- 200
group <- rep(c(0, 1), each = N/2)
initial <- .75*group + rnorm(N, sd=.25)
final <- .4*initial + .5*group + rnorm(N, sd=.1)
change <- final - initial
df <- data.frame(id = factor(1:N),
group = factor(group,
labels = c('Female', 'Male')),
initial,
final,
change)
#head(df)
#' plot, with regression lines and data ellipses
ggplot(df, aes(x = initial, y = final, …Run Code Online (Sandbox Code Playgroud) 在R包中,我有两个传统的Sweave小插图作为.Rnw文件.当我在本地检查包时(在R Studio:Build - > Check中),没有错误或警告,当使用R版本3.3.1在R-Forge上检查我的包时,同样的事情就会发生(2016-10) -17 r71531).
但是,当我将包提交给Win-builder(via devtools::build_win())以便在R devel(unstable)(2016-10-16 r71524)下进行检查时,我收到以下警告:我无法解决:
Files in the 'vignettes' directory newer than all files in 'inst/doc':
'fig/plot-Ortho-fm1.pdf', 'fig/plot-Ortho-fm3.pdf',
'fig/plot-grades-can-class.pdf', 'fig/plot-grades-can-gpa.pdf',
'fig/plot-grades-can-sex.pdf', 'fig/plot-grades-pairs.pdf',
'fig/plot-hern-can-age.pdf', 'fig/plot-hern-can-build.pdf',
'fig/plot-hern-can-cardiac.pdf', 'fig/plot-hern-can-pstat.pdf',
'fig/plot-hern-pairs.pdf', 'fig/plot-jury-can1.pdf',
...
Run Code Online (Sandbox Code Playgroud)
我的包目录结构包括在短文vignettes/和的结果devtools::build_vignettes()在inst/doc/如下:
...
man/
inst/
doc/HE-examples.{pdf,R,Rnw}
doc/repeated.{pdf,R,Rnw}
R/
vignettes/HE-examples.Rnw
/repeated.Rnw
/fig/ # all generated fig files
Run Code Online (Sandbox Code Playgroud)
我包含生成的.pdf文件的原因inst/doc/是,否则我得到一个警告,.pdf需要压缩文件qpdf,我实际可以做的唯一方法是在Acrobat Pro中打开它们并重新保存为压缩PDF.
我已经尝试添加inst/doc到.Rbuildignore,但产生一个错误.我也尝试添加inst/doc/*.pdf到.Rbuildignore但没有任何效果.
该警告阻止我向CRAN提交更新.我不明白为什么较新的文件vignettes/ …
在下面的三元图中,轴外部的标签被剪掉。我找不到可以避免这种情况的ggtern()设置ggplot2。对于基本 R 图形,我只会使用xpd=TRUE.
我的数据:
modes <-
structure(list(Mode = c("Literature", "Poetry", "Table", "Map",
"Thematic map", "Graph", "Art", "Diagram", "Statistical analysis",
"Statistical graphics"), Words = c(9L, 7L, 1L, 2L, 2L, 1L, 4L,
2L, 6L, 8L), Numbers = c(0.5, 1, 9, 2, 8, 1, 1, 0, 15, 8), Pictures = c(1L,
4L, 1L, 9L, 14L, 11L, 8L, 9L, 5L, 15L)), .Names = c("Mode", "Words",
"Numbers", "Pictures"), problems = structure(list(row = 10L,
col = NA_character_, expected = "4 …Run Code Online (Sandbox Code Playgroud) 在一个。rmd过去使用YAML行进行干净编译的文档,
output:
pdf_document:
fig_caption: yes
keep_tex: yes
number_sections: yes
includes:
in_header: mystyles.tex spacing.tex
...
Run Code Online (Sandbox Code Playgroud)
我现在遇到一个pandoc错误,就像mystyles.tex spacing.tex cannot be found
我现在正在使用R 3.3.2和R Studio 1.0.153。我还尝试了其他几种形式(给出了不同的错误):
in_header: "mystyles.tex" "spacing.tex"
in_header: mystyles.tex
in_header: spacing.tex
Run Code Online (Sandbox Code Playgroud)
如果我在两个文件中合并到LateX代码,并且只使用
in_header: mystyles.tex
Run Code Online (Sandbox Code Playgroud)
in_header一行中多个文件的语法是什么?它改变了吗?
我知道关于这个主题有很多问题,但大多数都是相当微不足道的,我无法为我的案例找到解决方案.
我有一组HTML文件,包含许多很多"媒体"项,如下所示,每个都是"段落",用"\n \n"分隔.这是我正在处理的类型的示例文件的链接.
<li class="media">
<div class="media-left">
<a href="#">
<img class="media-object" src="4_17-HE-assoc.png" width="250" alt="...">
</a>
</div>
<div class="media-body">
<h4 class="media-heading">Figure 4.17</h4>
Association plot for the hair-color eye-color data. Left: marginal table, collapsed over
gender; right: full table.
</div>
</li>
Run Code Online (Sandbox Code Playgroud)
对于每个<img ...>标记,我需要找到该src="file"值,并替换href="#"上一行中的值href="file" class="fancybox.即,这样项目就会如此
<li class="media">
<div class="media-left">
<a href="4_17-HE-assoc.png" class="fancybox">
<img class="media-object" src="4_17-HE-assoc.png" width="250" alt="...">
</a>
</div>
<div class="media-body">
<h4 class="media-heading">Figure 4.17</h4>
Association plot for the hair-color eye-color data. …Run Code Online (Sandbox Code Playgroud)