1

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)
Jeff
  • 57
  • 6

1 Answers1

1

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

manro
  • 3,529
  • 2
  • 9
  • 22
  • This is awesome @manro, thank you! Can you clarify one thing... I've added the "95%" as well, but when I make the line bold just the number 9 is not bold. I can certainly use the third option below, but I'd love to understand why it's happening. data<- tibble( "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), ) – Jeff Aug 19 '22 at 18:29
  • @Jeff I don't understand entirely. Do you want to make all bold except 95%? – manro Aug 19 '22 at 20:06
  • I'd like to make it all bold, but for some reason just the 9 is normal but everything else is bold. I feel like there's some regex thing happening but I can't sort out what. – Jeff Aug 20 '22 at 04:00
  • 1
    @Jeff Look, I added to my answer – manro Aug 20 '22 at 07:37
  • thank you @manro ! Wow it was the difference between using ` and ' – Jeff Aug 20 '22 at 09:12
  • @Jeff Good luck ;-) – manro Aug 20 '22 at 09:18