-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathfigures.qmd
More file actions
587 lines (387 loc) · 20.6 KB
/
figures.qmd
File metadata and controls
587 lines (387 loc) · 20.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
---
title: "Figures"
format: html
aliases:
- figures-and-layout.html
- figure-layout.html
---
Quarto includes a number of features aimed at making it easier to work with figures and subfigures, as well as for laying out panels that contain multiple figures, tables, or other content.
## Figure Basics
In Pandoc markdown, a figure is created whenever a captioned image appears by-itself in a paragraph. For example:
``` markdown

```
This results in the following treatment for various output types:
| HTML | PDF | Word |
|---------------------------|-----------------------|-----------------------|
| {fig-alt="A line drawing of an elephant." width="340"} | {fig-alt="A line drawing of an elephant."} | {fig-alt="A line drawing of an elephant."} |
Note that for LaTeX / PDF output figures are automatically numbered (you can arrange for figures to be numbered in other formats using [Cross References](cross-references.qmd)).
### Figure Sizing
By default figures are displayed using their actual size (subject to the width constraints imposed by the page they are rendered within). You can change the display size by adding the `width` and `height` attributes to the figure. For example:
``` markdown
{width=300}
```
Note that if only `width` is specified then `height` is calculated automatically. If you need to modify the default behaviour just add an explicit `height` attribute.
The default units for `width` and `height` are pixels. You can also specify sizes using a percentage or a conventional measurement like inches or millimetres. For example:
``` markdown
{width=80%}
{width=4in}
```
### Linked Figures
When rendering with Quarto, you can enclose a figure within a link and it will still be treated within output as a captioned figure. For example:
``` markdown
[](https://en.wikipedia.org/wiki/Elephant)
```
### Figure Alignment
Figures are center aligned by default. Add the `fig-align` attribute to the image to use a different alignment. For example:
``` markdown
{fig-align="left"}
```
Note that figure captions are left aligned to accommodate longer caption text (which looks odd when center aligned).
### Alt Text
You can add alternative text to a figure by adding the `fig-alt` attribute to the image. For example, the following Markdown...
``` markdown
{fig-alt="A drawing of an elephant."}
```
... will create the following HTML with the corresponding alt tag:
``` html
<img src="elephant.png" alt="A drawing of an elephant.">
```
Note that the figure caption, title, and alt text can all be different. For example, the following code...
``` markdown
{fig-alt="A drawing of an elephant."}
```
...produces this HTML:
``` html
<img src="elephant.png" title="Title: An elephant" alt="A drawing of an elephant.">
```
To render the caption instead as alt text
you can append a backslash to the line
as detailed in [the Pandoc documentation](https://pandoc.org/MANUAL.html#extension-implicit_figures):
``` markdown
\
```
### Multiformat Figures
You can write markdown that provides a distinct image file format depending on the target output format. To do this simply leave-off the extension, for example:
``` markdown

```
By default this will look for `elephant.png`, however if you are rendering to PDF it will look for `elephant.pdf`. You can customize this behavior using the `default-image-extension` option. For example:
``` yaml
format:
html:
default-image-extension: svg
pdf:
default-image-extension: tex
```
### Lightbox Figures
In HTML output formats you can add lightbox styling and behavior to images to allow a reader to click to see a larger version of the image. For example, the following image has lightbox treatment (click on the image to see lightbox in action):
{.lightbox width=50%}
Lightbox treatment can be added by adding the class `.lightbox` to an image:
```markdown
{.lightbox}
```
For further details and other ways to enable and disable lightbox treatment see [Lightbox Figures](/docs/output-formats/html-lightbox-figures.qmd).
### Applying Multiple Parameters
To combine the above methods, separate arguments by a space, for example:
``` markdown
{fig-alt="A drawing of an elephant." fig-align="left" width=20%}
```
## Cross References
You can cross-reference figures by adding an ID with the `fig-` prefix to them, and then referencing the figure using the `@` prefix. For example:
``` markdown
{#fig-elephant}
This is illustrated well by @fig-elephant. (Note the space before the `@`!)
```
For figures produced by executable code cells, include a `label` with a `fig-` prefix to make them cross-referenceable. For example:
```` markdown
For a demonstration of a line plot, see @fig-line-plot.
```{{python}}
#| label: fig-line-plot
#| fig-cap: "A line plot "
import matplotlib.pyplot as plt
plt.plot([1,23,2,4])
plt.show()
```
````
::: callout-important
## Label Prefix
In order for a figure to be cross-referenceable, its label must start with the `fig-` prefix.
:::
See the article on [Cross References](cross-references.qmd) for additional details.
## Subfigures
If you have several figures that appear as a group, you can create a figure div to enclose them. For example:
``` markdown
::: {#fig-elephants layout-ncol=2}
{#fig-surus}
{#fig-hanno}
Famous Elephants
:::
```
Again, the last paragraph provides the main caption, and the individual figures carry the sub-captions. Here is what this looks like when rendered as HTML:
{fig-alt="An artistic rendition of Surus, Hannibal's last war elephant, is on the left. Underneath this picture is the caption '(a) Surus.' On the right is a line drawing of Hanno, a famous elephant. Underneath this picture is the caption '(b) Hanno.' The words 'Figure 1: Famous elephants' are centered beneath both pictures."}
Note that the empty lines between the figures (and between the last figure and the caption) are required (it's what indicates that these images belong to their own paragraphs rather than being multiple images within the same paragraph).
Note also that we also used a `layout-ncol` attribute to specify a two-column layout. The next section delves more into customizing figure layouts.
## Figure Panels
Above we demonstrate laying out two side-by-side figures with subcaptions and a main caption. You may or may not want the caption / sub-caption treatment, and you might also want to use multiple rows of figures. All of these variations are possible.
To layout two figures with their own standalone captions (and no main caption), just eliminate the `#fig` identifiers and main caption at the bottom:
``` markdown
::: {layout-ncol=2}


:::
```
{fig-alt="An artistic rendition of Surus, Hannibal's last war elephant, is on the left. Underneath this picture is the caption 'Surus.' On the right is a line drawing of Hanno, a famous elephant. Underneath this picture is the caption 'Hanno.'"}
You can also eliminate the captions entirely:
``` markdown
::: {layout-ncol=2}


:::
```
### Multiple Rows
If you have more than 2 images, you might want to lay them out across multiple rows. You can do this using the `layout-nrow` attribute. For example:
``` markdown
::: {layout-nrow=2}




:::
```
{fig-alt="A 2x2 grid of pictures of elephants. There are labels underneath each of the pictures. Clockwise from the upper left, the labels say: Surus, Hanno, Lin Wang, and Abdul Abbas."}
More complex figure arrangements (e.g. rows with varying column layouts) are possible. See the [Custom Layouts](#complex-layouts) section below for more details.
## Figure Divs
You can treat any markdown content you want as a figure by enclosing it in Pandoc div block with an identifier prefaced with `#fig-`. For example, here we create a figure that includes an embedded iframe:
``` markdown
::: {#fig-elephant}
<iframe width="560" height="315" src="https://www.youtube.com/embed/SNggmeilXDQ"></iframe>
Elephant
:::
```
Note that the last paragraph in the div block is used as the figure caption.
## LaTeX Figures
This section describes some figure handling options that are specific to LaTeX output.
One very important thing to note is that using the `fig-env` and `fig-pos` options described below will trigger the creation of a LaTeX floating environment (most often `\begin{figure}`). Depending upon where this LaTeX is generated (e.g., within another `\begin{figure}`), this could produce invalid LaTeX. To be on the safe side, these attributes should typically only be used for images at the very top level of your document.
### Environments
There are a number of LaTeX packages that provide custom figure environments. For example, in two-column formats, the `figure*` environment spans both columns of the document. You can explicitly pass the figure environment to use as the `fig-env` attribute of the image or the fenced div:
``` markdown
{#fig-elephant fig-env="figure*"}
::: {#fig-elephant-2 fig-env="figure*"}

Another elephant.
:::
```
### Figure Position
The default LaTeX `figure` is a floating environment, so the specific location it appears in your document will depend on its size and the nature of the other content around it. You can exercise some control over this behavior using the `fig-pos` option. The `fig-pos` option provides a placement specifier for the `figure` environment. For example, the `ht` in this LaTeX snippet is the `fig-pos`:
``` latex
\begin{figure}[ht]
\end{figure}
```
You can specify `fig-pos` either at the document level, as an executable code block option, or within markdown. Here we do all three:
```` markdown
---
title: "My Document"
format:
pdf:
fig-pos: 'h'
---
```{{python}}
#| fig-pos: 't'
```
{fig-pos='b'}
````
See [this article](https://www.latex-project.org/publications/2014-FMi-TUB-tb111mitt-float-placement.pdf) for additional details on LaTeX figure positioning.
::: callout-note
## Figures and Code Blocks
If your figure was generated by an executable code block and the code was included in the output (`echo: true`), then `fig-pos` will be set to `H` by default (to try to keep it alongside the code that generated it). In all other cases, default LaTeX handing of figure position is used unless you specify an explicit `fig-pos`.
:::
### Short Captions
You can provide a short caption that is used in the "List of Figures" using the `fig-scap` option.
You can specify `fig-scap` as an executable code block option or as an attribute on an image:
````markdown
```{{python}}
#| fig-cap: "Long caption"
#| fig-scap: "Short caption"
```
{fig-scap='Short caption'}
````
### PGF/Ti*k*Z Graphics
If you are creating LaTeX output, Quarto will automatically emit the correct LaTeX for markdown images that reference `.tex` files containg [PGF/Ti*k*Z](https://en.wikipedia.org/wiki/PGF/TikZ) vector graphics. For example, the following markdown images:
``` markdown

{width=80%}
```
Will be written to LaTeX as:
``` tex
\input{image1.tex}
\resizebox{0.8\linewidth}{!}{\input{image2.tex}}
```
As shown above, `width` and `height` percentages are automatically converted to `\linewidth` scaled. Alternatively you can specify custom LaTeX for the `width` and `height` arguments of `\resizebox`.
## Caption Locations
By default, figure captions are positioned below figures. In HTML and PDF formats, you can modify this behavior using the `fig-cap-location` option. For example:
``` yaml
---
fig-cap-location: top
---
```
Note that this option is specified at the top level so that it can be shared by both PDF and HTML formats. If you are only targeting a single format you can place it alongside other `format` specific options.
Valid values for the caption location include:
| Value | Description |
|----------|----------------------------------------|
| `top` | Position the caption above the figure. |
| `bottom` | Position the caption below the figure. |
| `margin` | Position the caption in the margin. |
See the article on [Article Layout](article-layout.qmd#margin-captions) for additional details on placing captions in the margin.
## Custom Layouts {#complex-layouts}
The examples above used the `layout-ncol` or `layout-nrow` attributes to create straightforward layouts where all columns are of equal sizes. The `layout` attribute enables the creation of much more complex layouts.
For example, this defines a layout with two equally sized figures in the first row, then another image that spans the entire second row:
``` markdown
::: {layout="[[1,1], [1]]"}



:::
```
{fig-alt="Three elephant pictures arranged such that two pictures are side-by-side in the first row, and the third picture is underneath both of these. The picture on the left in the first row is captioned 'Surus' and the picture on the right is captioned 'Hanno'. The picture underneath these two is captioned 'Lin Wang' and is as as wide and tall as the other two put together."}
The `layout` attribute is a 2-dimensional array where the first dimension defines rows and the second columns. In this case `"layout="[[1,1], [1]]"` translates to: create two rows, the first of which has two columns of equal size and the second of which has a single column.
Note that the numbers in a row are arbitrary and don't need to add up to a particular total. You can therefore use whatever scheme is most natural. For example, here we define columns that occupy varying percentage widths of the row:
``` markdown
::: {layout="[[70,30], [100]]"}



:::
```
You can also use negative values to create space between elements. For example:
``` markdown
::: {layout="[[40,-20,40], [100]]"}



:::
```
{fig-alt="Three elephant pictures arranged such that two pictures are side-by-side in the first row, and the third picture is underneath both of these. The picture on the left in the first row is captioned 'Surus' and the picture on the right is captioned 'Hanno'. The two pictures are separated by some whitespace. The picture underneath these two is captioned 'Lin Wang' and is wider and taller than the other two put together."}
### Vertical Alignment
If you have a layout with a row of images of differing heights, you can control their vertical alignment using the `layout-valign` attribute. A simple example:
``` markdown
::: {layout="[15,-2,10]" layout-valign="bottom"}


:::
```
{fig-alt="Two pictures of elephants side by side. The picture on the left has the label 'Figure 1: Surus' underneath it. The picture on the right has the label 'Figure 2: Lin Wang' underneath it. The figure on the left is more than twice the width and height of the figure on the right."}
Note that vertical alignment isn't limited to images, you can also vertically align any other elements that are included in a panel.
## Computations
Figures produced by executable code blocks are automatically included in your document.
To set the ID, caption and link, use the chunk options `label`, `fig-cap` and `fig-link` respectively.
Other attributes, e.g. `fig-align` and `fig-alt`, can be set using the chunk option of the same name.
You can control the default size for computational figures using the `fig-width` and `fig-height` options in the document header.
Read more about these options in [Execution Options: Figure Options](/docs/computations/execution-options.qmd#figure-options).
### Layout
Note that figure layout attributes also work for figures produced by executable code blocks. Here are examples for both Jupyter and Knitr:
::: panel-tabset
#### Jupyter
```{{python}}
#| layout-ncol: 2
#| fig-cap:
#| - "Line Plot 1"
#| - "Line Plot 2"
import matplotlib.pyplot as plt
plt.plot([1,23,2,4])
plt.show()
plt.plot([8,65,23,90])
plt.show()
```
{fig-alt="Two line plots rendered by Jupyter side-by-side."}
#### Knitr
```{{r}}
#| layout-ncol: 2
#| fig-cap:
#| - "Speed and Stopping Distances of Cars"
#| - "Vapor Pressure of Mercury as a Function of Temperature"
plot(cars)
plot(pressure)
```
{fig-alt="Two scatter plots arranged side-by-side."}
:::
Note that in these examples we also use the `fig-cap` option to apply a caption to each of the generated figures.
### Subcaptions
You can create subcaptions for computational output by combining the `fig-cap` and `fig-subcap` options. When applying captions to computational output you can optionally include a `label` with a `fig-` prefix---if you do this then the figure will be numbered and [cross-referenceable](cross-references.qmd).
::: panel-tabset
#### Jupyter
```{{python}}
#| label: fig-charts
#| fig-cap: "Charts"
#| fig-subcap:
#| - "First"
#| - "Second"
#| layout-ncol: 2
import matplotlib.pyplot as plt
plt.plot([1,23,2,4])
plt.show()
plt.plot([8,65,23,90])
plt.show()
```
{fig-alt="Two line plots with captions rendered by Jupyter side-by-side."}
#### Knitr
```{{r}}
#| label: fig-charts
#| fig-cap: "Charts"
#| fig-subcap:
#| - "Cars"
#| - "Pressure"
#| layout-ncol: 2
plot(cars)
plot(pressure)
```
{fig-alt="Two scatter plots with captions arranged side-by-side."}
:::
### Custom Layout
The `layout` works the same way for figures produced by Knitr or Jupyter. For example, here's an Rmd code chunk that produces 3 plots and defines a custom layout for them:
```{{r}}
#| layout: [[45,-10, 45], [100]]
plot(cars)
plot(pressure)
plot(mtcars)
```
{.preview-image fig-alt="Two plots arranged side-by-side with a large plot underneath it. The top two plots are scatter plots visualizing the `cars` and `pressure` datasets. These two plots are separated by some additional white space. The plot on the bottom visualizes the `mtcars` dataset and is wider and taller than the other two plots combined. This plot is an 11 by 11 grid plotting each of the 11 variables in the `mtcars` dataset against each other as a scatterplot. Instead of scatter plots in the diagonal starting in the upper left and going to the lower right are text labels for each of the variable names. These are: 'mpg', 'cyl', 'disp', 'hp', 'drat', 'wt', 'qsec', 'vs', 'am', 'gear', and 'carb'."}
## Block Layout
While the examples above illustrate laying out figures, it's important to note that layout attributes can be used to layout any sort of block content. For example, here we layout 2 lists side-by-side:
``` markdown
::: {layout-ncol=2}
### List One
- Item A
- Item B
- Item C
### List Two
- Item X
- Item Y
- Item Z
:::
```
Note that headings are automatically combined with the block that follows them, so this markdown has a total of 2 columns to lay out. Here's an example of a paragraph next to a bullet list (without headings):
``` markdown
::: {layout-ncol=2}
- Item X
- Item Y
- Item Z
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur gravida eu erat et fring. Morbi congue augue vel eros ullamcorper, eget convallis tortor sagittis. Fusce sodales viverra mauris a fringilla. Donec feugiat, justo eu blandit placerat, enim dui volutpat turpis, eu dictum lectus urna eu urna. Mauris sed massa ornare, interdum ipsum a, semper massa.
:::
```
For more complicated content use divs (`:::`) to divide your content into blocks for the layout. For example, here's how you could lay out a code cell along with some text, next to a figure:
````markdown
:::: {layout="[ 40, 60 ]"}
::: {#first-column}
```r
# Some code
```
Some text that should be laid out below the code
:::
::: {#second-column}

:::
::::
````
The id attributes (`#first-column` and `#second-column`) are optional,
but aid readability.