Reputation: 1523
Please have a look at the reprex below. I use quarto to generate a word document with a table and a plot. I adapted some rmarkdown code, but I am not there yet. I would like the table and the plot to be numbered, to show their caption on the top and I would like to reference them, but I am not there yet. Finally, I would like to set the font type (e.g. verdana) for the whole document. Any suggestion is appreciated.
---
format:
docx:
toc: false
number-sections: true
highlight-style: github
execute:
echo: false
warning: false
message: false
---
```{r, scoreboard, echo=FALSE, eval=TRUE}
library(tidyverse, warn.conflicts = FALSE)
library(viridis)
library(flextable)
stat_cases <- structure(list(procedure_name = c("Agriculture Block Exemption Regulation",
"Fisheries Block Exemption Regulation", "General Block Exemption Regulation",
"Notified Aid", "Total"), n = c(38L, 3L, 251L, 47L, 339L), percent = c("11.2%",
"0.9%", "74.0%", "13.9%", "100.0%")), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame"), core = structure(list(procedure_name = c("Agriculture Block Exemption Regulation",
"Fisheries Block Exemption Regulation", "General Block Exemption Regulation",
"Notified Aid"), n = c(38L, 3L, 251L, 47L), percent = c(11.2,
0.9, 74, 13.9)), row.names = c(NA, -4L), class = "data.frame"), tabyl_type = "two_way", totals = "row")
stat_cases2 <- structure(list(year = c(2012, 2012, 2012, 2013, 2013, 2013, 2014,
2014, 2014, 2015, 2015, 2015, 2016, 2016, 2016, 2017, 2017, 2017,
2018, 2018, 2018, 2019, 2019, 2019, 2020, 2020, 2020, 2021, 2021,
2021, 2022, 2022, 2022), procedure_name = c("Agriculture Block Exemption Regulation",
"General Block Exemption Regulation", "Notified Aid", "Agriculture Block Exemption Regulation",
"General Block Exemption Regulation", "Notified Aid", "Agriculture Block Exemption Regulation",
"General Block Exemption Regulation", "Notified Aid", "Agriculture Block Exemption Regulation",
"General Block Exemption Regulation", "Notified Aid", "Agriculture Block Exemption Regulation",
"General Block Exemption Regulation", "Notified Aid", "Agriculture Block Exemption Regulation",
"General Block Exemption Regulation", "Notified Aid", "Agriculture Block Exemption Regulation",
"General Block Exemption Regulation", "Notified Aid", "Agriculture Block Exemption Regulation",
"General Block Exemption Regulation", "Notified Aid", "Agriculture Block Exemption Regulation",
"General Block Exemption Regulation", "Notified Aid", "Agriculture Block Exemption Regulation",
"General Block Exemption Regulation", "Notified Aid", "Agriculture Block Exemption Regulation",
"General Block Exemption Regulation", "Notified Aid"), expenditure = c(0.031341,
0.772735, 1.0506364, 0.031725, 0.672124, 1.1587967, 0.027114,
0.395669, 1.0655565, 0.023264, 0.910199, 0.9733338, 0.132403,
0.9334881, 0.9164271, 0.1159009, 0.9422837, 0.8899091, 0.1342904,
1.0357514, 0.7333295, 0.1724899, 1.1234719, 0.7128631, 0.1892858,
1.265574, 6.1494719, 0.1930364, 1.2223709, 8.573606, 0.207672,
1.2500287, 5.0596376), expenditure_deflated = c(0.040544631307,
0.999657179817, 1.359167399796, 0.040209125481, 0.851868187541,
1.468690367567, 0.033474074075, 0.488480246886, 1.315501851829,
0.027961538464, 1.09398918266, 1.169872355741, 0.155768235294,
1.098221294133, 1.078149529462, 0.134611962827, 1.094406155676,
1.033576190453, 0.152602727269, 1.176990227258, 0.83332897725,
0.192511049105, 1.253874888393, 0.795606138384, 0.205076706392,
1.371152762738, 6.662483098824, 0.204487711868, 1.29488442799,
9.082209745624, 0.207672, 1.2500287, 5.0596376)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -33L))
```
---
title: "Country Focus on this and that"
---
```{r, tab.cap="Number and share of State aid measures by type of procedure.", tab.id='tab1', label='tab1' , echo=FALSE, eval=TRUE}
year_focus <- 2022
ft <- stat_cases |>
flextable() |>
set_header_labels(procedure_name="Type of Procedure",
n="Number of Active Measures",
percent="Share of Total"
) |>
theme_zebra() |>
fontsize(part = "all", size = 8) |>
font(part="all", fontname = "Verdana") |>
colformat_double(big.mark = " ") |>
## autofit() ## %>%
width(width = c(2,2,2))
ft <- add_header_row(
x = ft, values = paste("State Aid Measures in ",year_focus, sep=""),
colwidths = c(3))
ft <- theme_box(ft) |>
align(align="center", part="header") |>
align(align = "right",j=c(2,3), part="body") ## |>
ft
```
I would like the table above to be numbered, to show its caption and I
would like to be able to reference it as Table xxx where xxx is the
table number.
```{r spending, echo=FALSE,fig.cap="State aid expenditure at constant prices by type of procedure.",fig.height = 6, fig.width = 12}
my_pal <- viridis(4)
ggplot(data = stat_cases2, aes(x = year, y=expenditure_deflated,
fill=procedure_name)) +
geom_bar(position=position_dodge2(preserve="single"), stat="identity", alpha=1, color="black")+
scale_fill_manual(NULL, values=my_pal)+
labs(title=paste("State aid spending in billion EUR "))+
xlab(NULL)+
ylab("State aid expenditure (EUR bn)")+
guides(fill=guide_legend(## nrow=1,byrow=TRUE,
position="top"))
```
Also, I would like the plot above to be numbered and to reference it
as Figure yyy where yyy is the plot number.
Finally, for both the plot and the table, I would like to have the
caption on top.
Last but not least, can I choose the font type (e.g. verdana) for the
whole document?
Upvotes: 0
Views: 115
Reputation: 9240
You have to change the chunk options and use the right labels to make them referencable:
---
title: "Country Focus on this and that"
format:
docx:
toc: false
number-sections: true
highlight-style: github
execute:
echo: false
warning: false
message: false
---
```{r, scoreboard, echo=FALSE, eval=TRUE}
library(tidyverse, warn.conflicts = FALSE)
library(viridis)
library(flextable)
stat_cases <- structure(list(procedure_name = c("Agriculture Block Exemption Regulation",
"Fisheries Block Exemption Regulation", "General Block Exemption Regulation",
"Notified Aid", "Total"), n = c(38L, 3L, 251L, 47L, 339L), percent = c("11.2%",
"0.9%", "74.0%", "13.9%", "100.0%")), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame"), core = structure(list(procedure_name = c("Agriculture Block Exemption Regulation",
"Fisheries Block Exemption Regulation", "General Block Exemption Regulation",
"Notified Aid"), n = c(38L, 3L, 251L, 47L), percent = c(11.2,
0.9, 74, 13.9)), row.names = c(NA, -4L), class = "data.frame"), tabyl_type = "two_way", totals = "row")
stat_cases2 <- structure(list(year = c(2012, 2012, 2012, 2013, 2013, 2013, 2014,
2014, 2014, 2015, 2015, 2015, 2016, 2016, 2016, 2017, 2017, 2017,
2018, 2018, 2018, 2019, 2019, 2019, 2020, 2020, 2020, 2021, 2021,
2021, 2022, 2022, 2022), procedure_name = c("Agriculture Block Exemption Regulation",
"General Block Exemption Regulation", "Notified Aid", "Agriculture Block Exemption Regulation",
"General Block Exemption Regulation", "Notified Aid", "Agriculture Block Exemption Regulation",
"General Block Exemption Regulation", "Notified Aid", "Agriculture Block Exemption Regulation",
"General Block Exemption Regulation", "Notified Aid", "Agriculture Block Exemption Regulation",
"General Block Exemption Regulation", "Notified Aid", "Agriculture Block Exemption Regulation",
"General Block Exemption Regulation", "Notified Aid", "Agriculture Block Exemption Regulation",
"General Block Exemption Regulation", "Notified Aid", "Agriculture Block Exemption Regulation",
"General Block Exemption Regulation", "Notified Aid", "Agriculture Block Exemption Regulation",
"General Block Exemption Regulation", "Notified Aid", "Agriculture Block Exemption Regulation",
"General Block Exemption Regulation", "Notified Aid", "Agriculture Block Exemption Regulation",
"General Block Exemption Regulation", "Notified Aid"), expenditure = c(0.031341,
0.772735, 1.0506364, 0.031725, 0.672124, 1.1587967, 0.027114,
0.395669, 1.0655565, 0.023264, 0.910199, 0.9733338, 0.132403,
0.9334881, 0.9164271, 0.1159009, 0.9422837, 0.8899091, 0.1342904,
1.0357514, 0.7333295, 0.1724899, 1.1234719, 0.7128631, 0.1892858,
1.265574, 6.1494719, 0.1930364, 1.2223709, 8.573606, 0.207672,
1.2500287, 5.0596376), expenditure_deflated = c(0.040544631307,
0.999657179817, 1.359167399796, 0.040209125481, 0.851868187541,
1.468690367567, 0.033474074075, 0.488480246886, 1.315501851829,
0.027961538464, 1.09398918266, 1.169872355741, 0.155768235294,
1.098221294133, 1.078149529462, 0.134611962827, 1.094406155676,
1.033576190453, 0.152602727269, 1.176990227258, 0.83332897725,
0.192511049105, 1.253874888393, 0.795606138384, 0.205076706392,
1.371152762738, 6.662483098824, 0.204487711868, 1.29488442799,
9.082209745624, 0.207672, 1.2500287, 5.0596376)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -33L))
```
```{r}
#| label: tbl-tab1
#| tbl-cap: Number and share of State aid measures by type of procedure.
#| echo: false
#| eval: true
#| tbl-cap-location: top
year_focus <- 2022
ft <- stat_cases |>
flextable() |>
set_header_labels(procedure_name="Type of Procedure",
n="Number of Active Measures",
percent="Share of Total"
) |>
theme_zebra() |>
fontsize(part = "all", size = 8) |>
font(part="all", fontname = "Verdana") |>
colformat_double(big.mark = " ") |>
## autofit() ## %>%
width(width = c(2,2,2))
ft <- add_header_row(
x = ft, values = paste("State Aid Measures in ",year_focus, sep=""),
colwidths = c(3))
ft <- theme_box(ft) |>
align(align="center", part="header") |>
align(align = "right",j=c(2,3), part="body") ## |>
ft
```
I would like the table above to be numbered, to show its caption and I
would like to be able to reference it as @tbl-tab1.
```{r}
#| label: fig-spending
#| fig-cap: State aid expenditure at constant prices by type of procedure.
#| fig-width: 12
#| fig-height: 6
#| echo: false
#| fig-cap-location: top
my_pal <- viridis(4)
ggplot(data = stat_cases2, aes(x = year, y=expenditure_deflated,
fill=procedure_name)) +
geom_bar(position=position_dodge2(preserve="single"), stat="identity", alpha=1, color="black")+
scale_fill_manual(NULL, values=my_pal)+
labs(title=paste("State aid spending in billion EUR "))+
xlab(NULL)+
ylab("State aid expenditure (EUR bn)")+
guides(fill=guide_legend(## nrow=1,byrow=TRUE,
position="top"))
```
Also, I would like the plot above to be numbered and to reference it
as @fig-spending.
Finally, for both the plot and the table, I would like to have the
caption on top.
Last but not least, can I choose the font type (e.g. verdana) for the
whole document?
To change font type you will need to create a word template, see here.
Upvotes: 1