Hel*_*son 11 r graph sparklines knitr r-markdown
我试图将我的所有数据运算转移到Rmarkdown而不是SPSS + Excel,但无法弄清楚如何创建带有附加图的表.
在Excel中,这可以使用迷你图功能完成,或者像我一样,只需创建一个图表并非常准确地放置它.
上表是使用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
Run Code Online (Sandbox Code Playgroud)
有没有人创造过这样的东西?也许使用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")
Run Code Online (Sandbox Code Playgroud)
现在吧:
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.
}
Run Code Online (Sandbox Code Playgroud)
也可以有两列.
在这里,我有一个比例表,我有两个对象,男性和女性的比例.
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>")
Run Code Online (Sandbox Code Playgroud)
当然,如果你愿意的话,你也可以把数字放在栏内,但是当栏很小时你就会有问题.
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}
Run Code Online (Sandbox Code Playgroud)
它看起来不太好.我是Latex的新手.我仍然需要找出如何将数字放在条形后面.以及如何将它放入我的函数中的if语句中.如果HTML然后:if pdf then ...
我希望这可以帮助别人.但是请考虑一下这个帖子中提到的包.他们很优秀,我的解决方案非常基于他们.我只是无法得到我正在寻找的包,所以我手动完成.
只需在上一个答案中添加一个迷你示例.希望能帮助到你.
---
title: http://stackoverflow.com/q/32841221/680068
---
```{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
)
}
)
}
)
)
```
Run Code Online (Sandbox Code Playgroud)
此外,我认为一个非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")
)
)
Run Code Online (Sandbox Code Playgroud)
我认为你需要的是可格式化的包.还有一个将迷你图与格式表混合的例子,但我无法根据您的需要进行调整.
---
title: http://stackoverflow.com/q/32841221/680068
---
```{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)))
```
Run Code Online (Sandbox Code Playgroud)