R/ggplot2: use literal strings on parse() - r

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:
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?

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

Related

add symbol to just one axis label in R [duplicate]

I have a plot which is generated thus:
ggplot(dt.2, aes(x=AgeGroup, y=Prevalence)) +
geom_errorbar(aes(ymin=lower, ymax=upper), colour="black", width=.2) +
geom_point(size=2, colour="Red")
I control the x axis labels like this:
scale_x_discrete(labels=c("0-29","30-49","50-64","65-79",">80","All")) +
This works but I need to change the ">80" label to "≥80".
However "≥80" is displayed as "=80".
How can I display the greater than or equal sign ?
An alternative to using expressions is Unicode characters, in this case Unicode Character 'GREATER-THAN OR EQUAL TO' (U+2265). Copying #mnel's example
.d <- data.frame(a = letters[1:6], y = 1:6)
ggplot(.d, aes(x=a,y=y)) + geom_point() +
scale_x_discrete(labels = c(letters[1:5], "\u2265 80"))
Unicode is a good alternative if you have trouble remembering the complicated expression syntax or if you need linebreaks, which expressions don't allow. As a downside, whether specific Unicode characters work at all depends on your graphics device and font of choice.
You can pass an expression (including phantom(...) to fake a leading >= within
the label argument to scale_x_discrete(...)
for example
.d <- data.frame(a = letters[1:6], y = 1:6)
ggplot(.d, aes(x=a,y=y)) + geom_point() +
scale_x_discrete(labels = c(letters[1:5], expression(phantom(x) >=80))
See ?plotmath for more details on creating mathematical expressions and
this related SO question and answer
plot(5, ylab=expression("T ">="5"))
You can use
expression("">=80)
So your full axis label like would look like:
scale_x_discrete(labels=c("0-29","30-49","50-64","65-79",expression("">=80),"All")) +
I have had trouble exporting plots when using unicode, but the expression function is more consistent.

Notation for fractions (divides by or over) in R and ggplot2

I am trying to place one word over another for a title. I mean literally over, not just a 'divides by' sign one value over another (or word) to save space. It is supposed to mean divides by and there needs to be a line.
I cannot find how to do this, can you help?
Thanks,
Chris
You can create fractions in a title with frac():
p <- ggplot(mtcars, aes(x = mpg, y = disp)) + geom_point()
p + ggtitle(expression(paste("This is a fraction: ", frac(foo, bar))))
Note that the whole title must be put inside expression(). Alternatively, you can also use over() to do the same. The following example also shows that you can create other mathematical expressions in a similar fashion:
p + ggtitle(expression(paste("This is a fraction: ", over(3 * alpha, sum(b[i], i==1, N)))))
For more information, read ?plotmath.

"≥" or "≤" symbol in ggplot2 legend [duplicate]

I have a plot which is generated thus:
ggplot(dt.2, aes(x=AgeGroup, y=Prevalence)) +
geom_errorbar(aes(ymin=lower, ymax=upper), colour="black", width=.2) +
geom_point(size=2, colour="Red")
I control the x axis labels like this:
scale_x_discrete(labels=c("0-29","30-49","50-64","65-79",">80","All")) +
This works but I need to change the ">80" label to "≥80".
However "≥80" is displayed as "=80".
How can I display the greater than or equal sign ?
An alternative to using expressions is Unicode characters, in this case Unicode Character 'GREATER-THAN OR EQUAL TO' (U+2265). Copying #mnel's example
.d <- data.frame(a = letters[1:6], y = 1:6)
ggplot(.d, aes(x=a,y=y)) + geom_point() +
scale_x_discrete(labels = c(letters[1:5], "\u2265 80"))
Unicode is a good alternative if you have trouble remembering the complicated expression syntax or if you need linebreaks, which expressions don't allow. As a downside, whether specific Unicode characters work at all depends on your graphics device and font of choice.
You can pass an expression (including phantom(...) to fake a leading >= within
the label argument to scale_x_discrete(...)
for example
.d <- data.frame(a = letters[1:6], y = 1:6)
ggplot(.d, aes(x=a,y=y)) + geom_point() +
scale_x_discrete(labels = c(letters[1:5], expression(phantom(x) >=80))
See ?plotmath for more details on creating mathematical expressions and
this related SO question and answer
plot(5, ylab=expression("T ">="5"))
You can use
expression("">=80)
So your full axis label like would look like:
scale_x_discrete(labels=c("0-29","30-49","50-64","65-79",expression("">=80),"All")) +
I have had trouble exporting plots when using unicode, but the expression function is more consistent.

R - parse function doesn't work as expected

In the following example (please notice differences in y-axis labels) I use a variable to fill in an axis label in ggplot2. Interestingly ~ produces much larger spaces, and extra spaces show up around an enlarged -.
library(ggplot2)
#LabelY <- "Miles per Gallon-Car"
LabelY <- parse(text="Miles~per~Gallon-Car")
a <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() +
ggtitle("Fuel Efficiency of 32 Cars") +
xlab(LabelY) + ylab(LabelY) +
theme(text=element_text(size=16))
print(a)
I am using parse because it allows me to use more complex examples including atop and greek letters.
Is there a way I can make use of parse to import complex strings while also preserving the desired "less spread out" appearance of the content?
It looks like enclosing the hyphenated term with backticks will allow you to keep the hyphen instead of turning it in a dash.
Here I put the new hyphenated version of the axis label on the x axis and leave the y axis as the original for comparison.
LabelY <- parse(text="Miles~per~Gallon-Car")
LabelY2 <- parse(text="Miles~per~`Gallon-Car`")
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() +
ggtitle("Fuel Efficiency of 32 Cars") +
xlab(parse(text = LabelY2)) + ylab(LabelY) +
theme(text=element_text(size=16))
As you pointed out in the comments, you can also use a curly bracket and single quote combination around the hyphenated term to get the same effect.
parse(text="Miles~per~{'Gallon-Car'}")

Keeping trailing zeroes with plotmath

I'm using annotate() to overlay text on one of my ggplot2 plots. I'm using the option parse=T because I need to use the Greek letter rho. I'd like the text to say = -0.50, but the trailing zero gets clipped and I get -0.5 instead.
Here's an example:
library(ggplot2)
x<-rnorm(50)
y<-rnorm(50)
df<-data.frame(x,y)
ggplot(data=df,aes(x=x,y=y))+
geom_point()+
annotate(geom="text",x=1,y=1,label="rho==-0.50",parse=T)
Does anyone know how I can get the last 0 to show up? I thought I could use paste() like this:
annotate(geom="text",x=1,y=1,label=paste("rho==-0.5","0",sep=""),parse=T)
but then I get the error:
Error in parse(text = lab) : <text>:1:11: unexpected numeric constant
1: rho==-0.5 0
^
It is an plotmath expression parsing problem; it's not ggplot2 related.
What you can do is ensure that 0.50 is interpreted as a character string, not a numeric value which will be rounded:
ggplot(data=df, aes(x=x, y=y)) +
geom_point() +
annotate(geom="text", x=1, y=1, label="rho=='-0.50'", parse=T)
You would get the same behavior using base:
plot(1, type ='n')
text(1.2, 1.2, expression(rho=='-0.50'))
text(0.8, 0.8, expression(rho==0.50))
If you want a more general approach, try something like
sprintf('rho == "%1.2f"',0.5)
There is an r-help thread related to this issue.

Resources