我正在用Rmarkdown编写一份报告,其中有一个图表和一个表格需要放在同一行.目前,我不能将它们放在同一行,但图表在表格上方.我提供了我的YAML和图形和表格的代码.表的输出是LaTeX形式的,这也造成了问题.我正在考虑将我的表格转换为.png,并附加到图表中,但应该有一种方法让latex 表和图表在Rmarkdown中对齐(编辑:请阅读末尾的段落):

---
title: Domestic and Global Price Trends – Key Commodities
output: pdf_document
header-includes:
  \usepackage{floatrow}
  \usepackage{fancyhdr}
  \pagestyle{fancy}
  \fancyhf{}
  \addtolength{\headheight}{1.5cm}
  \rhead{Research Department}
  \lhead{x} 
  \renewcommand{\headrulewidth}{1pt}
  \fancypagestyle{plain}{\pagestyle{fancy}} 
---


```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(readxl)
library(dplyr)
library(kableExtra)
library(ggplot2)
library(tidyverse)
library(cowplot)
options(scipen = 10)

```

```{r include=FALSE} 
ref=structure(list(date = structure(c(19236, 19243, 19250, 19257, 
19264, 19271, 19278, 19285, 19292, 19299, 19306, 19313, 19320, 
19327, 19334, 19341, 19348, 19355, 19362, 19369, 19376, 19383, 
19390, 19397, 19404, 19411, 19418, 19425, 19432, 19439, 19446, 
19453, 19460, 19467, 19474, 19481, 19488, 19495, 19502, 19509, 
19516, 19523, 19530, 19537, 19544, 19551, 19558, 19565, 19572, 
19579, 19586, 19593, 19600, 19607, 19614, 19621, 19628, 19635, 
19642, 19649, 19656), class = "Date"), SPI_WoW = c(1.31, -0.583352030513795, 
-0.19, -8.10798589129058, 0.944835391959042, 0.29, -0.573567296942579, 
0.35, 4.13, 0.533358285767749, 0.744601638123599, 0.62, 0.482049398586004, 
-0.16, 0, -0.402709134175372, -0.11, -0.09, 1.09, 0.44, 0.0045345304493738, 
0.453432483903149, 2.82116096415996, 0.17, 2.89258009378972, 
2.77718618222089, -0.29839612085043, 1.37, 0.963628162545627, 
1.8, -0.36, 0.92, -0.6, 0.506864623243941, 0.15, 1.05, 0.27, 
-0.16, -0.42, 0.03, 0.21, 0.2, 0.33, 0.33, 0.7, 0.33, -0.07, 
3.73, 1.3, 0.69, 0.78, 0.05, 0.54, 0.96, -0.25, 0.93, -0.02, 
0.11, 0.3, -1.70402319168493, -0.33), SPI_YoY = c(45.5, 42.6961226330027, 
40.58, 29.2766715439914, 30.6227712684666, 29.44, 28.4377747080246, 
27.13, 30.68, 30.6023217650276, 29.2417910447761, 28.67, 30.1558039961941, 
30.56, 30.66, 29.4243577545195, 28.76, 29.3, 30.3, 31.75, 31.8309522386275, 
32.5713601819161, 34.4925311448308, 34.83, 38.4175461352515, 
41.5439666803543, 41.071952149182, 42.27, 45.6406009700698, 46.65, 
45.36, 44.49, 44.61, 47.2259573224203, 46.82, 48.38, 48.02, 45.72, 
45.49, 42.67, 39.26, 34.96, 34.05, 34.05, 28.55, 28.96, 29.16, 
29.21, 29.83, 30.82, 27.57, 25.34, 24.39, 26.32, 26.25, 38.66, 
37.33, 37.07, 38.28, 35.4508695863984, 29.65)), row.names = c(NA, 
-61L), class = c("tbl_df", "tbl", "data.frame"))
SPI=structure(list(A = c("Tomatoes", "Pulse Masoor", "Sugar", "Diesel", 
"LPG"), B = c(17, 10.87, 6.73, 6.28, 5.19), C = c("Chicken", 
"Cooking oil", "Veg. Ghee (2.5kg)", "Tea", "Veg. Ghee (1kg)"), 
    D = c(-3.2, -1.03, -0.47, -0.43, -0.14)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -5L))

``` 

