annotate and expression

2,075 views
Skip to first unread message

Johannes Radinger

unread,
Nov 14, 2011, 10:43:57 AM11/14/11
to ggp...@googlegroups.com
Hi,

I'd like to follow up a already discussed topic:


>> For this example:
>>
> library(ggplot2)
>> p <- ggplot(mtcars, aes(x = wt, y=mpg)) + geom_point()
>> p <- p + scale_x_continuous(expression(beta))
>> p
>>
>> How can I include text with the expression. For example, "level of
>> \beta". Also, how can I increase
>Have a look at ?plotmath - you want something like
>expression(paste("Level of ", beta))

I also want to use an expression but in my case in the annotation.
But the text disappears when I want to use an expression.
I am working in the dev-mode ggplot2. Here a short example:

x <-c(1,2,3,4)
y <- c(1,2,3,4)
df <-data.frame(x,y)

plot <- ggplot(df, aes(x=x,y=y))+
geom_point()+
annotate("text", 2, 3, label=expression(beta))

What I am doing wrong?

In real I would like to have following two labels:
1) Flow [m^3/sec)
2) Slope (%o) #per mille symbol

I think I also have to use for that the paste function.

/johannes

--
NEU: FreePhone - 0ct/min Handyspartarif mit Geld-zurück-Garantie!
Jetzt informieren: http://www.gmx.net/de/go/freephone

Kohske Takahashi

unread,
Nov 14, 2011, 12:13:22 PM11/14/11
to Johannes Radinger, ggp...@googlegroups.com
hi,

pass character to annotate and set parse = TRUE like this:

plot <- ggplot(df, aes(x=x,y=y))+
               geom_point()+

               annotate("text", 2, 3, label="beta", parse = TRUE)

--
Kohske Takahashi <takahash...@gmail.com>

Research Center for Advanced Science and Technology,
The University of  Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html

2011/11/15 Johannes Radinger <JRad...@gmx.at>:

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

Johannes Radinger

unread,
Nov 15, 2011, 8:44:37 AM11/15/11
to ggp...@googlegroups.com
Hi,


Although I realized now that I have to use parse=TRUE in annotate
to display mathematical expression, I still don't know how to
get my two labels correctly displayed.

I want to display following, but I don't know what the
exact expression (plothmath) is:

1) "Flow [m^3/sec]"
2) "Slope [%o]" # the per mille symbol


Here a minimal example:

x <-c(1,2,3,4)
y <- c(1,2,3,4)
df <-data.frame(x,y)

plot <- ggplot(df, aes(x=x,y=y))+
geom_point()+

annotate("text", 2, 3, label="Flow [m^3/sec]",parse=TRUE)+
annotate("text", 2, 4, label="Slope [permille]",parse=TRUE))

Hopefully someone can help me!

Thank you.

best regards,
Johannes


-------- Original-Nachricht --------
> Datum: Tue, 15 Nov 2011 02:13:22 +0900
> Von: Kohske Takahashi <takahash...@gmail.com>
> An: Johannes Radinger <JRad...@gmx.at>
> CC: ggp...@googlegroups.com
> Betreff: Re: annotate and expression

Dennis Murphy

unread,
Nov 15, 2011, 1:53:36 PM11/15/11
to Johannes Radinger, ggp...@googlegroups.com
Here's how you can get the first expression; as for the second, there
is no per mille operator (0/00) in plotmath, so you end up stuck with
the definition.

ggplot(df, aes(x=x,y=y))+
geom_point()+
annotate('text', x = 2, y = 3, label = "'Flow [m'^3*'/sec]'",
parse = TRUE) +
annotate('text', x = 2, y = 3.5, label = "'Slope [10'^-3*']'",
parse = TRUE)

or replace the annotate with the pure text label

annotate('text', x = 2, y = 3.5, label = "Slope/1000")

Note that [] is the subscript operator in plotmath so you need to be a
little careful when using brackets as text tokens.

HTH,
Dennis

Reply all
Reply to author
Forward
0 new messages