You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Rmarkdown中让LaTeX表格与图表同行并对齐

解决Rmarkdown中LaTeX表格与ggplot图表同行时的垂直对齐问题

针对你用floatrow实现表格与图表同行但对齐效果差的问题,提供两种直接有效的解决思路:

方法1:设置表格垂直居中对齐

通过floatrow的配置让表格默认垂直居中,无需调整表格高度。只需在YAML的header-includes中添加表格对齐设置:

\floatsetup[table]{valign=c}

这条代码会让所有表格在floatrow环境中自动垂直居中,和图表保持对齐。

方法2:拉伸表格高度匹配图表

如果希望表格高度和图表完全一致,可以通过调整表格行高实现,用kableExtra的row_spec设置每行高度:

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"
    ) %>%
  kableExtra::row_spec(0:nrow(SPI), extra_css = "height: 1.8em;") # 1.8em可按需调整,直到匹配图表高度

修改后的完整代码示例

---
title: Domestic and Global Price Trends – Key Commodities
output: pdf_document
header-includes:
  \usepackage{floatrow}
  \floatsetup[table]{valign=c} # 添加表格垂直居中配置
  \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)
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))
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)
p<-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))
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"
    ) %>%
  kableExtra::row_spec(0:nrow(SPI), extra_css = "height: 1.8em;") # 可选:调整行高匹配图表

\newfloatcommand{btabbox}{table}

\begin{figure}[H]
\begin{floatrow}
\btabbox{%

x
}{\caption{Selected Commodities Showing Significant Change in Weekly Prices (percentage)}}
    \ffigbox{%
p
}{}

\end{floatrow}
\end{figure}

> 注:代码中把原来的`a`变量名改成了`p`,避免和之前计算的`a`变量冲突,防止报错。

内容的提问来源于stack exchange,提问作者Ali Inayat
相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 10:34:56