Line break in expression of table header?

505 Views Asked by At

I have seen a number of similar questions relating to line breaks in expressions, but I'm still not able to get a table with an expression and line break in the header.

I would like it to say Model R2 on the top line, and Mean (95% CIs) on the second line.

Here is what I've tried. I've removed the % sign which seemed to add additional problems.

library(ggpmisc)
library(tidyverse)

data <- tibble(
  "Mean" = c(1:4),
  "R^2" = c(5:8),
  "Model~R^2~Mean~(CIs)" = c(5:8),
  "atop('Model~R^2',\n,'Mean~(CIs)')"  = c(5:8),
  "Model~R^2,\nMean~(CIs)"  = c(5:8),
  "Model~R^2\nMean~(CIs)"  = c(5:8)
)

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

starwars %>% 
  ggplot(aes(height, mass)) +
  geom_point()+
  geom_table_npc(data = df, aes(label = list(data)),
                 table.theme = ttheme_gtlight( parse = T),
                 npcx = 0.5, npcy = 0.5,  size = 5)
1

There are 1 best solutions below

6
manro On BEST ANSWER

Try this:

library(ggpmisc)
library(tidyverse)

data <- tibble(
    "Mean" = c(1:4),
    "R^2" = c(5:8),
    "over(Model~R^2,~Mean~(CIs))" = c(5:8),
    "atop(Model~R^2,~Mean~(CIs))"  = c(5:8),
    "frac(Model~R^2,~Mean~(CIs))"  = c(5:8),
    "Model~R^2\nMean~(CIs)"  = c(5:8)
)

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

starwars %>% 
    ggplot(aes(height, mass)) +
    geom_point()+
    geom_table_npc(data = df, aes(label = list(data)),
                   table.theme = ttheme_gtlight(parse = T, 
                                                padding = unit(c(0.6, 0.7), "char"), 
                                                base_colour = "grey10"),
                                                npcx = 0.5, npcy = 0.5, size = 5)

Output:

enter image description here


An addition to your comment:

data <- tibble(
    "Mean" = c(1:4),
    "R^2" = c(5:8),
    "atop(Model~R^2,~Mean~('95%'~CIs))" = c(5:8), 
    "bold(atop(Model~R^2,~Mean~('95%'~CIs)))" = c(5:8),
    "bold(atop(Model~R^2,~Mean~(CIs)))" = c(5:8),
    "Model~R^2\nMean~(CIs)"  = c(5:8)
)

enter image description here