Reputation: 4879
I would like to display an expression in a ggplot2
plot axis, where I want to simultaneously change both the size
and face
for the axis test:
Using ggplot2::element_text
, I can change size
, but not face
.
ggplot(mtcars, aes(cyl, mpg)) + geom_point() +
scale_x_continuous(
labels = parse(text = "widehat(mu)=='6'"),
breaks = 6
) +
ggplot2::theme(axis.text.x = ggplot2::element_text(face = "bold", size = 12))
As suggested here, I can wrap the expression in bold
, which partly works (note that only 6
is in bold, and not italic mu) but then the text size
doesn't change:
ggplot(mtcars, aes(cyl, mpg)) + geom_point() +
scale_x_continuous(
labels = parse(text = "bold(widehat(mu)=='6')"),
breaks = 6
) +
ggplot2::theme(axis.text.x = ggplot2::element_text(face = "bold", size = 12))
Created on 2021-02-12 by the reprex package (v1.0.0)
Is there any way to do this in ggplot2
itself? Or one will have to use something like ggtext
?
Upvotes: 2
Views: 746
Reputation: 76412
Here is a way to make the font size vary as requested.
The .top
in the second plot's theme
seems to be a typo, remove it and the font becomes bigger.
And instead of parse
, use expression
for plotmath.
library(ggplot2)
ggplot(mtcars, aes(cyl, mpg)) + geom_point() +
scale_x_continuous(
labels = expression(bold(widehat(mu) == '6')),
breaks = 6
) +
theme(axis.text.x = element_text(face = "bold", size = 12))
Here is another one, with the axis label even bigger.
ggplot(mtcars, aes(cyl, mpg)) + geom_point() +
scale_x_continuous(
labels = expression(bold(widehat(mu) == '6')),
breaks = 6
) +
theme(axis.text.x = element_text(face = "bold", size = 24))
Upvotes: 2