I would like to include the mathematical sign for a multiplication (like the lower-case x) in a plot title.
I have tried using latex2exp to no avail - can someone help me with this?
The reprex code is this:
library(ggplot2)
library(latex2exp)
ggplot(data = data.frame(number = round(rnorm(200, mean=55, sd=5))),
aes(x = number)) + geom_density() +
ggtitle(TeX("Title containing times sign here: $\\times$"))
And it produces this outcome:
I would like the square (indicating an unrecognized sign) to display as this sign: . It seems that other signs work (e.g. alpha) using this approach but I cannot figure out why the times sign poses an issue.
I'm not set on using latex2exp for the solution, as long as it works with the latex font (LM Roman 10).
Thanks a lot in advance.
It works for me with plotmath facilities: the %*% operator displays as the times symbol. For example,
library(ggplot2)
ggplot(data = data.frame(number = round(rnorm(200, mean=55, sd=5))),
aes(x = number)) + geom_density() +
ggtitle(expression("Title containing times sign here: " %*% ""))
Created on 2021-06-14 by the reprex package (v2.0.0)
I think that is what latex2exp was trying to do, so it might be that on your system the times symbol was missing, which is why you got the box. Funny that the Unicode solution worked.
One approach might be to use the unicode code for the multiplication symbol:
library(ggplot2)
ggplot(data = data.frame(number = round(rnorm(200, mean=55, sd=5))),
aes(x = number)) + geom_density() +
ggtitle("Title containing times sign here: \u00D7")
Unicode characters can be added to any string with \u and the 4 hex code. You can find the appropriate code from your OS or with Google.
Related
Owing to the journal's formatting requirement, I need to use a true negative sign (UTF-16: 2212) for my graphs. This is similar to this post, except that my existing plots are based on ggplot. Rather than reconfiguring them back to base R plot, I am hoping to find a ggplot2-native solution.
The current ggplot-based questions here unfortunately deal with text annotations, character axis ticks, or headings instead of continuous axis ticks. Meanwhile, the closest idea I have for the scale_y_continuous function is to use the label variable in the function or simply use break and manually tune all the relevant axes, but I am still figuring it out.
The manual way for now:
ggplot(test, aes(x = x, y = y)) +
geom_raster(aes(fill = value)) +
scale_x_continuous(breaks = -3:3, labels = c("\U2212\U0033","\U2212\U0032","\U2212\U0031",0,1,2,3))
This produces a plot with the default hyphen-minus sign on the y-axis and the true minus sign on the x-axis. However, the number has to be coded in UTF format, else it could be concatenated by the UTF reader into another UTF character (i.e. "\U22123" is a Chinese character instead of -3.)
May I ask if there is a more elegant and/or dynamic solution to this?
You can write your own labeller function.
library(ggplot2)
label_trueminus <- function(x){
ifelse(sign(x) == -1, paste0("\u2212", abs(x)), x)
}
ggplot(mtcars, aes(mpg-20, disp)) +
geom_point() +
scale_x_continuous(labels = label_trueminus)
Created on 2022-01-25 by the reprex package (v2.0.1)
or even shorter:
library(tidyverse)
label_parse <- function(breaks) {
parse(text = breaks)
}
# sample data
tibble(
a = -5:5,
b = rnorm(length(a))
) %>%
# sample plotting
ggplot(aes(a,b)) +
geom_point() +
# here's the magic
scale_x_continuous(labels = label_parse)
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.)
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.
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.
I have recently updated my ggplot2 distribution from an old one (can't remember the version) to v0.9.1. Before the update, when I used a scale_y_log10 all the values were in power-of-10 notation, which was really nice to look at, especially by using LaTeX labeling. Now I get everything in scientific notation, and I can't say I like it very much when looking at the plots. It's just a detail, but is there a way to revert to the power-of-10 notation?
Thanks
Simply copy pasted what was on page 33-34 in the guide Chase linked to in the comments above.
# install.packages(c("scales", "ggplot2"), dependencies = TRUE)
require(ggplot2)
DF <- data.frame(x = rlnorm(1000, m = 10, sd = 5))
Power-of-10 notation
(q <- ggplot(DF, aes(x = log(x))) + geom_histogram())
Power-of-10 notation
require(scales)
q + scale_x_continuous(labels = math_format(e^.x)) + xlab("x")