将迷你图添加到表中 [英] Add sparkline graph to a table

查看:196
本文介绍了将迷你图添加到表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将所有数据处理为Rmarkdown而不是SPSS + Excel,但无法弄清楚如何使用附加图创建表。





在Excel中,这可以通过Sparklines功能完成,或者像我一样,只需创建图形并将其放置非常准确。



上面的表格是使用Tables包(和Pander)中的表格函数创建的。

  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 =表1.虹膜)#Heading for table

有没有人创建过类似的东西?也许解决方案与gridExtra包,虽然我怀疑包可以匹配表和图形。




编辑 - 到目前为止我的解决方案。

HTML完成。与pdf的一半。对于doc,我认为这是不可能的(复制粘贴到Excel)。
$ b

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有一个表格保存为一个数据框,我给它添加一个名为Mean_Sepal_Length的列
< span style ='width:100%; display:flex;'>< span style ='display:inline-block; border-radius:3px; padding-right:0; background-color:#00457C ; width:,#创建栏
MEAN_SEPAL_L_OBJECT,#设置栏的位置,我有一个具有这些比例的对象
%; margin-right:5px;'>& nbsp; < / span>< span>,MEAN_SEPAL_L_VARIABLE,%< / span>< / span>)#最后添加栏后面的值。
}



也可以有两列。



在这里,我还有一个比例表,我有两个对象,其比例为男性和女性。

  my_table $ male_female<  -  paste0(
< span style ='width:100%; display:flex;'>< span>,MALE_BAR,#男性比例
%< / span>< span style ='display:inline-block; border-top-left-radius:3px; border-bottom-left-radius:3px; padding:0; background-color: #00457C; width:,MALE_BAR,#男性栏的宽度
%; margin-left:5px;'>< / span>< span style ='display y:inline-block; border-right-right-radius:3px;边框右下角半径:3px的;填充:0; background-color:#AC1A2F;宽度:,
FEMALE_BAR,#女性栏的宽度
%; margin-right:5px;'>< / span>< span>,FEMALE_BAR,#女性比例
%< / span>< / span>)

当然,您可以如果您愿意的话,也可以在酒吧里面输入数字,但是当酒吧很小的时候就会出现问题。

PDF

到目前为止,我还没有收到太多的信息,下面是我的解决方案:

  \begin {tabular} {< $ \rhd $} lrl} 
在分支& \mybar的第151行循环{3.420} \\
在辐射& \mybar { 3.270} \\
标量面值& \mybar {3.090} \\
在get& \mybar {1.700}中的第102行循环\\
得到明显的焓& \mybar {1.250} \\
\ end {表格}



<这看起来不太好。我对乳胶很陌生。我仍然需要找出如何将数字放在条形图后面。以及如何将它放入我的函数中的if语句中。如果HTML然后:如果PDF然后...



我希望这可以帮助某人。
但是,请考虑这个线程中提到的包。他们非常出色,我的解决方案非常基于他们。我只是无法得到我正在寻找的包,所以我手动做了这个。

解决方案

我认为你需要的是


I am trying to move all my data crunching to Rmarkdown, instead of SPSS+Excel, but can't figure out how to create a table with an appending graph.

In Excel this can be done with Sparklines feature or, as I do it, simply creating a graph and placing it very accurately.

The table above is created with tabular function from Tables package (and Pander). And pasted into Excel to create the graph (and header for graph).

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

Has anyone created something like this? Maybe a workaround with the gridExtra package, although I am sceptical that the package can match together table and graph.


Edit - My solution so far.
The HTML is finished. Half way there with the pdf. For doc, I don't think it is possible (copy paste to Excel).

HTML table
First, so R knows if I am rendering a html, pdf or doc. out_type takes the values: "html", "pdf" and "docx". I can use this object in if statements.

out_type <- knitr::opts_knit$get("rmarkdown.pandoc.to")

Now the bars:

    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;'>&nbsp;</span><span>", MEAN_SEPAL_L_VARIABLE, "%</span></span>") #finally add the value behind the bar.
    }

It is also possible to have two columns.

Here again I have a table with proportions, I have two objects that have the proportions of males and females.

    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>")

Of course you can also have the numbers inside the bars if you wish, but it is a problem when the bars are small.

PDF
I haven't gotten as far with the pdf. Here is my solution so far:

\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}

It doesn't look as good. I am very new to Latex. I still need to find out how to place the numbers behind the bars. And how to put this into an if statement in my function. If HTML then: if pdf then...

I hope this helps someone. But please, consider the packages mentioned in this thread. They are excellent, my solution is very much based on them. I just could not get exactly what I was looking for with the packages so I did this manually.

解决方案

I think what you need is formattable package. There is also an example of mixing sparklines with formattable, but I couldn't adapt it to your needs.

---
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)))


```

这篇关于将迷你图添加到表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