Learn R Programming

ggpp (version 0.4.3)

geom_table: Inset tables

Description

geom_table and geom_table_npc add data frames as table insets to the base ggplot, using syntax similar to that of geom_text and geom_text_s. In most respects they behave as any other ggplot geometry: a layer con contain multiple tables and faceting works as usual.

Usage

geom_table(
  mapping = NULL,
  data = NULL,
  stat = "identity",
  position = "identity",
  ...,
  nudge_x = 0,
  nudge_y = 0,
  add.segments = TRUE,
  arrow = NULL,
  table.theme = NULL,
  table.rownames = FALSE,
  table.colnames = TRUE,
  table.hjust = 0.5,
  parse = FALSE,
  na.rm = FALSE,
  show.legend = FALSE,
  inherit.aes = FALSE
)

geom_table_npc( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., table.theme = NULL, table.rownames = FALSE, table.colnames = TRUE, table.hjust = 0.5, 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 data set - 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

...

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

nudge_x, nudge_y

Horizontal and vertical adjustments to nudge the starting position of each text label. The units for nudge_x and nudge_y are the same as for the data units on the x-axis and y-axis.

add.segments

logical Display connecting segments or arrows between original positions and displaced ones if both are available.

arrow

specification for arrow heads, as created by arrow

table.theme

NULL, list or function A gridExtra ttheme defintion, or a constructor for a ttheme or NULL for default.

table.rownames, table.colnames

logical flag to enable or disable printing of row names and column names.

table.hjust

numeric Horizontal justification for the core and column headings of the table.

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.

Value

A plot layer instance.

Details

You can modify the alignment of inset tables 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").

You can modify the size of inset tabls with the vp.width and vp.height aesthetics. These can take a number between 0 (smallest possible inset) and 1 (whole plotting area width or height). The default value for for both of these aesthetics is 1/5. Thus, in contrast to geom_text and geom_text_s the size of the insets remains the same relative to the size of the plotting area irrespective of how the plot is rendered. The aspect ratio of insets is preserved and size is adjusted until the whole inset fits within the viewport.

You can modify inset 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"`). There several two special alignments: `"inward"` and `"outward"`. Inward always aligns text towards the center of the plotting area, and outward aligns it away from the center of the plotting area. It tagged with `_mean` or `_median` the mean or median of the data in the panel along the corresponding axis is used as center.

By default this geom uses position_nudge_center which is backwards compatible with position_nudge but provides additional control on the direction of the nudging. In contrast to position_nudge, position_nudge_center and all other position functions defined in packages 'ggpp' and 'ggrepel' keep the original coordinates thus allowing the plotting of connecting segments and arrows.

This geom works only with tibbles as data, as its expects a list of data frames (or tibbles) to be mapped to the label aesthetic. A table is built with function gridExtra::gtable() for each data frame in the list, and formatted according to a table theme or ttheme. The character strings in the data frame can be parsed into R expressions so the inset tables can include maths.

If the argument passed to table.theme is a constructor function (passing its name without parenthesis), the values mapped to size, colour, fill, alpha, and family aesthetics will the passed to this theme constructor for each individual table. In contrast, if a ready constructed ttheme stored as a list object is passed as argument (e.g., by calling the constructor, using constructor name followed by parenthesis), it will be used as is, i.e., mappings to aesthetics such as colour are ignored if present. By default the constructor ttheme_gtdefault is used and colour and fill, are mapped to NA. Mapping these aesthetics to NA triggers the use of the default base_colour of the ttheme. As the table is built with function gridExtra::gtable(), for formatting details, please, consult tableGrob.

The x and y aesthetics determine the position of the whole inset table, similarly to that of a text label, justification is interpreted as indicating the position of the inset table with respect to its x and y coordinates in the data, and angle is used to rotate the inset table as a whole.

In the case of geom_table_npc(), npcx and npcy aesthetics determine the position of the inset table. As for text labels, justification is interpreted as indicating the position of the inset plot with respect to its npcx and npcy coordinates, and angle is used to rotate the plot as a whole.

annotate cannot be used with geom = "table". Use annotate (automatic override unless 'ggpp' is not attached) as redefined in 'ggpp' when adding inset plots as annotations (automatic unless 'ggpp' is not attached).

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

Formatting of tables stat_fmt_table, ttheme_gtdefault, ttheme_set, tableGrob.

Other geometries adding layers with insets: 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 = 5.45, y = 34, tb = list(tb))

# using defaults
ggplot(mtcars, aes(wt, mpg, colour = factor(cyl))) +
  geom_point() +
  geom_table(data = df, aes(x = x, y = y, label = tb))

ggplot(mtcars, aes(wt, mpg, colour = factor(cyl))) +
  geom_point() +
  geom_table(data = df, aes(x = x, y = y, label = tb),
             table.rownames = TRUE, table.theme = ttheme_gtstripes)

# settings aesthetics to constants
ggplot(mtcars, aes(wt, mpg, colour = factor(cyl))) +
  geom_point() +
  geom_table(data = df, aes(x = x, y = y, label = tb),
             color = "red", fill = "#FFCCCC", family = "serif", size = 5,
             angle = 90, vjust = 0)

# passing a theme constructor as argument
ggplot(mtcars, aes(wt, mpg, colour = factor(cyl))) +
  geom_point() +
  geom_table(data = df, aes(x = x, y = y, label = tb),
             table.theme = ttheme_gtminimal) +
  theme_classic()

df2 <- tibble(x = 5.45,
              y = c(34, 29, 24),
              x1 = c(2.29, 3.12, 4.00),
              y1 = c(26.6, 19.7, 15.1),
              cyl = c(4, 6, 8),
              tb = list(tb[1, 1:3], tb[2, 1:3], tb[3, 1:3]))

# mapped aesthetics
ggplot(data = mtcars, mapping = aes(wt, mpg, color = factor(cyl))) +
  geom_point() +
  geom_table(data = df2,
             inherit.aes = TRUE,
             mapping = aes(x = x, y = y, label = tb))

# nudging and segments
ggplot(data = mtcars, mapping = aes(wt, mpg, color = factor(cyl))) +
  geom_point(show.legend = FALSE) +
  geom_table(data = df2,
             inherit.aes = TRUE,
             nudge_x = 0.7, nudge_y = 3,
             vjust = 0.5, hjust = 0.5,
             arrow = arrow(length = unit(0.5, "lines")),
             mapping = aes(x = x1, y = y1, label = tb)) +
  theme_classic()

# Using native plot coordinates instead of data coordinates
dfnpc <- tibble(x = 0.95, y = 0.95, tb = list(tb))

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

# }

Run the code above in your browser using DataLab