How do i make tableGrob accept expression labels

848 views
Skip to first unread message

Herbert Jägle

unread,
Jul 23, 2015, 1:42:00 PM7/23/15
to ggplot2
I just updated to R 3.2.1 and thus to the latest ggplot2 and gridExtra package 2.0.0.
With this version my scripts using expressions in column names do no longer work. A simple example is:

------
df <- data.frame(c('OD', 'OS'), c(1, 2), c(10, 20))
tab.base <- qplot(0:10, 0:10, geom = "blank")
print(tab.base + annotation_custom(grob=tableGrob(df, cols=c('Eye', bquote('N75 [ms]'), expression('N75-P100 ['*mu*'V]')))))
-----

How i can include expressions in column or row names?

Many thanks!
Herbert

Bryan Hanson

unread,
Jul 23, 2015, 2:14:28 PM7/23/15
to Herbert Jägle, ggplot2
There are lots of changes in gtable 2.0 which might be part of the issue.

There are examples here: https://cran.r-project.org/web/packages/gridExtra/vignettes/tableGrob.html which might be helpful.

Bryan

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Herbert Jägle

unread,
Jul 24, 2015, 1:33:11 AM7/24/15
to ggplot2, han...@depauw.edu
Thanks for the link. I updated my code and besides some label issues it works fine.
So may i ask three specific questions?

 -----
library(gridExtra)
library(grid)
d <- head(iris[,1:3])
colnames(d) <- c("plain text", "text [unit]", "text [*mu*V]")
tt <- ttheme_default(colhead=list(fg_params = list(fontface=2, parse=TRUE)))
grid.table(d, theme=tt)
-----

1. The selected fontface is "bold", but as soon as a subscript is added, the fontface changes to plain. Can this be avoided?
2. The "[" is interpreted as starting subscript. How do i escape the "[" character to get brackets in the labels?
3. The label "text [muV]" is intended to be give the same as expression("text [*mu*V]"). How do i get this going?

Thanks,
Herbert

Bryan Hanson

unread,
Jul 24, 2015, 6:51:00 AM7/24/15
to Herbert Jägle, ggplot2
Herbert, just to clarify, the only column header to be interpreted in a special manner is the 3rd one, and you want it to be, verbally, some text followed by square brackets enclosing the symbol for microvolts?  If not, what is *mu*V supposed to typeset as? Bryan

Herbert Jägle

unread,
Jul 24, 2015, 7:13:17 AM7/24/15
to ggplot2, han...@depauw.edu
Hi Bryan.
This is my intention. I want to get column labels like "name [unit]" with the name of a variable and the unit in brackets. The last example should give microvolt within the brackets.

I tried to play around with the character string and did not find the systematics. For example, the second label is interpreted as "text" with subscript "unit", but when i add a tilde before the bracket, it seems no longer interpreted.
Herbert

Bryan Hanson

unread,
Jul 24, 2015, 8:05:48 AM7/24/15
to Herbert Jägle, ggplot2
Alright.  gridExtra / gtable is expecting plotmath and in plotmath you have to escape the bracket by quoting it (found on SO).  So I thought the following would work to escape the brackets, but it doesn’t quite:

