2

Can part of the string I pass to parse(text=...) be taken as a literal string? Literal string means it will not try to interpret it.

For instance, I want to have the text "p-value" text in a plot (with the p italicized).

I am doing:

library(ggplot2)
ggplot(data.frame(x=rnorm(500)), aes(x)) + geom_histogram() + geom_text(label='italic(p)-value==0.10', parse=TRUE, x=-2, y=40)

Result:

pvalue

The hyphen has a little too much padding and too big (because it takes it as the subtraction symbol), and it is not showing the number with the full precision I have used.

Can I just tell him to take part of that string as is?

1 Answer 1

3

How about this:

ggplot(data.frame(x=rnorm(500)), aes(x)) + 
    geom_histogram() + 
    annotate("text", label='italic(p)*"-value"=="0.15"', parse=TRUE, x=-2, y=40)

Here we use double quotes to specify character values and use * to place them right next to expressions.

Also note the change to annotate() rather than geom_text(). The latter would print out a 500 labels at the same location since it's tied to the data you specified in the ggplot call.

With set.seed(15), I get

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

I did not know you could use "-quotes within '-quotes. This is great! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.