创建带有嵌入式图形的R表 [英] Creating R tables with embedded graphics

查看:111
本文介绍了创建带有嵌入式图形的R表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够创建一个表,其中一列为图形,其他列为文本.理想情况下,我将创建一个excel电子表格,但是我很确定R到Excel程序包都不能将PDF写入单元格.我想我可以使用Knittr或Sweave一起破解某些东西,尽管我不知道具体如何.有什么建议吗?

I'd like to be able to create a table with one of the columns being graphical, others text. Ideally, I'd create an excel spreadsheet, but I'm pretty sure that none of the R to Excel packages can write PDFs into cells. I think I can hack something together using Knittr or Sweave, though I don't know how, exactly. Any advice?

推荐答案

我尝试重现您的示例:

因此,我研究了R数据集,并使用了棒球数据集,尽管我对棒球运动员而言,g-r看起来真的很愚蠢……

So I've looked into the R dataset, and used a baseball dataset, though I have the nagging doubt that for baseball players, the g-r might look really stupid...

无论如何,我提供了两个使用get_bar_df函数的示例,并尝试尽可能多地注释我的脚本以帮助您使用它.

Anyway I produced two examples of the use of the get_bar_df function, and tried to comment my script as much as possible to help you use it.

该脚本是您需要使用knitr或sweave运行的独立sweave脚本

The script is a standalone sweave script that you need to run using knitr or sweave

\documentclass{article}
\usepackage[table,dvipsnames]{xcolor}% http://ctan.org/pkg/xcolor
\usepackage[nomessages]{fp}% http://ctan.org/pkg/
\newlength{\maxlen}
\newcommand{\databarright}[2][gray!25]
{%
    \settowidth{\maxlen}{\maxnum}%
    \addtolength{\maxlen}{\dimexpr2\tabcolsep-\arrayrulewidth}%
    \FPeval\result{round(#2/\maxnum:4)}%
    \rlap{\color{#1}\hspace*{\dimexpr-\tabcolsep+0.1\arrayrulewidth}\rule[-.05\ht\strutbox]{\result\maxlen}{.95\ht\strutbox}}%
    \makebox[\dimexpr\maxlen-2\tabcolsep+\arrayrulewidth][r]{\phantom{XXX}}%
}

\newcommand{\databarleft}[2][red!25]
{%
    \settowidth{\maxlen}{\maxnum}%
    \addtolength{\maxlen}{\dimexpr2\tabcolsep-\arrayrulewidth}%
    \FPeval\result{round(#2/\maxnum:4)}%
    \makebox[\dimexpr\maxlen-2\tabcolsep+\arrayrulewidth][r]{\phantom{XXX}}%
    \llap{\color{#1}\rule[-.05\ht\strutbox]{\result\maxlen}{.95\ht\strutbox}\hspace*{\dimexpr-\tabcolsep-4\arrayrulewidth}}%    
}
\begin{document}
<<load_libraries, echo = FALSE, eval = TRUE, results ="hide">>=
library(knitr) 
library(xtable)
@
<<get_bar_df, echo = FALSE, eval = TRUE, results ="hide">>=
#' @title get_databar creates labels for xtable in a dataframe
#' @description It will create two new columns and fill them with values for xtable, it will use the last column
#' @param data the dataframe
#' @param colorpos one color for positive values that is interpretable by the xcolor dvips
#' one of 68 standard colors known to dvips  \link{https://en.wikibooks.org/wiki/LaTeX/Colors}, default "grey"
#' @param colorneg one color for negative values default "red"
#' @param transparent, the percentage of transparency passed to labels default 50 use zero for no transparency
#' @param caption, caption passed to xtable. 
#' @param vline, add a vertical line at the end of the table  default FALSE
#' @return A dataframe with the last two columns edited for xtable
get_bar_df <- function(data,
    colorpos = "grey",
    colorneg="red",
    transparent=50,
    caption="",
    vline=FALSE)
{
  if (transparent!=0){
    colorpos <- paste0(colorpos,"!",transparent)
    colorneg <- paste0(colorneg,"!",transparent)
  }
  the_col <- ncol(bsb)
  idxneg <- data[,the_col] < 0
  idxpos <- data[,the_col] > 0
  data[idxneg,"\\phantom{neg}"] <- paste0("\\databarleft[",colorneg,"]{", -data[idxneg,the_col],"}")
  data[idxpos,"\\phantom{pos}"] <- paste0("\\databarright[",colorpos,"]{", data[idxpos,the_col],"}")
  maxnum <<- max(abs(data[,the_col])) # value assigned in .GlobalEnv for later use by latex
  if (!vline) {
  xdata <-xtable(data, align = rep("l",ncol(data)+1), caption = caption)  
  } else {
      xdata <-xtable(data, align = c(rep("l",ncol(data)),"|l"), caption = caption)    
  }
  return(xdata)
} 
@
<<test, echo = FALSE, eval = TRUE, results ="hide">>=
library(plyr)
library(dplyr)
bsb <- select(baseball, id, year, g, r) %>% filter(year == 1872) %>% transform(gr = g - 
            r)
bsb1 <- select(baseball, id, year, g, r) %>% filter(year == 1873) %>% transform(gr = g - 
            r)
xbsb <- get_bar_df(data = bsb, caption="example with default values")
xbsb1 <- get_bar_df(data = bsb1, 
    colorpos = "MidnightBlue",
     colorneg = "Goldenrod", 
    transparent = 60, 
    caption = "Another example with MidnightBlue, Goldenrod, transparent= 60, vline=TRUE",
    vline=TRUE)
print.xtable(xbsb, sanitize.text.function = identity, file = "bsb.tex", hline.after = NULL, include.rownames =FALSE)
print(xbsb1, sanitize.text.function = identity, file = "bsb1.tex", hline.after = NULL, include.rownames =FALSE)
@
% this must come after the chunk (maxnum is defined in the chunk)
\newcommand{\maxnum}
{%
    \Sexpr{maxnum}
}

\input{bsb.tex}
\input{bsb1.tex}  
\end{document}

一些代码来自于这篇出色的文章: https://tex.stackexchange.com/questions/81994/partially-coloring-带有直方图的单元格背景

Some of the code comes from this excellent post : https://tex.stackexchange.com/questions/81994/partially-coloring-cell-background-with-histograms

这篇关于创建带有嵌入式图形的R表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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