colnames(d) <- c("plain text", "text '['unit']'", “tmp text")

So, again using ideas from SO, I did this, which is getting much closer:

cn1 <- "plain text"
cn2 <- expression(paste("text [unit]"))
cn3 <- expression(paste("text [", mu*V, "]",))
colnames(d) <- c(cn1, cn2, cn3)

tt <- ttheme_default(colhead=list(fg_params = list(fontface=2, parse = TRUE)))
grid.table(d, theme=tt)

The remaining problem is the bolding of the expressions.  There might be a way around this, maybe someone else will have an answer.  You might post this partial answer to SO and see what the experts there have to say.  There are several who are very very sharp with plotmath.

There is also a new package latex2exp which I have great hopes for, but I couldn’t make it work in this case (but latex2exp is advertised as ‘beta’)

Bryan

Brian

unread,
Jul 24, 2015, 8:28:33 AM7/24/15
to Bryan Hanson, Herbert Jägle, ggplot2
Howdy,
please try doing everything in expression, which is vectorized.

Like this:
print(tab.base + annotation_custom(grob=tableGrob(df,
cols=expression('Eye', "N75[ms]", N75-P100*'['*mu*'V]'))))

For bold stuff:
print(tab.base + annotation_custom(grob=tableGrob(df,
cols=expression('Eye', bold(N75*"[ms]"), N75-P100*'['*mu*'V]'))))

Hope that helps.

Cheers,
Brian




On 24.07.2015 14:05, Bryan Hanson wrote:
> Alright. gridExtra / gtable is expecting plotmath and in plotmath you
> have to escape the bracket by quoting it (found on SO). So I thought
> the following would work to escape the brackets, but it doesn’t quite:
>
> colnames(d) <- c("plain text", "text '['unit']'", “tmp text")
>
> So, again using ideas from SO, I did this, which is getting much closer:
>
> cn1 <- "plain text"
> cn2 <- expression(paste("text [unit]"))
> cn3 <- expression(paste("text [", mu*V, "]",))
> colnames(d) <- c(cn1, cn2, cn3)
>
> tt <- ttheme_default(colhead=list(fg_params = list(fontface=2, parse =
> TRUE)))
> grid.table(d, theme=tt)
>
> The remaining problem is the bolding of the expressions. There might
> be a way around this, maybe someone else will have an answer. You
> might post this partial answer to SO and see what the experts there
> have to say. There are several who are very very sharp with plotmath.
>
> There is also a new package latex2exp which I have great hopes for,
> but I couldn’t make it work in this case (but latex2exp is advertised
> as ‘beta’)
>
> Bryan
>
>> On Jul 24, 2015, at 7:13 AM, Herbert Jägle <jae...@eye-regensburg.de
>> <mailto:jae...@eye-regensburg.de>> wrote:
>>
>> Hi Bryan.
>> This is my intention. I want to get column labels like "name [unit]"
>> with the name of a variable and the unit in brackets. The last
>> example should give microvolt within the brackets.
>>
>> I tried to play around with the character string and did not find the
>> systematics. For example, the second label is interpreted as "text"
>> with subscript "unit", but when i add a tilde before the bracket, it
>> seems no longer interpreted.
>> Herbert
>>
>>
>> On Friday, July 24, 2015 at 12:51:00 PM UTC+2, Bryan Hanson wrote:
>>
>> Herbert, just to clarify, the only column header to be
>> interpreted in a special manner is the 3rd one, and you want it
>> to be, verbally, some text followed by square brackets enclosing
>> the symbol for microvolts? If not, what is *mu*V supposed to
>> typeset as? Bryan
>>
>>> On Jul 24, 2015, at 1:33 AM, Herbert Jägle
> <mailto:ggplot2+u...@googlegroups.com>.

Herbert Jägle

unread,
Jul 24, 2015, 9:03:00 AM7/24/15
to ggplot2, han...@depauw.edu, zenl...@gmail.com
Brian, your suggestion is almost exactly the way i coded the table so far. But tableGrob in V2.0.0 seems to no longer support expression syntax in cols. At least i was unable to get it working.
The new version also utilizes tthemes parameters to set the parsing True/False.

Thanks Bryan for the code example. It works for me and it is possible to get bold as well (except for the micro-symbol):

colnames(d) <- c(expression(paste(bold("plain text"))), expression(paste(bold("text ["), bold("unit]"))), expression(paste(bold("text ["), bold(mu*V), bold("]"))))

Best wishes,
Herbert

Bryan Hanson

unread,
Jul 24, 2015, 9:21:21 AM7/24/15
to Herbert Jägle, ggplot2, zenl...@gmail.com
Herbert, super, it looks very good w/your changes.  I think I can use this somewhere too.  Bryan
Reply all
Reply to author
Forward
0 new messages