-
Notifications
You must be signed in to change notification settings - Fork 10
Update custom figures/tables vignette #443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
be471d5
30bcc67
dfcdc97
9e06f09
dd36cab
7389ded
a6e84f1
d38ae7f
1e42cad
bf45385
c4f123d
e4291f1
2b98f46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,207 @@ | ||
| ### Add custom tables and figures {#sec-add-plots_coding} | ||
|
|
||
| In your 'report' folder, open your figures and tables docs (probably named something like '08_tables.qmd' and '09_figures.qmd'). Add the following code there. | ||
|
|
||
| **IMPORTANT**: The label in your captions/alt text csv must match the label in your plot's chunk options, **MINUS the "fig-" or "tab-"**. For example, the label "custom_table1" in the csv would match up properly with the "tbl-custom_table1" label in a code chunk. | ||
| The same rule applies for figures: "custom_figure1" in the csv would match up with "fig-custom_figure1" in the chunk options' label. | ||
|
|
||
| #### Tables | ||
|
|
||
| Follow the instructions in the appropriate section below based on your table classification: reg_reg, reg_long, wide_reg, wide_long, ewide_reg, or ewide_long. | ||
|
|
||
| For all table types, add a setup chunk to define your table and its caption: | ||
|
|
||
| ````{verbatim} | ||
| ```{r} | ||
| #| label: 'tab-setup-custom' | ||
| #| include: false | ||
|
|
||
| # Example dataframe | ||
| your_df <- data.frame( | ||
| "x" = c(1, 2, 3), | ||
| "y" = c(4, 5, 6) | ||
| ) | ||
|
|
||
| # Example table | ||
| your_table <- gt::gt(your_df) | ||
|
|
||
| # Define your caption | ||
| your_cap <- "This is the caption for my custom table." | ||
| ``` | ||
| ```` | ||
|
|
||
| ##### Regular tables (`reg_reg`) | ||
|
|
||
| *Portrait orientation, fits on one page.* | ||
|
|
||
| Add this chunk to display the table: | ||
|
|
||
| ````{verbatim} | ||
| ```{r} | ||
| #| label: 'tbl-custom1' | ||
| #| echo: false | ||
| #| warning: false | ||
| #| tbl-cap: !expr your_cap | ||
|
|
||
| your_table | ||
| ``` | ||
| ```` | ||
|
|
||
| ##### Wide tables (`wide_reg`) | ||
|
|
||
| *Landscape orientation, fits on one page.* | ||
|
|
||
| Wrap the chunk in landscape braces: | ||
|
|
||
| ````{verbatim} | ||
| ::: {.landscape} | ||
|
|
||
| ```{r} | ||
| #| label: 'tbl-custom-wide' | ||
| #| echo: false | ||
| #| warning: false | ||
| #| tbl-cap: !expr your_cap | ||
|
|
||
| your_table |> | ||
| gt::tab_options(table.width = pct(100), table.layout = 'auto') |> | ||
| gt::cols_width(everything() ~ pct(20)) | ||
| ``` | ||
|
|
||
| ::: | ||
| ```` | ||
|
|
||
| ##### Long tables (`reg_long` or `wide_long`) | ||
|
|
||
| *Splits by row across multiple pages. `wide_long` uses landscape; `reg_long` uses portrait.* | ||
|
|
||
| You will need a separate chunk for each page of the split. For example, if your table requires 2 pages, repeat this chunk twice, changing the index in `grp_pull(1)` to `grp_pull(2)`. | ||
|
|
||
| ````{verbatim} | ||
| ```{r} | ||
| #| label: 'tbl-custom-long-p1' | ||
| #| echo: false | ||
| #| tbl-cap: !expr paste0(your_cap, ' (Page 1 of 2)') | ||
|
|
||
| your_table |> | ||
| gt::tab_options(table.width = pct(100), table.layout = 'auto') |> | ||
| gt::cols_width(everything() ~ pct(20)) |> | ||
| asar::gt_split(row_every_n = 38) |> # Use 28 for wide_long | ||
| gt::grp_pull(1) # Change this to 2 for the next page | ||
| ``` | ||
| ```` | ||
|
|
||
| > **NOTE**: For `wide_long`, wrap the chunk in `::: {.landscape}` braces. For example: | ||
|
|
||
| ````{verbatim} | ||
| ::: {.landscape} | ||
| # code chunk shown above | ||
| ::: | ||
| ```` | ||
|
|
||
| ##### Extra-wide tables (`ewide_reg`) | ||
|
|
||
| *Landscape orientation, split by column across multiple pages.* | ||
|
|
||
| Because extra-wide tables are split by column, you must have already run `export_split_tbls()` to create the `_split.rda` file. Load that split file to access the list of tables: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. export_split_tbls() on their custom table? If so, would be good to explain that step here |
||
|
|
||
| ````{verbatim} | ||
| ```{r} | ||
| #| label: 'tbl-setup-ewide-split' | ||
| #| include: false | ||
| # Load the list of split tables generated by export_split_tbls() | ||
| load(file.path(tables_dir, 'example_tab_table_split.rda')) | ||
| table_list_split <- table_list | ||
| ``` | ||
| ```` | ||
|
|
||
| Then, for each split table in the list, create a landscape chunk: | ||
|
|
||
| ````{verbatim} | ||
| ::: {.landscape} | ||
|
|
||
| ```{r} | ||
| #| label: 'tbl-custom-ewide-c1' | ||
| #| echo: false | ||
| #| tbl-cap: !expr paste0(your_cap, ' (Column split 1 of 3)') | ||
|
|
||
| table_list_split[[1]] |> | ||
| gt::tab_options(table.width = pct(100), table.layout = 'auto') |> | ||
| gt::cols_width(everything() ~ pct(20)) | ||
| ``` | ||
|
|
||
| ::: | ||
| ```` | ||
|
|
||
| ##### Extra-wide, long tables (`ewide_long`) | ||
|
|
||
| *Landscape orientation, split by both column AND row.* | ||
|
|
||
| This requires a nested approach using the `table_list_split` loaded above. You must create chunks for every combination of column-split and row-split. | ||
|
|
||
| ````{verbatim} | ||
| ::: {.landscape} | ||
|
|
||
| ```{r} | ||
| #| label: 'tbl-custom-ewide-long-c1-r1' | ||
| #| echo: false | ||
| #| tbl-cap: !expr paste0(your_cap, ' (Column split 1 of 2, Row split 1 of 3)') | ||
|
|
||
| table_list_split[[1]] |> | ||
| gt::tab_options(table.width = pct(100), table.layout = 'auto') |> | ||
| gt::cols_width(everything() ~ pct(20)) |> | ||
| asar::gt_split(row_every_n = 28) |> | ||
| gt::grp_pull(1) | ||
| ``` | ||
|
|
||
| ::: | ||
| ```` | ||
|
|
||
| The next chunk would be identical except for a different label, caption, and would have `gt::grp_pull(2)` instead of `gt::grp_pull(1)`, for instance. When adding the second split table, add `table_list_split[[2]]` instead of `table_list_split[[1]]`, then change the label, caption, and `gt::grp_pull()` as needed. | ||
|
|
||
| ##### Final step: page break | ||
|
|
||
| If you are adding multiple tables, insert a page break between them: | ||
|
|
||
| ```{verbatim} | ||
| {{< pagebreak >}} | ||
| ``` | ||
|
|
||
| #### Figures | ||
|
|
||
| The workflow for figures is very similar to the workflow for regular tables. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would add at the end of this sentence something like "a set-up chunk followed by an executable chunk". I think it would be helpful to reiterate this and make it as clear as possible since this is not always intuituve. |
||
|
|
||
| In your figures doc, add the following chunk to display the figure in your .qmd when it's rendered: | ||
|
|
||
| ````{verbatim} | ||
| ```{r} | ||
| #| label: 'fig-custom1' # choose a label | ||
| #| echo: false | ||
| #| warning: false | ||
| #| fig-cap: !expr This is your figure caption. | ||
| #| fig-alt: !expr This is your figure alternative text. | ||
| # Import your data | ||
| your_df <- data.frame( | ||
| "x" = c(1, 2, 3), | ||
| "y" = c(4, 5, 6) | ||
| ) | ||
|
|
||
| # Make your figure | ||
| your_figure <- ggplot2::ggplot( | ||
| your_df, | ||
| aes( | ||
| x = x, | ||
| y = y | ||
| ) | ||
| ) + | ||
| ggplot2::geom_point() | ||
|
|
||
| # Show your figure | ||
| your_figure | ||
| ``` | ||
| ```` | ||
|
|
||
| If you are adding another figure, add a page break first: | ||
|
|
||
| ```{verbatim} | ||
| {{< pagebreak >}} | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| ### Add custom tables and figures {#sec-add-plots_img} | ||
|
|
||
| In your 'report' folder, open your figures and tables docs (probably named something like '08_tables.qmd' and '09_figures.qmd'). Add the following code there. | ||
|
|
||
|
|
||
| **IMPORTANT**: The label in your captions/alt text csv must match the label in your plot's chunk options, **MINUS the "fig-" or "tab-"**. For example, the label "your_image_label_name" in the csv would match up properly with the "tbl-your_image_label_name" label in this code: | ||
|
|
||
| ``` rmd | ||
| {width=4in #fig-your_image_label_name} | ||
| ``` | ||
|
|
||
| The same rule applies for tables: "custom_table2" in the csv would match up with "tbl-custom_table2" in the markdown's label. | ||
|
|
||
| #### Identify page orientation | ||
|
|
||
| If you're using this workflow, your table/figure should be at most 8" wide or can be shrunken to 8" wide and still be legible. Find the width of your image file. Would you prefer to show it on a page with a portrait orientation (can fit 5" width) or landscape orientation (can fit 8" width)? | ||
|
|
||
| Identify the preferred width of your image. Keep it in mind for when you add your image in the next section. | ||
|
|
||
| #### Add image | ||
|
|
||
| Next, add your image in this format ([see this Quarto documentation for more information](https://quarto.org/docs/authoring/figures.html)): | ||
|
|
||
| ``` markdown | ||
| {width=___in #fig-your_image_label_name} | ||
| ``` | ||
| > **NOTE**: Remember that alternative text placed in this markdown format will not be put into your final report file; it must be in the captions_alt_text.csv file. However, *you must put the caption here*. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need to double check this, but I don't think this is necessarily true. I think there is a way this will work |
||
|
|
||
| > **NOTE**: It can be a headache to specify the correct image path. We recommend placing your files in either of these places: | ||
|
|
||
| 1. Your report folder (path = the image name, like *image.png*) | ||
| 2. A folder within your report folder (path = the relative filepath, like *images/image.png* if there's an images folder within report) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would add a third example that the NE just taught me which is relative to the folder "../../my_image.png" for example
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh and maybe a folder structure diagram similar to the example below |
||
|
|
||
| Here are some examples, using the following example folder structure setup: | ||
|
|
||
| ``` markdown | ||
| stock_assessments_2025/ | ||
| ├── captions_alt_text.csv | ||
| ├── report/ | ||
| │ ├── 01_executive_summary.qmd | ||
| │ ├── 02_introduction.qmd | ||
| │ ├── my_custom_figure.png | ||
| │ ├── images/ | ||
| │ │ ├── my_custom_table.png | ||
| ``` | ||
|
|
||
| ``` markdown | ||
| # Example 1 | ||
| {width=3in #fig-custom_figure1} | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would change the name of this example of the other like my_custom_figure.png to differentiate these so they understand that this is coming from a different directory than the next path. I think its clear to me but just to be safe |
||
| ``` | ||
|
|
||
| ``` markdown | ||
| # Example 2 | ||
| {width=5in #tab-custom_table1} | ||
| ``` | ||
|
|
||
| #### Add landscape braces (optional) | ||
|
|
||
| If you'd like to place your image on a landscape-oriented page, you'll add [landscape braces](https://quarto.org/docs/authoring/article-layout.html#landscape-mode) before and after your table/figure to ensure it will be displayed properly, like this: | ||
|
|
||
| ```{verbatim} | ||
| ::: {.landscape} | ||
| ``` | ||
|
|
||
| ``` rmd | ||
| {width=___in #fig-your_image_label_name} | ||
| ``` | ||
|
|
||
| Then, add three colons to close the braces: | ||
|
|
||
| ```{verbatim} | ||
| ::: | ||
| ``` | ||
|
|
||
| > **NOTE**: Landscape braces only work in [Quarto v1.6+](https://quarto.org/docs/blog/posts/2024-11-25-1.6-release/). Please read [this Discussion](https://github.com/nmfs-ost/asar/discussions/186) to learn how to check your Quarto version and update it if necessary. | ||
|
|
||
| #### Page break | ||
|
|
||
| If you are including more than one custom table or figure in a file, we suggest adding a page break to separate the tables/figures: | ||
|
|
||
| ```{verbatim} | ||
| {{< pagebreak >}} | ||
| ``` | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it might be useful to list these instead and have them linked to each of their respective sections below