Insert Figure / Table in R Markdown

So I want to insert the table AND a picture in R Markdown. In a regular text document, I can simply insert a table (5 rows by 2 columns), and for an image just copy and paste.

  1. How to insert a table of 5 rows of 2 columns (and also introduce material into them) (and also adjust the table in terms of the second column, which will be wider than the first)?

  2. How to insert a picture? In my opinion, I should first save the image as png, and then refer to it in my document. In addition, I want to automatically adjust the image in the report, for example, not to occupy more than a page or less than a page. (Is there a way to adjust the image size to my liking)?

  3. If anyone knows anything cool / formatting about R Markdown, can you also let me know? I know that # makes a great heading for a paragraph and ** ** holds things. Thats about everything I know, though!

+103
r r-markdown
Aug 6 '14 at 17:34
source share
4 answers

Several sites provide reasonable cheat sheets or HOWTOs for tables and images. Top on my list:

Images are very easy to use, but do not allow you to adjust the image to fit the page (see Update below). To configure image properties (size, resolution, colors, borders, etc.), you will need an image editor. I believe that I can do everything I need with one of ImageMagick , GIMP or InkScape , all with free and open source.

To add an image, use:

 ![Caption for the picture.](/path/to/image.png) 

I know that pandoc supports PNG and JPG, which should satisfy most of your needs.

You have control over the size of the image if you create it in R (like a graph). This can be done either directly in the command to create the image, or, even better, using the parameters if you use knitr (it is highly recommended ... read the parameters of the block , especially in the Section section).

I highly recommend looking through these tutorials; The markdown is very convenient and has many functions that most people do not use on a regular basis, but really like it when they study it. (SO is not necessarily the best place to ask questions that are very directly answered in these lessons.)




Update, August 2019-31

Some time ago, pandoc included "link_attributes" for images (apparently in 2015, with the jgm / pandoc # 244cd56 commit ). "Image resizing" can be done directly. For example:

 ![unchanged image](foo.jpg) ![much-smaller image](foo.jpg){#id .class width=30 height=20px} ![half-size image](foo.jpg){#id .class width=50% height=50%} 

Sizes can be specified without units (estimated pixels) or with " px , cm , mm , in , inch and % " (ref: https://pandoc.org/MANUAL.html , search link_attributes ).

(I'm not sure CommonMark implemented this, although there was a lengthy discussion .)

+127
Aug 6 '14 at 18:14
source share

Update : Since the response from @ r2evans, it’s much easier to embed images in R Markdown and control the image size.

Images

Bookdown does an excellent job of including the best way to include images using include_graphics() . For example, a full-width image can be printed with the caption below:

 '''{r pressure, echo=FALSE, fig.cap="A caption", out.width = '100%'} knitr::include_graphics("temp.png") ''' 

The reason this method is better than the pandoc ![your image](path/to/image) approach:

  • It automatically changes the command based on the output format (HTML / PDF / Word)
  • The same syntax can be used for chart size ( fig.width ), output width in the report ( out.width ), adding captions ( fig.cap ), etc.
  • It uses the best graphics devices for output. This means that PDF images remain tall.

tables

knitr::kable() is the best way to include tables in an R Markdown report, as fully explained here . Again, this feature is smart when automatically choosing the right formatting for the selected output.

 '''{r table} knitr::kable(mtcars[1:5,, 1:5], caption = "A table caption") ''' 

If you want to create your own simple tables in R Markdown and use R Studio, you can check out the insert_table package . It provides a user-friendly graphical interface for creating tables.

Achieving a custom style of table column width is beyond knitr , but for kableExtra this was the kableExtra package kableExtra : https://cran.r-project.org/web/packages/kableExtra/index.html

Style tips

The R Markdown title page is still the best place to learn most of the basic syntaxes you can use.

If you are looking for potential formatting extensions, the bookdown package bookdown also worth exploring. It provides the ability to cross-reference, create custom headers, and more: https://bookdown.org/yihui/bookdown/markdown-extensions-by-bookdown.html

+60
Apr 16 '18 at 11:40
source share

When it comes to inserting an image, the r2evans ![Caption for the picture.](/path/to/image.png) can be problematic if PDF output is required.

Knitr function include_graphics knitr::include_graphics('/path/to/image.png') - a more portable alternative on your behalf will create a markdown that is most suitable for the output format that you generate.

+5
Apr 16 '18 at 9:32
source share

In March, I made a deck presentation in slidify, Rmarkdown with impress.js , which is a cool 3D wireframe. My index.Rmd header looks like

 --- title : French TER (regional train) monthly regularity subtitle : since January 2013 author : brigasnuncamais job : Business Intelligence / Data Scientist consultant framework : impressjs # {io2012, html5slides, shower, dzslides, ...} highlighter : highlight.js # {highlight.js, prettify, highlight} hitheme : tomorrow # widgets : [] # {mathjax, quiz, bootstrap} mode : selfcontained # {standalone, draft} knit : slidify::knit2slides 
Subcategories

:

 /assets /css /impress-demo.css /fig /unnamed-chunk-1-1.png (generated by included R code) /img /SS850452.png (my image used as background) /js /impress.js /layouts/custbg.html # content:--- layout: slide --- {{{ slide.html }}} /libraries /frameworks /impressjs /io2012 /highlighters /highlight.js /impress.js index.Rmd 

the slide with the image in the background code snippet will be in my .Rmd:

 <div id="bg"> <img src="assets/img/SS850452.png" alt=""> </div> 

Some problems have appeared since the last time I worked on it (photos are no longer in the background, the text is too large on my R chart), but it works fine on my local computer. A crash occurs when I run it on RPubs.

+4
Aug 16 '15 at 11:06
source share



All Articles