Spacing in axis label when using expression(paste(...)) - r

Consider the following example:
plot(c(2,4,6)~c(1,2,3),xlab="x",
ylab=expression(paste('flux (g ',CO[2]~m^{-2}~h^{-1},')')))
Obviously I want a full space between "g" and "CO", but for some reason I get a smaller (with some labels even zero) space in the graph label.
The problem is even more obvious, if I do it like this:
plot(c(2,4,6)~c(1,2,3),xlab="x",
ylab=expression(paste('flux (g C',O[2]~m^{-2}~h^{-1},')')))
Am I doing something wrong? Is there a way to fix the spacing or even a better way to create labels with lots of sub/superscripts and greek letters?

In all likelihood you are getting a typographically correct "space", in the font your OS uses for non-serif display. You can change fonts or you can insert blank space that is sufficient to hold a particular character string with plotmath phantom():
plot(c(2,4,6)~c(1,2,3),xlab="x",
ylab=expression(paste('flux',phantom(x),'(g ',CO[2]~m^{-2}~h^{-1},')')))
Or as #baptiste points out this can be done without plomath paste using ordinary plotmath separators because a tilde in a true R expression gets handled as a "space":
ylab=expression(flux*phantom(x)*(g~CO[2]~m^{-2}~h^{-1})))

Related

R ylab line modified with italic

I have trouble making my plots looking similar because my labels (both xlab and ylab) move if I use italic. Consider the following short example:
plot(1:10,cex.lab=1.25,ylab="p-value")
plot(1:10,cex.lab=1.25,ylab=expression(italic(p)~"-value"))
The problem is that "p-value" is slightly nearest from the axis than "p-value". I believe it's because the p tail defines the writing line which is considered different without italic. How can I fix that easily?
I frequently use both strings and expressions for my plots and that would be complicated to use mtext (with the line argument) to manage each label for each plot.
I found a workaround playing with unicode characters.
p is \U1D631 and P is \U1D617
plot(1:10,cex.lab=1.25,ylab="p-value")
plot(1:10,cex.lab=1.25,ylab="\U1D631-value")
This solved my problem.

Resizing parentheses in R expression

I would like my axis label to read something like
(m²)
with the height of the parentheses a little larger to match the superscript 2, but the parentheses nonetheless inline.
However, I either get parentheses that are too small, via something like
parse(text='group("(",m^{2},")")')
which yields
or parentheses that are too big and out-of-line, via something like
parse(text='bgroup("(",m^{2},")")')
which yields
Can I not do this in R?
Update:
As per the suggestion of user #42-, I've also tried scriptstyle. However, this makes the parenthesized text much smaller. It's particularly noticeable with neighboring text. For instance,
parse(text='Area~~(scriptstyle(m^{2}))')
would yield
I realize the workaround would be using something like
parse(text='scriptstyle(Area~~(m^{2}))')
which yields
and then manually upscaling font size to compensate, but is there a fix or alternative that won't require this kind of guesstimation?
Do you want;
plot(1,1, main=parse(text='scriptstyle( bgroup("(",m^{2},")"))') )
Or perhaps:
plot(1,1, main=parse(text='"("*scriptstyle(m^{2})*")"') )
A third altermative is to use "phantom()" which will reserve space equivalent to its argument. I found by experimentation that you could get parentheses that were not so "descend-ful" using:
plot(1,1, main=parse(text='"("*phantom(m^2)*")"') ,cex.main=1.6)
And then filling in the gap with:
title(main=expression(m^2) )
And it further twerking is needed, one can adjust the level of text relative to the "box" in units of text lines with title(main= <expression> , line= 2.5)
plot(1,1) ; title( main=expression(Area(phantom(" "))) ,cex.main=1.5, line=1.5)
title(main=expression(phantom('Area(')*m^2) ,line=1.5)

Both custom tickmarks AND custom fontface in ggplot2

