ggpmisc (version 0.3.2)

geom_table: Inset tables

Description

geom_table adds a textual table directly to the ggplot using syntax similar to that of geom_label while geom_table_npc is similar to geom_label_npc in that x and y coordinates are given in npc units.

Usage

geom_table(mapping = NULL, data = NULL, stat = "identity",
  position = "identity", ..., parse = FALSE, na.rm = FALSE,
  show.legend = FALSE, inherit.aes = FALSE)

geom_table_npc(mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., parse = FALSE, na.rm = FALSE, show.legend = FALSE, inherit.aes = FALSE)

Arguments

mapping

The aesthetic mapping, usually constructed with aes or aes_. Only needs to be set at the layer level if you are overriding the plot defaults.

data

A layer specific dataset - only needed if you want to override the plot defaults.

stat

The statistical transformation to use on the data for this layer, as a string.

position

Position adjustment, either as a string, or the result of a call to a position adjustment function.

...

other arguments passed on to layer. This can include aesthetics whose values you want to set, not map. See layer for more details.

parse

If TRUE, the labels will be parsed into expressions and displayed as described in ?plotmath.

na.rm

If FALSE (the default), removes missing values with a warning. If TRUE silently removes missing values.

show.legend

logical. Should this layer be included in the legends? NA, the default, includes if any aesthetics are mapped. FALSE never includes, and TRUE always includes.

inherit.aes

If FALSE, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g. borders.

Alignment

You can modify table alignment with the vjust and hjust aesthetics. These can either be a number between 0 (right/bottom) and 1 (top/left) or a character ("left", "middle", "right", "bottom", "center", "top").

Inset size

You can modify inset table size with the size aesthetics, which determines the size of text within the table.

Details

The "width" and "height" of the table, like for text elements are 0, so stacking and dodging tables will not work by default. In addition, axis limits are not automatically expanded to include the whole tables, but instead only their x and y coordinates. Obviously, tables do have height and width, but they are in physical units, not data units. The amount of space they occupy on a plot is not constant in data units: when you resize a plot, tables stay the same size, but the size of the axes changes.

References

This geometry is inspired on answers to two questions in Stackoverflow. In contrast to these earlier examples, the current geom obeys the grammar of graphics, and attempts to be consistent with the behaviour of 'ggplot2' geometries. https://stackoverflow.com/questions/12318120/adding-table-within-the-plotting-region-of-a-ggplot-in-r https://stackoverflow.com/questions/25554548/adding-sub-tables-on-each-panel-of-a-facet-ggplot-in-r?

See Also

function tableGrob as it is used to construct the table.

Other Statistics for adding insets to ggplots: geom_grob, geom_plot

Examples

Run this code
# NOT RUN {
library(dplyr)
library(tibble)

mtcars %>%
  group_by(cyl) %>%
  summarize(wt = mean(wt), mpg = mean(mpg)) %>%
  ungroup() %>%
  mutate(wt = sprintf("%.2f", wt),
         mpg = sprintf("%.1f", mpg)) -> tb

df <- tibble(x = 0.95, y = 0.95, tb = list(tb))

ggplot(mtcars, aes(wt, mpg, colour = factor(cyl))) +
  geom_point() +
  geom_table_npc(data = df, aes(npcx = x, npcy = y, label = tb),
                 hjust = 1, vjust = 1)

# }

Run the code above in your browser using DataLab