我试图将所有数据处理转移到 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 已经完成一半了。对于文档,我认为不可能(复制粘贴到 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,则...
我希望这可以帮助别人。
但是请考虑此线程中提到的软件包。他们非常出色,我的解决方案很大程度上基于他们。我只是无法准确地获得我想要的包,所以我手动完成了此操作。