Jean-Alexandre Patteet
Jean-Alexandre Patteet

Reputation: 159

How to center row names and space out tables using kable

Here is an example code where I try to display 4 tables, tables on the second row are too close from tables on the first row, is there any way I can separate them?

I would also like row names to all be aligned and centered, The last row name is never aligned with the other ones.


```{r, echo=FALSE, warning= FALSE}

ex <- data.frame(B=c(10,5,12,4),
                 W=c(20,2,6,7))
row.names(ex) <- c("D", "E","A","B")

```

```{r, echo=FALSE, warning=FALSE}

#| label: tbl-ConfusionMatrices
#| tbl-cap: "Confusion matrices for each tool with the confusion matrix for ROI 1 and the with combined ROIs"
#| tbl-subcap: 
#|   - "Tool K1"
#|   - "Tool K2"
#|   - "Tool K3"
#|   - "Tool K3"


library(kableExtra)
library(knitr)
kable(ex)%>% pack_rows( index = c("ROI1" = 2, "ROIs"))
kable(ex)%>% pack_rows( index = c("ROI1" = 2, "ROIs"))
kable(ex)%>% pack_rows( index = c("ROI1" = 2, "ROIs"))
kable(ex)%>% pack_rows( index = c("ROI1" = 2, "ROIs"))
```

Upvotes: 1

Views: 557

Answers (1)

Shafee
Shafee

Reputation: 19897

You can create some space between tables using negative value in layout chunk option. From the Quarto documentation on Custom Layout,

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.

You can also use negative values to create space between elements.

And the last row name B doesn't align with others because it is not packed under ROIs. So if you intend to pack both rows A and B under ROIs, then simply use the named vector c("ROI1" = 2, "ROIs" = 2).

---
title: "Kable Table alignment"
format: pdf
---

## Quarto

```{r, echo=FALSE, warning= FALSE}

ex <- data.frame(B=c(10,5,12,4),
                 W=c(20,2,6,7))
row.names(ex) <- c("D", "E","A","B")

```


```{r, echo=FALSE, warning=FALSE}
#| label: tbl-ConfusionMatrices
#| tbl-cap: "Confusion matrices for each tool with the confusion matrix for ROI 1 and the with combined ROIs"
#| tbl-subcap: 
#|   - "Tool K1"
#|   - "Tool K2"
#|   - "Tool K3"
#|   - "Tool K3"
#| layout: '[[1,1], [-1], [1,1]]'


library(kableExtra)
library(knitr)
kable(ex)%>% pack_rows( index = c("ROI1" = 2, "ROIs" = 2))
kable(ex)%>% pack_rows( index = c("ROI1" = 2, "ROIs" = 2))
kable(ex)%>% pack_rows( index = c("ROI1" = 2, "ROIs" = 2))
kable(ex)%>% pack_rows( index = c("ROI1" = 2, "ROIs" = 2))
```


kable tables with space


Upvotes: 1

Related Questions