问题
I have a simple data structure: cases are countries and for each country I have a couple of numeric variables. Like so:
dat <- data.frame(country = c("Belgium", "Germany", "Holland", "Ireland"), Var1 = 1:4, Var2 = 11:14)
print(dat, row.names = FALSE)
country Var1 Var2
Belgium 1 11
Germany 2 12
Holland 3 13
Ireland 4 14
The table needs to be formatted still, with headings in bold, and rows colored in grey or white, alternatingly.
Now, what I want is to add two additional columns, in between "country" and "Var1". The first new column is called "flag" and should contain the country's flag. The second new column is called "flagged" and contains an image of a red flag is the country scores very bad on a particular human rights issue, an orange flag if it scores mediocre and nothing elsewise.
How can I create an object that prints that way in R? How to combine images with data in such a layout?
(eventually this will part of a larger document created with knitr
)
回答1:
If you are using knitr with the rmarkdown package, it is pretty easy -- just use the Markdown syntax ![]()
to include images, e.g.
---
title: "Flags"
author: "Yihui Xie"
date: "2014/08/03"
output: html_document
---
```{r results='asis'}
dat <- data.frame(
country = c('Canada', 'United Kindom'),
abbr = c('ca', 'gb'),
var1 = c(1, 2),
var2 = rnorm(2)
)
dat$flag <- sprintf('', dat$abbr)
library(knitr)
kable(dat)
```

If you need LaTeX/PDF output, you have to download these images by yourself. Here is an example:
---
title: "Flags"
author: "Yihui Xie"
date: "2014/08/03"
output: html_document
---
```{r}
dat <- data.frame(
country = c('Canada', 'United Kindom'),
abbr = c('ca', 'gb'),
var1 = c(1, 2),
var2 = rnorm(2)
)
dat$file <- paste0(dat$abbr, '.png')
dat$link <- paste0('http://flagpedia.net/data/flags/mini/', dat$file)
dat$flag <- sprintf('', dat$file)
for (i in seq_len(nrow(dat))) {
if (!file.exists(dat$file[i])) xfun::download_file(dat$link[i])
}
knitr::kable(dat[, -c(5, 6)])
```
回答2:
With this experimental fork of gtable, you can do,
require(gtable)
dat <- data.frame(country = c("Belgium", "Germany", "Holland", "Ireland"), Var1 = 1:4, Var2 = 11:14)
g <- gtable_table(dat)
library(png)
# pirate-land flag for illustration
img <- readPNG(system.file("img", "Rlogo.png", package="png"), native = FALSE)
imgRgb <- rgb(img[,,1],img[,,2],img[,,3])
dim(imgRgb) <- dim(img)[1:2]
flags <- replicate(nrow(g), rasterGrob(imgRgb), simplify = FALSE)
g <- gtable_add_cols(g, unit(1,"cm"), 0)
g <- gtable_add_grob(g, flags, t = seq_len(nrow(g)), l=1, r=1, z=1)
grid.newpage()
grid.draw(g)
formatting options described here

回答3:
The question was asked with pdf as the output, here is an answer using knitr, and something more appropriate than just \includegraphics
The trick is to use the adjustbox package in latex, with the following arguments:
- the
height
(later used as the argument to the R functionget_picture_code
) is the height of the picture. - the
valign
argument (which default here to valign=m), will perform vertical adjustment according to the text, - the
margin
here defined as1ex
surrounding the picture allows to separate the flags.
So we just use this function
get_picture_code <- function(path,height,col=NULL)
{
paste0("\\adjustimage{height=",height,",valign=m,margin*=1ex}{",path,"}")
}
To get a vector of images added to the table.
Finally we use xtable with argument sanitize.text.function = identity
to print the tex code :
\documentclass{article}
\usepackage{adjustbox}
\begin{document}
<<load_libraries, echo = FALSE, eval = TRUE, results ="hide">>=
library(knitr)
library(xtable)
@
<<include_images, echo = FALSE, eval = TRUE, results ="hide">>=
get_picture_code <- function(path,height,col=NULL){
paste0("\\adjustimage{height=",height,",valign=m,margin*=1ex}{",path,"}")
}
@
<<test, echo = FALSE, eval = TRUE, results ="hide">>=
dat <- data.frame(country = c("Belgium", "Germany", "Holland", "Ireland"),
Var1 = 1:4, Var2 = 11:14)
mypath <- paste0("images/",dat$country,".png")
dat$flag <- get_picture_code(path=mypath,height="0.8cm")
dat$test <-NA
dat$test[2:3] <-get_picture_code(path="images/orange_flag",height="0.6cm")
print(xtable(dat,
align = c("l","l","l","l","c","c"),
caption = "Example with flags"),
sanitize.text.function = identity,
file="table_with_images.tex")
@
\input{table_with_images.tex}
\end{document}
The adjustbox documentation contains many other options, including background colors, trim, horizonal alignment which will allow you to do some very fine adjustment of the position of the images... There is also a nice example of the use of this package in TeX-Latex stackexchange
来源:https://stackoverflow.com/questions/25106481/add-an-image-to-a-table-like-output-in-r