R语言 如何让我的表格和图形在同一行

qmelpv7a  于 2023-11-14  发布在  其他
关注(0)|答案(1)|浏览(104)

我正在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)
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)
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))
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{%

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

\end{floatrow}
\end{figure}

字符串
根据@peter下面的评论,我已经能够在同一行中获取图形和表格,但对齐仍然看起来有点不对。我已经调整了上面的代码,我能够并排获取它们,但看起来表格比图形小。我希望我的表格垂直扩展以匹配图形的高度,或者至少与图形中心对齐。x1c 0d1x
n53p2ov0

n53p2ov01#

一种方法是在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{%
a
}{}

\end{floatrow}
\end{figure}

字符串
结果是:

![](https://i.stack.imgur.com/Kc4sA.png)
的数据

相关问题