2015-09-29 10 views
10

私はすべてのデータをSPSS + ExcelではなくRmarkdownに移動しようとしていますが、グラフを追加してテーブルを作成する方法を理解することはできません。 Excelでスパークライングラフをテーブルに追加

Ideal table

これは、スパークラインで行うことができ、単純にグラフを作成し、非常に正確にそれを置く、私はそれがそうであるように、機能または。

上記の表は、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テーブル
まず、私はHTML、PDFまたはドキュメントをレンダリングしていた場合にRが知っているので。 out_typeは、 "html"、 "pdf"、 "docx"の値をとります。 if文でこのオブジェクトを使用できます。

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

は今の棒:2つの列を有することも可能

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

Looks like this

です。再びここで

Another possibility

私はプロポーションを持つテーブルを持って、私は男性と女性の比率を持つ2つのオブジェクトを持っています。

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} 

PDF bars

それはのように良い見ていません。私はラテックスにとってとても新しいです。私はまだバーの後ろに数字を配置する方法を見つける必要があります。そして、これを私の関数のif文に入れる方法。 HTMLなら:if pdf then ...

私はこれが誰かを助けてくれることを願っています。 しかし、このスレッドに記載されているパッケージを検討してください。彼らは優れている、私のソリューションは非常にそれらに基づいています。私はちょうど私がパッケージで探していたものを正確に得ることができなかったので、これを手動で行いました。

+2

参照は非常にクールなパッケージ/ウィジェットです – zx8754

+0

をhttps://github.com/htmlwidgets/sparkline。テーブルを作成せずに、rmarkdownのテーブルにどのように追加するのですか?テキストのように、ページに表示されます。例えば これが機能しない: 'ddplyパンダー( (虹彩、 "種"、要約、 N =長さ(Sepal.Length)、 平均=平均(Sepal.Length、na.rm = T)、 Sd = sd(Sepal.Length、na.rm = T)、 spark = sparkline(Mean)) ) ' –

答えて

6

あなたが必要と思うのはformattable packageです。書式設定可能なsparklinesを混合するexampleもありますが、私はあなたのニーズに適応できませんでした。ただ、前の回答にスパークラインの例を追加

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


``` 

enter image description here

+2

あなたはformattableに関する追加ヘルプが必要かどうかお知らせください。 – timelyportfolio

+0

@timelyportfolioスパークラインを使った回答例を追加できますか? 3つのスパークラインは、「Sepal.Length」の「Species group」ごとに折れ線グラフを表示する必要があります。 – zx8754

7

。それが役に立てば幸い。

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


``` 

また、非の例では、いくつかの人々を幸せにするかもしれないと思った。

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

_formattable_を_tables_パッケージの表関数とともに使用することはできますか?私は大きなネストしたテーブルを作ろうとしています。私。私が上に提供する小さな例です。私はそのテーブルに書式設定可能なバーを追加できますか? –

+0

今すぐこのコメントが表示されます。まだ興味をお持ちですか?私はRで見つけることができるフォーマット可能なテーブルパッケージとほぼすべてのテーブルパッケージを使って実験しました。 – timelyportfolio

+0

問題はありません、私に戻ってくれてありがとう。いいえ、実際はテーブルパッケージを使用していないので、これを整理しました。あなたの助けに感謝します。私の解決策はあなたのパッケージに基づいていました。 –

1

あなたがスパークラインオブジェクトを作成し、あなたの値下げ対象に追加することができsparkTableというパッケージもあります。オブジェクトは、rmarkdownで参照できると推測しているggplotオブジェクトのようなものです。しかし、オブジェクトをpdf、eds、またはpngとして保存することもできます。

また、あなたが印刷したような表を印刷するsparkTableオブジェクトもあります。

紙(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 ![ firstSparkLine ](sparkLine.png) 
text ... 
+2

最近の[Rジャーナル](https://journal.r-project.org/archive/2015-1/templ-kowarik-meindl.pdf)の彼らの論文(p28)は、ニットに関するいくつかの詳細に入ります: –

+0

ありがとう、それはクールなパッケージです。私はそれを試して、私がそれで探しているものを得ることができるかどうかを見てみましょう。 –

関連する問題