math notation on ggplot2 axis labels - r

I want to add math notation to a ggplot2 axis using latex2exp, but I run into problems because scale_x_discrete() only accepts new label names when they are between quotation marks which renders the TeX() function, as used in the example below, as text. How can I both change the name of the labels while at the same time include math notation?
p<-ggplot(data=x, aes(x=y, y=x)) +
geom_errorbar(aes(ymin=x-ci, ymax=x+ci)) +
scale_x_discrete(breaks=c("label1","label2"),
labels=c("TeX('$\\alpha^\\beta$')","newlabel2"))
p

Checkout this vignette from the package creators:
https://cran.r-project.org/web/packages/latex2exp/vignettes/using-latex2exp.html
scale_color_discrete(labels=lapply(sprintf('$\\alpha = %d$', alpha), TeX))
For your code:
p<-ggplot(data=x, aes(x=y, y=x)) +
geom_errorbar(aes(ymin=x-ci, ymax=x+ci)) +
scale_x_discrete(breaks=c("label1","label2"),
labels = lapply(sprintf('$\\alpha^\\beta$'), TeX)
p
The other option, if your math notation is not very complicated, is to use bquote and UTF codes:
mn <- bquote("\u03B1 \u03B2")
labels=c(mn, "newlabel2")

Related

ggplot2: How to use variables inside expression for labeling axis? [duplicate]

I'm writing a wrapper function around ggplot2 and having difficulty with one of the string arguments passed. Here's the sample code
myPlot <- function(tim, labx){
ggplot(subset(dat,TIME=tim), aes(x=WT, y=Var))+
geom_point(size=2)+
facet_wrap(~Dose)+
scale_x-continuous(expression(bold("Predicted"~labx~"Concentration ("*mu*"g/mL)")))
}
When I say myplot(100, "Week3"), my x-axis label is showing as "Predicted labx Concentration (µg/mL)" instead of "Predicted Week3 Concentration (µg/mL)". How do I fix this?
One solution is to use bquote() instead of expression(), and use .() inside of bquote to evaluate character (string) variables.
Below is a fully reproducible example of the issue.
library(ggplot2)
labx = "Week3"
p = ggplot(data.frame(x=1:5, y=1:5), aes(x, y)) +
geom_point() +
xlab(bquote(bold(Predicted~.(labx)~Concentration~(mu*g/mL))))
p

boldface of labels containing an expression with lower or equal symbol

I need to render in boldface the labels of the legend of a graph. One of the labels is an expression containing a "lower or equal" sign.
This is where I started from:
library(ggplot2)
df <- data.frame(x=factor(rep(0:1, 10)), y=rnorm(10), z=factor(rep(0:1, 10)))
ggplot(df, aes(x, y, shape=z)) +
geom_point() +
scale_shape_discrete(labels=c("Age > 65", expression(Age <= 65))) +
theme(legend.text=element_text(face="bold"))
In this way, the first label is bold, but the second is not. Following the suggestion here I tried to use plotmath bold():
library(ggplot2)
df <- data.frame(x=factor(rep(0:1, 10)), y=rnorm(10), z=factor(rep(0:1, 10)))
ggplot(aes(x, y, shape=z)) +
geom_point() +
scale_shape_discrete(labels=c("Age > 65", expression(bold(Age <= 65)))) +
theme(legend.text=element_text(face="bold"))
The label is rendered in bold only up to the "<=" sign. I have also tried to put the second part of the string within bold():
expression(bold(Age bold(<= 65)))
but to no avail. Any help is appreciated.
Tucked away in the plotmath documentation is the following:
Note that bold, italic and bolditalic do not apply to symbols, and hence not to the Greek symbols such as mu which are displayed in the symbol font. They also do not apply to numeric constants.
Instead, a suggested approach is to use unicode (assuming font and device support) which, in this case, means we can dispense with plotmath altogether.
ggplot(df, aes(x, y, shape=z)) +
geom_point() +
scale_shape_discrete(labels=c("Age > 65", "Age \U2264 65")) +
theme(legend.text=element_text(face="bold"))
While it is a little overkill for this particular issue, package ggtext makes complicated labels in ggplot2 a lot easier. It allows the use of Markdown and HTML syntax for text rendering.
Here's one way to write your legend text labels, using Markdown's ** for bolding and HTML's ≤ for the symbol.
library(ggtext)
ggplot(df, aes(x, y, shape=z)) +
geom_point() +
scale_shape_discrete(labels=c("**Age > 65**", "**Age ≤ 65**")) +
theme(legend.text=element_markdown())
(I'm on a Windows machine, and the default windows graphics device can have problems with adding extra spaces to symbols. Using ragg::agg_png() avoids the issue when saving plots, but also the next version of RStudio will allow you to change the graphics backend to bypass these problems.)

ggplot facet_grid label superscript

I am having trouble with putting subscript in facet_grid label. Here is
an example of the work I have been trying to do.
df <- data.frame(species=gl(2,10,labels=c('sp1','sp2')),
age=sample(3:12,40,replace=T),
variable=gl(2,20,labels=c('N1P1 var','N2P1 var')),
value=rnorm(40))
test.plot <- ggplot(data=df,aes(x=age,y=value)) +
geom_point() +
facet_grid(variable~species)
Now I want to make by vertical facet label as 'N[1]P[1] var' and so on,
where the numbers in the squared bracket means subscript.
I have consulted some helps in this platform regarding this, but none helped me. I have used expression, bquote as suggested, but nothing worked!
You need to do 2 things:
first, make your labels as plotmath expressions
variable_labels <-
c(expression(paste(N[1],P[1]~var)), expression(paste(N[2],P[1]~var)))
df <- data.frame(species=gl(2,10,labels=c('sp1','sp2')),
age=sample(3:12,40,replace=T),
variable=gl(2,20,labels=variable_labels),
value=rnorm(40))
And then change the default labeller function in facet_grid to "label_parsed"
test.plot <- ggplot(data=df,aes(x=age,y=value)) +
geom_point() +
facet_grid(variable~species, labeller = "label_parsed")

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'}")

specifying the format of tick labels in ggplot2 without a transform using scales?

I have a graph in ggplot2 (via rpy2) that formats the x axis on a log2 scale:
p += ggplot2.scale_x_continuous(trans=scales.log2_trans(),
breaks=scales.trans_breaks("log2",
robj.r('function(x) 2^x'),
n=8),
labels=scales.trans_format("log2", robj.r('math_format(2^.x)')))
If the x values are already in log2, how can I just apply the formatting transformation of scales, to make the values appear in 2^x format, rather than a decimal log2 value? I.e. if I were to drop the trans= argument, how can I still format the ticks correctly?
I can give the answer in pure R, but I don't know rpy2 to be able to translate it.
Effectively, you just specify the labels argument which controls how the labels are displayed; don't change the trans or breaks argument which affect the overall scaling and where the breaks appear. Using mtcars as an example:
library("ggplot2")
library("scales")
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
scale_x_continuous(labels = math_format(2^.x))
(Obviously that doesn't make sense since the weight is not already on a log base 2 scale, but the concept works.)
Guessing that math_format() is in scales (can't check it right now), and based on Brian's answer the rpy2 version should be the following:
from rpy2.robjects.lib import ggplot2
from rpy2.robjects.packages import importr
scales = importr("scales")
p = ggplot2.ggplot(mtcars) + \
ggplot2.aes_string(x="wt", y="mpg")) + \
ggplot2.geom_point() + \
ggplot2.scale_x_continuous(labels = scales.math_format("2^.x"))
p.plot()

Resources