10

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)

2
  • The answer to this will depend on the eventual format of the resulting document (HTML, PDF, Word, etc.).
    – Thomas
    Aug 3, 2014 at 16:21
  • The final format will be PDF. Aug 3, 2014 at 19:48

3 Answers 3

24

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('![](http://flagpedia.net/data/flags/mini/%s.png)', dat$abbr)
library(knitr)
kable(dat)
```

knitr, tables, and flags

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('![](%s)', 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)])
```
14
  • That's really nice. I indeed just started working with knitr to be able to create the report that this table will be part of. I hadn't figured out this solution yet, and was about to start writing code in R HTML to make this happen. But your solution is quite straightforward. Would there be an advantage to going the R HTML route for this? Aug 3, 2014 at 19:55
  • 1
    @PeterVerbeet HTML is a lot more fun than either Word or LaTeX. If you take a look at the rmarkdown link I included in the answer, you will realize the output of R Markdown is not necessarily HTML.
    – Yihui Xie
    Aug 6, 2014 at 2:20
  • Is there a way to do the same thing, while using Latex instead of rmarkdown? I have tried with "\includegraphics" as part of dat$flag, but it breaks down no matter how I try. Aug 22, 2014 at 20:20
  • 1
    @mavericks I have updated my answer for PDF output. You need to download the images.
    – Yihui Xie
    Oct 2, 2019 at 16:30
  • 1
    @mavericks Use kable(..., format = 'pandoc') if you are using bookdown.
    – Yihui Xie
    Oct 3, 2019 at 14:23
3

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

enter image description here

1
  • Hi Baptiste, this is an intriguing option. It is certainly easy to add formatting to the table like this (such as alternatingly adding a lightgrey background to the rows). I will have to study this further, thanks. Aug 3, 2014 at 20:55
1

The question was asked with pdf as the output, here is an answer using knitr, and something more appropriate than just \includegraphics

Table with flags

The trick is to use the adjustbox package in latex, with the following arguments:

  • the height (later used as the argument to the R function get_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 as 1ex 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.