具有不同图形包装图形的面板 [英] Panel with different graphic packages figures

查看:72
本文介绍了具有不同图形包装图形的面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这个问题能正确陈述.出于教学目的,我想创建一个编织文档,在面板中比较R基础图形系统的输出和Lattice软件包的输出. 考虑一个数值向量:

I hope the question is correctly stated. I would like to create, for didactic purposes, a knitr document where the outputs of the R base graphics system and the outputs of the Lattice package are compared in a panel. Consider a numeric vector:

```{r dataload}
e2 <- c(72, 79, 81, 80, 63, 62, 89, 90, 50, 78, 87, 97, 55, 69, 97, 87, 
        88, 99, 76, 78, 65, 77, 88, 90, 81)
```

我试图用以下代码制作一个面板:

I tried to produce a panel with the following code:

```{r plots}
par(mfrow = c(1,2))    #Set 1 row 2 columns panel
hist(e2)               #Base graphic histogram on the left
histogram(~e2)         #Lattice histogram on the right
```

但是Lattice直方图会关闭面板,删除基本图形直方图.

But the Lattice histogram closes the panel, deleting the Base graphics histogram.

预期结果:

面板应在一侧报告基本图,在另一侧报告格子图,类似于此非常漂亮的产品

The panel should report the Base graph on a side and the Lattice graph on the other, similarly to the product of this really beautiful post. Unfortunately, the blog does not explain how the result is produced, and I did not find further information on the problem. I could easily produce two different images, but I think the panel representation produces a more beautifully comparable result

非常感谢您的耐心等候.

Thank you very much for your patience.

推荐答案

如所讨论的文章 ://r.789695.n4.nabble.com/combine-lattice-plot-and-standard-R-plot-tp3496409p3497717.html"rel =" nofollow noreferrer>此条目证明了它可以完成,但有一些复杂性.但是,这种简单的案例不值得花力气,因为一个更简单的解决方案是生成两个独立的图并将它们并排放置.

As discussed here on the R mailing list, the compatibility of lattice and base R graphics is limited. The article that is referenced in this entry demonstrates that it can be done, but with some complications. However, this simple case is not worth the effort because a much simpler solution is to generate two independent plots and place them side by side.

这引出了一个新问题:如何在Rmarkdown中并排放置两个图形?有一些答案可以解释如何与knitr并排放置图形.请参阅LaTeX/RNW文档. 此答案中提供了Rmarkdown的可能解决方案,但我想提出另一种简单的方法涉及添加自定义CSS:

This leads to the new question: How to place two figures side by side in Rmarkdown? There are some answers that explain how to place figures side by side with knitr, but most of them refer to LaTeX/RNW documents. A possible solution for Rmarkdown is presented in this answer, but I'd like to suggest another, simple approach that doesn't involve adding custom CSS:

```{r mychunk, fig.show='hide'}
library(knitr)
library(lattice)
hist(iris$Sepal.Length)
histogram(iris$Sepal.Length)
```

base|lattice
-------------------------------|-------------------------------:
`r include_graphics(paste0(opts_chunk$get("fig.path"), "mychunk-1.png"))`|`r include_graphics(paste0(opts_chunk$get("fig.path"), "mychunk-2.png"))`

块选项fig.show = 'hide' 禁止自动打印图形.无论如何都会生成图形,然后可以使用include_graphics以及通过opts_chunk$get("fig.path")获得的图形目录的路径以及knitr将图像命名为fig.path-label-i,其中i从1开始递增"的事实来插入图形. "(来源:上一个链接).

The chunk option fig.show = 'hide' suppresses automatic printing of the figures. The figures are produced anyways and can then be inserted using include_graphics in combination with the path to the figures directory obtained using opts_chunk$get("fig.path") and the fact that knitr names "images as fig.path-label-i where i is incremental from 1" (source: previous link).

使用 Pandoc pipe_table 实现并排布局.这很方便,因为可以通过简单地在第二行中添加或删除破折号来控制图像的宽度.

The side by side layout is achieved using a Pandoc pipe_table. This is convenient because the width of the images can be controlled by simply adding or removing dashes from the second row.

注意:在HTML文档中,除非 YAML选项self_contained false,使用数据URI(而非文件)包含图像.尽管如此,上述解决方案仍然有效,因为在knitr将RMD文件处理为MD时,图像或尚未转换为数据URI.下一步是Pandoc通过MD文件生成HTML.

Note: In HTML documents, unless the YAML option self_contained is false, images are included using data URIs, not files. The solution above works nonetheless because at the point where knitr processes the RMD file to MD, the images or not yet converted to data URIs; this occurs in the next step where Pandoc generates HTML from the MD file.

这篇关于具有不同图形包装图形的面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