R: Draw a Textual Table

ggtexttable {ggpubr} | R Documentation

## Draw a Textual Table

### Description

Draw a textual table.

- `ggtexttable()`: draw a textual table.

- `ttheme()`: customize table theme.

- `rownames_style(), colnames_style(), tbody_style()`: helper functions to customize the table row names, column names and body.

- `table_cell_font()`: access to a table cell for changing the text font (size and face).

- `table_cell_bg()`: access to a table cell for changing the background (fill, color, linewidth).

### Usage

    ggtexttable(x, rows = rownames(x), cols = colnames(x), vp = NULL,
      theme = ttheme(), ...)

    ttheme(base_style = "default", base_size = 11, base_colour = "black",
      padding = unit(c(4, 4), "mm"), colnames.style = colnames_style(size =
      base_size), rownames.style = rownames_style(size = base_size),
      tbody.style = tbody_style(size = base_size))

    colnames_style(color = "black", face = "bold", size = 12,
      fill = "grey80", linewidth = 1, linecolor = "white", parse = FALSE,
      ...)

    rownames_style(color = "black", face = "italic", size = 12, fill = NA,
      linewidth = 1, linecolor = "white", parse = FALSE, ...)

    tbody_style(color = "black", face = "plain", size = 12,
      fill = c("grey95", "grey90"), linewidth = 1, linecolor = "white",
      parse = FALSE, ...)

    table_cell_font(tab, row, column, face = NULL, size = NULL)

    table_cell_bg(tab, row, column, fill = NULL, color = NULL,
      linewidth = NULL)

### Arguments

`x` | a `data.frame` or `matrix`.
`rows` | optional vector to specify row names
`cols` | optional vector to specify column names
`vp` | optional viewport
`theme` | a list, as returned by the function `ttheme()`, defining the parameters of the table theme. Allowed values include one of `ttheme()` and `ttheme_clean()`.
`...` | extra parameters for text justification, e.g.: hjust and x. Default is "centre" for the body and header, and "right" for the row names. Left justification: `hjust = 0, x = 0.1`. Right justification: `hjust = 1, x = 0.9`.
`base_style` | charcter string the table style/theme. The available themes are illustrated in the ggtexttable-theme.pdf file. Allowed values include one of `c("default", "blank", "classic", "minimal", "light", "lBlack", "lBlue", "lRed", "lGreen", "lViolet", "lCyan", "lOrange", "lBlackWhite", "lBlueWhite", "lRedWhite", "lGreenWhite", "lVioletWhite", "lCyanWhite", "lOrangeWhite", "mBlack", "mBlue", "mRed", "mGreen", "mViolet", "mCyan", "mOrange", "mBlackWhite", "mBlueWhite", "mRedWhite", "mGreenWhite", "mVioletWhite", "mCyanWhite", "mOrangeWhite" )`. Note that, l = "light"; m = "medium".
`base_size` | default font size
`base_colour` | default font colour
`padding` | length-2 unit vector specifying the horizontal and vertical padding of text within each cell
`colnames.style` | a list, as returned by the function `colnames_style()`, defining the style of the table column names. Considered only when `base_size = "default"`.
`rownames.style` | a list, as returned by the function `rownames_style()`, defining the style of the table row names. Considered only when `base_size = "default"`.
`tbody.style` | a list, as returned by the function `tbody_style()`, defining the style of the table body. Considered only when `base_size = "default"`.
`color, face, size` | text font color, face and size, respectively. Allowed values for face include c("plain", "bold", "italic", "bold.italic").
`fill` | background color.
`linewidth, linecolor` | line width and color, respectively.
`parse` | logical, default behaviour for parsing text as plotmath
`tab` | an object of class ggtexttable.
`row, column` | an integer specifying the row and the column numbers for the cell of interest.

### Value

an object of class ggplot.

### Examples

    # data
    df <- head(iris)

    # Default table
    # Remove row names using rows = NULL
    ggtexttable(df, rows = NULL)

    # Blank theme
    ggtexttable(df, rows = NULL, theme = ttheme("blank"))

    # classic theme
    ggtexttable(df, rows = NULL, theme = ttheme("classic"))

    # minimal theme
    ggtexttable(df, rows = NULL, theme = ttheme("minimal"))

    # Medium blue (mBlue) theme
    ggtexttable(df, rows = NULL, theme = ttheme("mBlue"))

    # Customize the table as you want
    ggtexttable(df, rows = NULL,
               theme = ttheme(
                 colnames.style = colnames_style(color = "white", fill = "#8cc257"),
                 tbody.style = tbody_style(color = "black", fill = c("#e8f3de", "#d3e8bb"))
               )
    )

    # Use RColorBrewer palette
    # Provide as many fill color as there are rows in the table body, here nrow = 6
    ggtexttable(df,
               theme = ttheme(
                 colnames.style = colnames_style(fill = "white"),
                 tbody.style = tbody_style(fill = get_palette("RdBu", 6))
               )
    )

    # Text justification
    #::::::::::::::::::::::::::::::::::::::::::::::
    # Default is "centre" for the body and header, and "right" for the row names.
    # Left justification: hjust=0, x=0.1
    # Right justification: hjust=1, x=0.9
    tbody.style = tbody_style(color = "black",
       fill = c("#e8f3de", "#d3e8bb"), hjust=1, x=0.9)
    ggtexttable(head(iris), rows = NULL,
               theme = ttheme(
                 colnames.style = colnames_style(color = "white", fill = "#8cc257"),
                 tbody.style = tbody.style
               )
    )

    # Access and modify the font and
    # the background of table cells
    # :::::::::::::::::::::::::::::::::::::::::::::
    tab <- ggtexttable(head(iris), rows = NULL,
                      theme = ttheme("classic"))
    tab <- table_cell_font(tab, row = 3, column = 2,
                          face = "bold")
    tab <- table_cell_bg(tab, row = 4, column = 3, linewidth = 5,
                        fill="darkolivegreen1", color = "darkolivegreen4")
    tab

    # Combine density plot and summary table
    #:::::::::::::::::::::::::::::::::::::
    # Density plot of "Sepal.Length"
    density.p <- ggdensity(iris, x = "Sepal.Length",
                          fill = "Species", palette = "jco")

    # Draw the summary table of Sepal.Length
    # Descriptive statistics by groups
    stable <- desc_statby(iris, measure.var = "Sepal.Length",
                         grps = "Species")
    stable <- stable[, c("Species", "length", "mean", "sd")]
    stable.p <- ggtexttable(stable, rows = NULL,
                           theme = ttheme("mOrange"))

    # Arrange the plots on the same page
    ggarrange(density.p, stable.p,
             ncol = 1, nrow = 2,
             heights = c(1, 0.5))

[Package ggpubr version 0.1.6 Index]