facet_grid using "label_parsed" and expression

869 views
Skip to first unread message

Herbert Jägle

unread,
Jun 7, 2012, 4:06:23 PM6/7/12
to ggp...@googlegroups.com
I have two questions.
1. If i use labeller=label_parsed like for plot q, the numbers are reformatted with a format like "%.3f", so the 0.0002 will show as 2e-04. How can i set the number formatting?
2. I want to use label_parsed to use an expression including superscript characters. The label should show like "0.01 cd.s/m2" with a superscript 2.
i tried the following to get an expression

expression(fact_str* ' cd.s/m' ^ '2')

but how do i get fact_str substituted with the actual string and how do i get the expression into facet_grid?

Thanks,
Herbert

--------
df.d <- data.frame(x=rnorm(100), y=rnorm(100), fact=rep(c(0.01,0.0002), 50))
df.d$fact_str <- sprintf('%.4f', df.d$fact)
df.d$fact_int <- factor(df.d$fact, levels=unique(df.d$fact), labels=unique(df.d$fact_str), ordered=TRUE)

p <- ggplot(df.d) +
        geom_point(aes(x, y)) +
        facet_grid(fact_int ~ .)
print(p)

q <- ggplot(df.d) +
        geom_point(aes(x, y)) +
        facet_grid(fact_int ~ ., labeller='label_parsed')
print(q)

Brian Diggs

unread,
Jun 7, 2012, 6:39:47 PM6/7/12
to Herbert Jägle, ggplot2
On 6/7/2012 1:06 PM, Herbert J�gle wrote:
> I have two questions.
> 1. If i use labeller=label_parsed like for plot q, the numbers are
> reformatted with a format like "%.3f", so the 0.0002 will show as 2e-04.
> How can i set the number formatting?
> 2. I want to use label_parsed to use an expression including superscript
> characters. The label should show like "0.01 cd.s/m2" with a superscript 2.
> i tried the following to get an expression
>
> expression(fact_str* ' cd.s/m' ^ '2')
>
> but how do i get fact_str substituted with the actual string and how do i
> get the expression into facet_grid?

I believe what you want is

labeller=label_bquote(expr = (.x)* ' cd.s/m' ^ '2')

I'm not sure about the first request.

See also https://github.com/hadley/ggplot2/wiki/labeller

> Thanks,
> Herbert
>
> --------
> df.d<- data.frame(x=rnorm(100), y=rnorm(100), fact=rep(c(0.01,0.0002), 50))
> df.d$fact_str<- sprintf('%.4f', df.d$fact)
> df.d$fact_int<- factor(df.d$fact, levels=unique(df.d$fact),
> labels=unique(df.d$fact_str), ordered=TRUE)
>
> p<- ggplot(df.d) +
> geom_point(aes(x, y)) +
> facet_grid(fact_int ~ .)
> print(p)
>
> q<- ggplot(df.d) +
> geom_point(aes(x, y)) +
> facet_grid(fact_int ~ ., labeller='label_parsed')
> print(q)
>


--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University

Herbert Jägle

unread,
Jun 9, 2012, 11:22:57 AM6/9/12
to ggp...@googlegroups.com, Herbert Jägle
Thanks for pointing to label_bquote. It`s close to what i want if i omit the "expr=". Otherwise x will not be replaced.

However, the expression is used for all variables. Is it possible to provide an expression for every variable?

On Friday, June 8, 2012 12:39:47 AM UTC+2, Brian Diggs wrote:

Dennis Murphy

unread,
Jun 9, 2012, 1:21:47 PM6/9/12
to Herbert Jägle, ggp...@googlegroups.com, Herbert Jägle
Hi:

Here's a Q & D way to get the labels you were looking for in version
0.9.1. As far as the how the values are printed, you can change the
scipen global R option; this might be a bit of overkill, but I used

options(scipen = 3)

That should be more than enough to take care of the scientific
notation problem. Then, create yet another version of the factor:

df.d$fact_exp <- factor(df.d$fact_str, levels = unique(df.d$fact_str),
labels = sapply(unique(df.d$fact_str),
function(x) paste(x, 'cd.s^2', sep = '*')))
ggplot(df.d) + geom_point(aes(x = x, y = y)) +
facet_grid(fact_exp ~ ., labeller = label_parsed)

Does that work for you (0.01 and 0.0002)?

HTH,
Dennis
> --
> 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

Herbert Jägle

unread,
Jun 12, 2012, 12:36:10 PM6/12/12
to ggp...@googlegroups.com
Thanks Denis, this is almost what i am looking for.

Almost, because i would like to insert a blank into the text (like: ' cd.s^2'). A leading blank gets removed and a blank inside the text ends up in an error.
Is it possible to protect a blank within the text?

Thanks,
Herbert
> To unsubscribe: email ggplot2+unsubscribe@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
> To unsubscribe: email ggplot2+unsubscribe@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
> To unsubscribe: email ggplot2+unsubscribe@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
> To unsubscribe: email ggplot2+unsubscribe@googlegroups.com
> More options: http://groups.google.com/group/ggplot2

Brandon Hurr

unread,
Jun 12, 2012, 12:46:23 PM6/12/12
to Herbert Jägle, ggp...@googlegroups.com
I think you have to put in a ~ or something to get a space character in an expression.

Reply all
Reply to author
Forward
0 new messages