我正在尝试将我所有的数据处理移动到 Rmarkdown,而不是 SPSS+Excel,但无法弄清楚如何创建带有附加图的表。
在 Excel 中,这可以通过 Sparklines 功能来完成,或者像我一样,简单地创建一个图表并非常准确地放置它。
上面的表格是使用 Tables 包(和 Pander)中的表格函数创建的。并粘贴到 Excel 中以创建图表(以及图表的标题)。
library(tables)
library(pander)
pander( #convert table to rmarkdown table
tabular(
(Species + 1) ~ (n=1) + Format(digits=2) *
(Sepal.Length + Sepal.Width) * (mean + sd),
data = iris),
caption = "Table 1. Iris") #Heading for table
有没有人创造过这样的东西?也许是 gridExtra 包的解决方法,尽管我怀疑该包是否可以将表格和图表匹配在一起。
编辑 - 到目前为止我的解决方案。
HTML 已完成。pdf的一半。对于doc,我认为这是不可能的(复制粘贴到Excel)。
HTML 表
首先,R 知道我是在渲染 html、pdf 还是 doc。out_type 取值:“html”、“pdf”和“docx”。我可以在 if 语句中使用这个对象。
out_type <- knitr::opts_knit$get("rmarkdown.pandoc.to")
现在的酒吧:
if(out_type == "html"){ #if output is html then:
my_table$Mean_Sepal_Length <- paste0( #I have a table saved as a dataframe. I add a column to it called Mean_Sepal_Length
"<span style=' width: 100%; display: flex;'><span style='display: inline-block; border-radius: 3px; padding-right: 0; background-color: #00457C; width:", #create the bar
MEAN_SEPAL_L_OBJECT, #set the with of the bar. I have an object with these proportions
"%; margin-right: 5px;'> </span><span>", MEAN_SEPAL_L_VARIABLE, "%</span></span>") #finally add the value behind the bar.
}
也可以有两列。
在这里,我再次有一个比例表,我有两个具有男性和女性比例的对象。
my_table$male_female <- paste0(
"<span style=' width: 100%; display: flex;'><span>", MALE_BAR, #the proportion of males
"%</span><span style='display: inline-block;border-top-left-radius: 3px; border-bottom-left-radius:3px; padding: 0; background-color: #00457C; width:", MALE_BAR, #width of the bar for males
"%; margin-left: 5px;'></span><span style='display: inline-block; border-top-right-radius: 3px; border-bottom-right-radius:3px; padding:0; background-color: #AC1A2F; width:",
FEMALE_BAR, #width of bar for females
"%;margin-right: 5px;'></span><span>", FEMALE_BAR, #proportion of females
"%</span></span>")
当然,如果您愿意,您也可以将数字放在条形内,但是当条形很小时,这是一个问题。
PDF
我对 pdf 的了解还不够远。到目前为止,这是我的解决方案:
\begin{tabular}{>{$\rhd$ }lrl}
Loop at line 151 in divergence & \mybar{3.420}\\
Loop at line 1071 in radiation & \mybar{3.270}\\
scalar face value & \mybar{3.090}\\
Loop at line 102 in get & \mybar{1.700}\\
get sensible enthalpy & \mybar{1.250}\\
\end{tabular}
它看起来不太好。我对乳胶很陌生。我仍然需要找出如何将数字放在栏后面。以及如何将其放入我的函数中的 if 语句中。如果是 HTML 则:如果是 pdf 则...
我希望这可以帮助别人。但是,请考虑这个线程中提到的包。它们非常好,我的解决方案非常基于它们。我只是无法准确地得到我正在寻找的包,所以我手动完成了这个。