```{r plot1, echo=F ,out.width="50%",out.height="50%",fig.align='center' ,warning=F, message=F}
df<-tail(ref,n=52)
d=max(df$SPI_WoW)
c=min(df$SPI_WoW)
b=max(df$SPI_YoY)
a=min(df$SPI_YoY)
m=(d-c)/(b-a)
k=(c*b-a*d)/(b-a)
a=df%>% ggplot(aes(x=as.Date(date)))+geom_line(aes(y=SPI_WoW, color="SPI_WoW"),linewidth=1)+geom_line(aes(y=(m*SPI_YoY+k), color="SPI_YoY"),linewidth=1)+scale_y_continuous(name = "SPI_WoW",sec.axis = sec_axis(trans = ~ (.-k)/m,name = "SPI_YoY"))+scale_x_date(name='Date',breaks = as.Date(ref$date[seq(length(ref$date)-54,length(ref$date),2)]), date_labels = "%d %b %y")+ggtitle("Weekly Price Changes (SPI-Domestic)")+scale_color_manual(name="",values = c("SPI_WoW"='darkblue',"SPI_YoY"='red'))+theme_gray()+theme(legend.position=c(0.85,0.8),panel.grid = element_blank(),legend.title = element_blank(),legend.background = element_rect(fill = "lightgrey"),panel.border = element_rect(fill = NA),axis.text.x = element_text(angle = 90))


``` 

```{r table1, echo=F }
x=SPI %>% 
  knitr::kable(
    format = "latex",
    align = "l",
    booktabs = TRUE,
    longtable = TRUE,
    linesep = "",
    col.names = NULL) %>%
  kableExtra::kable_styling(
      position = "center",
      latex_options = c("striped", "repeat_header"),
      stripe_color = "gray!15"
    )

```

\newfloatcommand{btabbox}{table}

\begin{figure}[H]
  \begin{floatrow}
    \btabbox{%
```{r, fig.align="right", echo = F}
x
```
    }{\caption{Selected Commodities Showing Significant Change in Weekly Prices (percentage)}}
        \ffigbox{%
```{r, fig.align = "right", echo = F}
a
```
    }{}
  \end{floatrow}
\end{figure}


Following the comment below from @peter I have been able to make get graph and table in the same row but the alignment still looks a bit off. I have adjusted above code where I was able to get them side by side but it looks like table is smaller than the figure. I want my table to expand vertically to match the height of the graph or at least center align with the graph. I want the table to move a but upwards either by stretching or by shifting so that it aligns neatly with graph

推荐答案

一种方法是在Figure环境中使用\TopFloatBoxes命令.flowrow包文档提供了更多详细信息,并允许进行更多配置.

\newfloatcommand{btabbox}{table}


\begin{figure}\TopFloatBoxes
  \begin{floatrow}
    \btabbox{%
```{r, fig.align="right", echo = F}
x
```
    }{\caption{Selected Commodities Showing Significant Change in Weekly Prices (percentage)}}
        \ffigbox{%
```{r, fig.align = "right", echo = F}
a
```
    }{}
  \end{floatrow}
\end{figure}

这将导致:

enter image description here

R相关问答推荐

使用log 10转换绘制geom_smooth

Select 与特定列中最大值对应的数据帧行

手工PCA上的载体与输出双图不匹配

高质量地将R格式的图表从Word中输出

将Multilinetring合并到一个线串中,使用sf生成规则间隔的点

如何将在HW上运行的R中的消息(错误、警告等)作为批处理任务输出

从R导出全局环境中的所有sf(numrames)对象

在"gt"表中添加第二个"groupname_col",而不连接列值

IMF IFS数据以R表示

如何计算多个日期是否在一个日期范围内

汇总数据表中两个特定列条目的值

使用整齐的计算(curl -curl )和杂音

如何对2个列表元素的所有组合进行操作?

R Select()可以测试不存在的子集列

给定开始日期和月份(数字),如何根据R中的开始日期和月数创建日期列

如何根据数据帧中的值从该数据帧中提取值?

当我添加美学时,geom_point未对齐

在R中,如何从一系列具有索引名的变量快速创建数据帧?

是否有可能从边界中找到一个点值?

如何在条形图中的x和填充变量中包含多个响应变量?