如何在RMarkdown表中嵌入一个图?

Xan*_*der 5 plot r knitr r-markdown

我正在尝试在RMarkdown中生成报告模板.在本报告中,我想在单元格中的一个图表旁边有我正在使用的调查问题的数量和文本.我生成了一些示例图,并在MS Word中制作了以下示例.是否可以在RMarkdown中将R生成的图放在表中?

代码看起来会是什么样的?

期望的输出

编辑(9/8/16):我现在包含.Rmd文件.问题是html文件无法使用以下消息进行编译.

pandoc: Could not fetch Template1_files/figure-html/score_table-1.png
Template1_files/figure-html/score_table-1.png: openBinaryFile: does not exist (No such file or directory)
Error: pandoc document conversion failed with error 67
Execution halted
Run Code Online (Sandbox Code Playgroud)

Template1是文件名,score_table是块标签.

有人愿意帮我诊断问题吗?

<meta charset="utf-8">
---
title: "Untitled"
author: "Xander"
date: "September 7, 2016"
output: html_document
    self_contained: false
---

```{r mychunk, fig.show = "hide", echo = FALSE, fig.height=3, fig.width=5}
library(knitr)
library(ggplot2)

# sample data
dat <- data.frame(text = sapply(1:10, FUN = function(x) { paste0(sample(x = LETTERS, size = 15), collapse = "") }))
score_set = replicate(n = 10, expr = {data.frame(other = c("other", "other", "other", "other"), score=sample(1:7,4,TRUE))},simplify = F)

#Plot Function
plotgen<-function(score_set,other,score){
  p <- ggplot(score_set, aes(factor(other), score))
  p + geom_violin(fill = "#99CCFF") + coord_flip() + scale_x_discrete(name=NULL) +
  scale_y_continuous(breaks = round(seq(1, 7, by = 1),1), limits = c(1,7), name=NULL) + theme(axis.text.y=element_blank(),axis.title.y=element_blank(),axis.ticks.y=element_blank(),
      panel.grid.major.y = element_line(colour = "black"),
      panel.grid.minor = element_blank(),
      panel.background = element_rect(fill = "white"),
      panel.border = element_rect(colour = "black", fill=NA, size=1)) +
geom_hline(yintercept=sample(1:7,1,TRUE), size = 1.5, colour = "#334466")
}

# generate plots
invisible(lapply(seq_along(score_set), FUN = function(x) {plotgen(score_set[[x]],other,score)}))

out <- cbind(row.names(dat), 
         as.character(dat$text), 
         sprintf("![](%s%s-%s.png)", opts_current$get("fig.path"), opts_current$get("label"), 1:nrow(dat)))
kable(out, col.names = c("ID", "Text", ""))
````
Run Code Online (Sandbox Code Playgroud)

CL.*_*CL. 6

你可以做如下(为基地地块,ggplot2图-见注释在最底层):

```{r mychunk, fig.show = "hide", echo = FALSE, fig.height=3, fig.width=5}
library(knitr)
# sample data
dat <- data.frame(
  text = sapply(1:10, FUN = function(x) { paste0(sample(x = LETTERS, size = 15), collapse = "") }), 
  x1 = rnorm(10), 
  x2 = rnorm(10, mean = 3), 
  x3 = rnorm(10, mean = 5))

# generate plots
invisible(apply(dat[, 2:4], MARGIN = 1, FUN = boxplot))

out <- cbind(row.names(dat), 
             as.character(dat$text), 
             sprintf("![](%s%s-%s.png)", opts_current$get("fig.path"), opts_current$get("label"), 1:nrow(dat)))
kable(out, col.names = c("ID", "Text", "Boxplot"))
```
Run Code Online (Sandbox Code Playgroud)
  • apply(dat[, 2:4], MARGIN = 1, FUN = boxplot)使用来自x1x2和的数据生成箱线图x3。这些数字已生成但未包含在文档中,因为fig.show="hide"
  • sprintf("![](%s%s-%s.png)", opts_current$get("fig.path"), opts_current$get("label"), 1:nrow(dat))生成降价语法以包含绘图。这类似于调用,include_graphics但优点是我们将降价作为字符向量。
  • 最后,kable生成表。

或者,您可以使用 Pandoc 手动生成表格,pipe_table 如下所示,这提供了更大的灵活性。

上面代码的输出:

表中图

Gregor's answer here显示了如何将其应用于ggplot2绘图:list逐个打印apply元素返回,即invisible(apply(...))变成invisible(lapply(apply(...), print)).