Apologies if this is straightforward or has already been answered, but I've searched high and low and haven't been able to find a solution anywhere.
I'm using ggplot2, trying to (1) specify custom tick marks to an axis which involve subscripts, and (2) trying to specify that some of these tick marks should be bold and others italic. I know how to do (1) and (2) separately, but when I try to do both together, only (1) succeeds.
y.labels<-c(expression(A[Subscript]),expression(B^Superscript))
y.face<-c("bold","italic")
testdf<-data.frame(x=rnorm(100),y=1:2)
Bold/italics don't appear when I supply custom labels:
ggplot(testdf,aes(x=x,y=y))+geom_point()+
scale_y_discrete(label=y.labels)
theme(axis.text.y=element_text(face=y.face))
But when I let the labels take their default values, bold/italics do appear:
ggplot(testdf,aes(x=x,y=y))+geom_point()+
theme(axis.text.y=element_text(face=y.face))
As far as I can tell, the problem doesn't have anything to do with subscripting/superscripting,
even though that is my aim, here (and which is why I'm passing the tick marks directly, rather than creating a factor variable with the desired labels, which does work). This doesn't work, either:
ggplot(testdf,aes(x=x,y=y))+geom_point()+
scale_y_discrete(label=c("Custom1","Custom2"))+
theme(axis.text.y=element_text(face=y.face))
If anyone can explain what's going on, here, and provide a solution whereby I can do both, I'd be much obliged. Thanks.
plotmath doesn't honour fontface, you want to use bold() or italic() in your expressions,
y.labels<-c(expression(bold(A)[Subscript]),expression(italic(B)^Superscript))
ggplot(testdf,aes(x=x,y=y))+geom_point()+
scale_y_discrete(label=y.labels)

Add less than symbol, "<", to a ggplot via geom_text in R

The short version: How would I make this contrived code plot a proper greek beta character AND the rest of the label string, with the space and the less than character and the numbers formatted as typed?
library("ggplot2")
df<-data.frame(a=1:15,b=6:20)
ggplot(data=df, aes(x=a,y=b)) + geom_point() +
geom_text(x=5,y=4,label="beta=1.00 p<0.0001", parse=TRUE)
If I leave out the ", parse=TRUE" argument, the string works great, but won't give me a real greek beta. If I leave it in, all hell breaks loose.
The long version (and why I can't find a duplicate): I finally discovered in How to use Greek symbols in ggplot2? that methods for placing greek characters on a ggplot depends upon where you're putting them. Since I am using geom_text to force strings of text onto the body of the plot, my attempts to use expression(beta) have failed and I recently started using the parse argument mentioned above. I came across How can I add alpha-numeric AND greek characters to geom_text() in ggplot?, which I thought was my answer, but fixing the "space" resulted in extra parentheses popping in, "=" replaced by a comma, and loss of formatting for all of my previously formatted text numerics.
A link to a guide for using the parse argument, and how to make sense of what seems to me to be completely unintuitive would be very helpful to me, and probably future R users ending up here. Many searches have been fruitless.
This should do it:
g <- ggplot(data=df, aes(x=a,y=b)) + geom_point()
g + annotate("text", x=5,y=4,parse=TRUE, label="beta==1.00 * ' p<0.0001'")
It's a matter of splitting the label with single quotes to the side of the whitespace and connecting two label bits with *. You also need the == for equals to.

R: bold numbers in plot annotation created by plotmath

I build graphics in which I use regular and bold font for titles and/or labels. They contain symbols, text and numbers which I combine using ?plotmath.
I noticed that using bold within the expression changes only character strings to bold face, but not numbers which are part of the expression. See this minimal example:
x <- y <- 1:10
plot(x, y, xlab=bquote(bold(foo[bar]^2==.(mean(x)))))
In this example, there is a numeric superscript on the LHS, and an evaluated expression on the RHS of the equality sign. Both of which are not printed in bold face as suggested by using bold. When I change these to character strings, the plot shows them in bold.
plot(x, y, xlab=bquote(bold(foo[bar]^"2"==.(as.character(mean(x))))))
Now, for me it is not always desirable to work with character input in these expressions. I would like to find a way of setting the numeric values in bold without converting them to character strings. (I'm also not sure what to expect of the equality sign.)
Additionally, I use Cairo to select specific fonts for my graphics.
In my actual graphics, where I also plot greek letters, I use it to set the "symbol" fonts to bold.
By setting the "regular" font to bold, I could do the same for numbers, but that would affect all remaining text as well.
Therefore, I would appreciate if there is a solution that doesn't require to change the default font in the Cairo device.
EDIT:
As mentioned in the comments, plotmath does not apply bold to numeric constants as in the example above. I would still like to see if there is a solution that allows to set numbers in lables/titles to bold without changing the rest of the plot (as for example Cairo would).
Thanks!

Resources