我正在 Rmarkdown 中编译一份报告,其中有一个图表和一个表格需要位于同一行。目前,我无法将它们放在同一行,但图表位于表格上方。我提供我的 YAML 以及图表和表格中的代码。表输出是乳胶形式,这也产生了问题。我正在考虑将我的表格转换为
.png
并附加图表,但应该有一种方法可以让乳胶表格和图表在 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}
根据@peter的以下评论,我已经能够在同一行中获取图形和表格,但对齐方式看起来仍然有点偏离。我已经调整了上面的代码,我可以将它们并排显示,但看起来表格比图小。我希望我的表格垂直扩展以匹配图表的高度或至少与图表中心对齐。
一种方法是对混合对象行使用
\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}
结果是: