3

This question uses 'ggpmisc' version 0.3.4 or earlier, see answer below for version 0.3.5 or later.

I like the newish geom_table_npc in ggpmisc which provides a simple way to add a table to a ggplot and works particularly well with transformed x-axis where trying to set location can be frustrating if plotting bounds may change.

I'm wondering if its possible to change the table theme? I can change the text size using size = 1.8 but then the padding of the cells looks strange which is what I'd like to change. May also want to change the colour.

I think it is using gridExtra::ttheme_default so I tried setting this in case it gets passed through in the ... but it just ignores it.

  library(ggplot2)
  library(ggpmisc)
  library(gridExtra)

  testdat <- data.frame(x = 1/2:50, y = 50:2)

annotatedf <- data.frame(cat = letters[1:3], val = 1:3)

t1plt <- ggplot(testdat, aes(x = x, y = y)) +
           geom_point() + 
           scale_x_log10()


t1plt + 
  geom_table_npc(data = annotatedf, 
                 label = list(annotatedf),
                 npcx = 0.05, npcy = 0.95)

t1plt + 
  geom_table_npc(data = annotatedf, 
                 label = list(annotatedf),
                 npcx = 0.05, npcy = 0.95, size = 1.8)


t1plt + 
  geom_table_npc(data = annotatedf, 
                 label = list(annotatedf),
                 npcx = 0.05, npcy = 0.95, size = 1.8,
                 theme = ttheme_default(padding = unit(c(1, 1), "mm")))
#> Warning: Ignoring unknown parameters: theme

Created on 2020-02-18 by the reprex package (v0.3.0)

Pedro J. Aphalo
  • 5,796
  • 1
  • 22
  • 23
Sarah
  • 3,022
  • 1
  • 19
  • 40

1 Answers1

2

Theme support for inset tables is now implemented in the development version od 'ggpmisc' available in the git repository at Bitbucket and will be released as version 0.3.5 soon.

library(ggplot2)
library(ggpmisc)
library(gridExtra)

testdat <- data.frame(x = 1/2:50, y = 50:2)

annotatedf <- data.frame(cat = letters[1:3], val = 1:3)

t1plt <- ggplot(testdat, aes(x = x, y = y)) + 
  geom_point() + 
  scale_x_log10()

The plot produced by the code below, using defaults is only slightly different in the new version of 'ggpmisc' compared to the earlier one, with very slightly less padding and right alignment instead of center alignment. Explicit setting of hjust and vjust is not needed as their default is "inwards".

t1plt + 
  geom_table_npc(data = annotatedf, 
                 label = list(annotatedf),
                 npcx = 0.05, npcy = 0.95)

enter image description here

The default output no longer uses excessive padding when the size aesthetic is mapped to a small value as padding is in the new version proportional to character size.

t1plt +
  geom_table_npc(data = annotatedf, 
                 label = list(annotatedf),
                 npcx = 0.05, npcy = 0.95, size = 1.8)

enter image description here

As the questions asks for tighter padding than the default, and how to use a theme, we show two ways of doing this. In the code below we can pass a ttheme as a list object created with a constructor but this in the current version prevents mapping of aesthetics, requiring in this case us to pass the size to the theme constructor.

t1plt +
  geom_table_npc(data = annotatedf, 
                 label = list(annotatedf),
                 npcx = 0.05, npcy = 0.95,
                 table.theme = ttheme_default(base_size = 7, 
                                              padding = unit(c(1, 1), "mm")))

enter image description here

The code below shows the use of a theme constructor. If we define a customized theme constructor as a wrapper of gridExtra::ttheme_default() or one of the themes from 'ggpmisc' package, then mapping of aesthetics remains active.

ttheme_custom <- function(base_size = 12, 
                          base_colour = "black", 
                          base_family = "",
                          parse = FALSE, 
                          padding = unit(c(1, 1), "mm"), 
                          ...) {
  gridExtra::ttheme_default(base_size = base_size,
                            base_colour = base_colour,
                            base_family = base_family,
                            parse = parse,
                            padding = padding,
                            ...)
}

t1plt +
  geom_table_npc(data = annotatedf, 
                 label = list(annotatedf),
                 npcx = 0.05, npcy = 0.95, size = 1.8,
                 table.theme = ttheme_custom)

enter image description here

The 'ggpmisc' package provides several table-theme constructors, use one of them is shown below.

t1plt +
  geom_table_npc(data = annotatedf, 
                 label = list(annotatedf),
                 npcx = 0.05, npcy = 0.95,
                 table.theme = ttheme_gtlight)

enter image description here

Pedro J. Aphalo
  • 5,796
  • 1
  • 22
  • 23