将迷你图添加到 table
Add sparkline graph to a table
我正在尝试将我所有的数据处理移动到 Rmarkdown,而不是 SPSS+Excel,但不知道如何创建带有附加图表的 table。
在 Excel 中,这可以通过迷你图功能来完成,或者像我一样,简单地创建一个图表并非常准确地放置它。
上面的 table 是使用 Tables 包(和 Pander)中的表格函数创建的。并粘贴到 Excel 以创建图形(和 header 用于图形)。
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 包的解决方法,尽管我怀疑该包是否可以将 table 和图形匹配在一起。
编辑 - 到目前为止我的解决方案。
HTML 完成。 pdf 到一半了。对于 doc,我认为这是不可能的(复制粘贴到 Excel)。
HTML table
首先,让 R 知道我正在渲染 html、pdf 还是 doc。 out_type 取值:"html"、"pdf" 和 "docx"。我可以在 if 语句中使用这个 object。
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.
}
也可以有两列。
我这里又是一个table有比例的,我有两个object有男女比例的
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 那么...
我希望这对某人有所帮助。
但是请考虑这个线程中提到的包。它们非常好,我的解决方案很大程度上基于它们。我只是无法准确地找到我在寻找的包裹,所以我手动完成了这个。
我认为你需要的是 formattable package. There is also an example 将迷你图与格式化表混合,但我无法根据你的需要进行调整。
---
title:
---
```{r cars}
library(dplyr)
library(formattable)
res <-
iris %>%
group_by(Species) %>%
summarise(N=n(),
SL_Mean=round(mean(Sepal.Length),3),
SL_SD=round(sd(Sepal.Length),3),
SW_Mean=round(mean(Sepal.Width),3),
SW_SD=round(sd(Sepal.Width),3))
#using formattable
formattable(res,
list(
SL_Mean=color_bar("pink", 0.5)))
```
只是在之前的答案中添加了一个迷你图示例。希望对你有帮助。
---
title:
---
```{r results="asis"}
library(dplyr)
library(formattable)
library(sparkline)
res <-
iris %>%
group_by(Species) %>%
summarise(N=n(),
SL_Mean=round(mean(Sepal.Length),3),
SL_SD=round(sd(Sepal.Length),3),
SW_Mean=round(mean(Sepal.Width),3),
SW_SD=round(sd(Sepal.Width),3)) %>%
mutate(sparkline = as.character(Species))
#using formattable
formattable(
res,
list(
SL_Mean=color_bar("pink", proportion),
"sparkline"=function(z){
sapply(
z,
function(zz){
knitr::knit(
text = sprintf(
'`r sparkline(c(%s))`',
paste0(
iris[which(iris$Species == zz),"Sepal.Length"],
collapse=","
)
),
quiet = TRUE
)
}
)
}
)
)
```
另外,我认为一个非rmarkdown
的例子可能会让一些人高兴。
library(dplyr)
library(formattable)
library(sparkline)
res <-
iris %>%
group_by(Species) %>%
summarise(N=n(),
SL_Mean=round(mean(Sepal.Length),3),
SL_SD=round(sd(Sepal.Length),3),
SW_Mean=round(mean(Sepal.Width),3),
SW_SD=round(sd(Sepal.Width),3)) %>%
mutate("sparkline" = as.character(Species))
#using formattable
ft <- formattable(
res,
list(
SL_Mean=color_bar("pink", proportion),
"sparkline"=function(z){
sapply(
z,
function(zz){
sprintf(
'<span class="sparkline-bar">%s</span>',
paste0(
iris[which(iris$Species == zz),"Sepal.Length"],
collapse=","
)
)
}
)
}
)
)
library(htmlwidgets)
browsable(
attachDependencies(
tagList(
onRender(
as.htmlwidget(ft),
"function(el,x){$('.sparkline-bar').sparkline('html',{type:'bar'});}"
)
),
htmlwidgets:::widget_dependencies("sparkline","sparkline")
)
)
还有一个名为sparkTable的包,您可以创建迷你图对象并将它们添加到您的降价对象中。这些对象就像我猜你可以在 rmarkdown 中引用的 ggplot 对象。但您也可以将对象另存为 pdf、eds 或 png。
还有一个 sparkTable 对象可以像您打印的那样打印 table。
来自论文 (p28):
‘‘‘{r, echo=TRUE}
require( sparkTable )
sl <- newSparkLine (values = rnorm (25) , lineWidth = .18, pointWidth = .4,
width = .4, height = .08)
export(sl , outputType = "png", filename = "sparkLine ")
‘‘‘
This is a sparkline included in the 
text ...
我正在尝试将我所有的数据处理移动到 Rmarkdown,而不是 SPSS+Excel,但不知道如何创建带有附加图表的 table。
在 Excel 中,这可以通过迷你图功能来完成,或者像我一样,简单地创建一个图表并非常准确地放置它。
上面的 table 是使用 Tables 包(和 Pander)中的表格函数创建的。并粘贴到 Excel 以创建图形(和 header 用于图形)。
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 包的解决方法,尽管我怀疑该包是否可以将 table 和图形匹配在一起。
编辑 - 到目前为止我的解决方案。
HTML 完成。 pdf 到一半了。对于 doc,我认为这是不可能的(复制粘贴到 Excel)。
HTML table
首先,让 R 知道我正在渲染 html、pdf 还是 doc。 out_type 取值:"html"、"pdf" 和 "docx"。我可以在 if 语句中使用这个 object。
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.
}
也可以有两列。
我这里又是一个table有比例的,我有两个object有男女比例的
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 那么...
我希望这对某人有所帮助。 但是请考虑这个线程中提到的包。它们非常好,我的解决方案很大程度上基于它们。我只是无法准确地找到我在寻找的包裹,所以我手动完成了这个。
我认为你需要的是 formattable package. There is also an example 将迷你图与格式化表混合,但我无法根据你的需要进行调整。
---
title:
---
```{r cars}
library(dplyr)
library(formattable)
res <-
iris %>%
group_by(Species) %>%
summarise(N=n(),
SL_Mean=round(mean(Sepal.Length),3),
SL_SD=round(sd(Sepal.Length),3),
SW_Mean=round(mean(Sepal.Width),3),
SW_SD=round(sd(Sepal.Width),3))
#using formattable
formattable(res,
list(
SL_Mean=color_bar("pink", 0.5)))
```
只是在之前的答案中添加了一个迷你图示例。希望对你有帮助。
---
title:
---
```{r results="asis"}
library(dplyr)
library(formattable)
library(sparkline)
res <-
iris %>%
group_by(Species) %>%
summarise(N=n(),
SL_Mean=round(mean(Sepal.Length),3),
SL_SD=round(sd(Sepal.Length),3),
SW_Mean=round(mean(Sepal.Width),3),
SW_SD=round(sd(Sepal.Width),3)) %>%
mutate(sparkline = as.character(Species))
#using formattable
formattable(
res,
list(
SL_Mean=color_bar("pink", proportion),
"sparkline"=function(z){
sapply(
z,
function(zz){
knitr::knit(
text = sprintf(
'`r sparkline(c(%s))`',
paste0(
iris[which(iris$Species == zz),"Sepal.Length"],
collapse=","
)
),
quiet = TRUE
)
}
)
}
)
)
```
另外,我认为一个非rmarkdown
的例子可能会让一些人高兴。
library(dplyr)
library(formattable)
library(sparkline)
res <-
iris %>%
group_by(Species) %>%
summarise(N=n(),
SL_Mean=round(mean(Sepal.Length),3),
SL_SD=round(sd(Sepal.Length),3),
SW_Mean=round(mean(Sepal.Width),3),
SW_SD=round(sd(Sepal.Width),3)) %>%
mutate("sparkline" = as.character(Species))
#using formattable
ft <- formattable(
res,
list(
SL_Mean=color_bar("pink", proportion),
"sparkline"=function(z){
sapply(
z,
function(zz){
sprintf(
'<span class="sparkline-bar">%s</span>',
paste0(
iris[which(iris$Species == zz),"Sepal.Length"],
collapse=","
)
)
}
)
}
)
)
library(htmlwidgets)
browsable(
attachDependencies(
tagList(
onRender(
as.htmlwidget(ft),
"function(el,x){$('.sparkline-bar').sparkline('html',{type:'bar'});}"
)
),
htmlwidgets:::widget_dependencies("sparkline","sparkline")
)
)
还有一个名为sparkTable的包,您可以创建迷你图对象并将它们添加到您的降价对象中。这些对象就像我猜你可以在 rmarkdown 中引用的 ggplot 对象。但您也可以将对象另存为 pdf、eds 或 png。
还有一个 sparkTable 对象可以像您打印的那样打印 table。
来自论文 (p28):
‘‘‘{r, echo=TRUE}
require( sparkTable )
sl <- newSparkLine (values = rnorm (25) , lineWidth = .18, pointWidth = .4,
width = .4, height = .08)
export(sl , outputType = "png", filename = "sparkLine ")
‘‘‘
This is a sparkline included in the 
text ...