2

I am trying to print an apostrophe for the column name below in a table using tableGrob

"Kendall's~tau"

The end result is that the whole label is italicized without the ~ and tau being interpreted:

colname

How do I correctly specify this?

I don't think it's helpful, but this is the theme that I've specified to tableGrob:

table_theme <-  ttheme_default(
   core = list(fg_params=list(fontsize = 6)),
   colhead = list(fg_params=list(fontsize = 6, parse=TRUE)),
   rowhead = list(fg_params=list(fontsize = 6, parse=TRUE)),
   padding = unit(c(2, 3), "mm"))

The column name is interpreted via plotmath in grDevices -- the standard way of specifying mathematical annotation in figures generated in R.

Again it has nothing to do with how to specify the expression itself, but here is the table constructor:

tableGrob(stats_df,
     theme = table_theme, 
     rows = c("Kendall's~tau"))

Here's a reproducible example:

library(gridExtra)
library(grid)
data(iris)
table_theme <-  ttheme_default(rowhead = list(fg_params=list(parse=TRUE)))
grid.table(head(iris),
       rows = c(letters[c(1:4)], "plotmath~works~omega", "Kendall's~tau"),
       theme = table_theme)

example

saladi
  • 3,103
  • 6
  • 36
  • 61
  • this does not work for me. I do not seem to be able to add an apostrophe! None of the "" '' `` or ~~ separators or escaped separators with a \ in front seem to work. What am I missing? Example: qplot(1:10,1:10)+annotate("text",x=2,y=2,label="Matt's diner~3^2",parse=T) gives: Error in parse(text = text[[i]]) – mattador Jun 23 '21 at 02:01

4 Answers4

1

This works:

library(gridExtra)
library(grid)
data(iris)
table_theme <-  ttheme_default(rowhead = list(fg_params=list(parse=TRUE)))
grid.table(head(iris),
  rows = c(letters[c(1:4)], "plotmath~works~omega", "Kendall's"~tau),
  theme = table_theme)

enter image description here

0

Try with a backslash in your expression like "Kendall\'s~tau". It should work then.

sam
  • 158
  • 7
0

I tried to use apostrophe with expressions to plot in ggplot. In my database is invalid to use ' in expressions, but this worked

expression(paste(u,"'",(t),sep=""))

But this "paste" also cause bad behaviour of the subindex expression expression(U[0]). So to use both together, this one worked

paste(expression(paste("u","'",sep="")),"/U[0]",sep="")

If anyone knows a easier way, I'd be very glad.

Kristian
  • 2,456
  • 8
  • 23
  • 23
0

If your apostrophe is embedded in a longer string, along with special symbols like ~, these solutions will not work. The only thing I've found to work is using regular expression substitution.

#This doesn't work
stringWithApostrophe="Matt's and Louise's diner~...and~also Ben's diner~X^2"
qplot(1:10,1:10)+annotate("text",x=2,y=4,label=stringWithApostrophe,parse=T)

Error: "Error in parse(text = text[[i]]) : :1:5: unexpected string constant"

The problem is the special characters like tilde and apostrophe happening in the same quoted segment. So you have to separate "Matt's" from "Louise's" and "~". Here's the code to do that.

stringWithApostrophe2<-stringr::str_replace_all(pattern = "([^~()*]*'[^~()*']*)",replacement = "\"\\1\"",string=stringWithApostrophe)
qplot(1:10,1:10)+annotate("text",x=2,y=8,hjust=0,label=stringWithApostrophe2,parse=T)

Plots successfully. The final expression that plotmath in R parses correctly is: ""Matt's and Louise's diner"~...and~"also Ben's diner"~X^2"

mattador
  • 421
  • 4
  • 12