Rmarkdown中同一行上的Kableextra表和ggplot图(PDF-不是Flexdashboard) [英] A Kableextra table and a ggplot plot on same row in Rmarkdown (PDF - not Flexdashboard)

查看:53
本文介绍了Rmarkdown中同一行上的Kableextra表和ggplot图(PDF-不是Flexdashboard)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用R Markdown创建一些PDF报告.我在正确设置布局方面遇到困难.基本上,我需要在同一行上创建一个KableExtra表(数据框)和一个ggplot图.我已经研究了一些网格程序包,但无法使其正常工作.

I have been experimenting with R Markdown to create some PDF reports. I am having difficulty in getting the layout right. Basically, I need to have a KableExtra created table (dataframe) and a ggplot plot on the same row. I have explored some grid packages, but couldn't get it to work.

这是我的代码:

---
title: "Untitled"
author: ""
date: "14 June 2018"
output: pdf_document

---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(knitr)
library(reshape2)
library(dplyr)
library(kableExtra)
```

## R Markdown

```{r chart, echo=FALSE}
Years <-  c("2016","2016","2016","2016",
           "2017","2017","2017","2017")
Quarters <-  c("Q1","Q2","Q3","Q4",
        "Q1","Q2","Q3","Q4")
Series1 <- c("100","200","300","400","500","600","700","800")
Series1 <- as.numeric(Series1)

df <- data.frame(Years,Quarters, Series1)

library(ggplot2)
ggplot(df) +
        geom_point(aes(x = Quarters, y = Series1)) + 
        facet_wrap( ~ Years, strip.position = "bottom",scales = "free_x") + 
        theme(panel.spacing = unit(0,"lines"), strip.background = 
element_blank(),
              strip.placement = "outside")
```
```{r table, echo=FALSE}

Table <- dcast(df, Years ~ Quarters, fun.aggregate = sum, value.var = 
"Series1")


Table <- Table %>%
        kable(format = "latex", caption = "Balances", booktabs = TRUE) %>%
        kable_styling(latex_options = c("striped","hold_position","condensed"),
                      font_size = 10) 
Table
```

推荐答案

如果您不太依赖 kable(),我可以提供此 gridExtra 解决方案.当使用 tableGrob(kable(.))时,乳胶代码将无法执行,也许其他人想出了如何在 tableGrob()中执行乳胶代码.

If you are not strongly depending on kable() I can provide this gridExtra solution. When using tableGrob(kable(.)) the latex code won't be executed somehow, maybe somebody else comes up with how to execute latex code within a tableGrob().

```{r chart, echo=FALSE, message=FALSE}
df <- data.frame(Years=rep(2016:2017, each=4),
                 Quarters=rep(paste0("Q", 1:4), 2),
                 Series1=seq(100, 800, 100))

library(ggplot2)
p1 <- ggplot(df) +
  geom_point(aes(x=Quarters, y=Series1)) + 
  facet_wrap( ~ Years, strip.position="bottom", scales="free_x") + 
  theme(panel.spacing=unit(0, "lines"), 
        strip.background=element_blank(), 
        strip.placement="outside", 
        aspect.ratio=1)  # set aspect ratio

Table <- dcast(df, Years ~ Quarters, fun.aggregate=sum, value.var="Series1")

library(gridExtra)
t1 <- tableGrob(Table, theme=ttheme_minimal(), rows=NULL)  # transform into a tableGrob

grid.arrange(p1, t1, nrow=1)
```

产品:

Produces:

这篇关于Rmarkdown中同一行上的Kableextra表和ggplot图(PDF-不是Flexdashboard)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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