Marek Fiołka
Marek Fiołka

Reputation: 4949

Unicode Ø symbol with superscript in ggplot

I would like to put the following text behind the ggplot chart

enter image description here

And I thought it should be very easy to achieve. Unfortunately, all my attempts ended in a complete failure. I have already read over a dozen posts on this forum that dealt with this topic. I have already made several dozen attempts and still do not manage to get the effect I expect. By the way, I noticed a rather strange treatment of unicode symbols. Please compare the title and subtitle from the chart below.

library(tidyverse)
ggplot()+
  labs(
    title = expression("\u00AE \u00A9, \u00A2, \u00BC, \u00BD, \u00BE, \u00A5, \u00D8, \u00F8"),
    subtitle = "\u00AE \u00A9, \u00A2, \u00BC, \u00BD, \u00BE, \u00A5, \u00D8, \u00F8"
  )+
  annotate(geom="text", label="\u00D828.15^{+0.15}", x=0, y=0.08,parse = TRUE, size=5)+
  annotate(geom="text", label="\u00D8", x=0, y=0, size=5)+
  annotate(geom="text", label="28.15^{+0.15}", x=0.03, y=0.001,parse = TRUE, size=5)+
  xlim(c(-0.1, 0.1))+
  ylim(c(-0.02, 0.1))+
  theme_void()

enter image description here

Why do some unicode symbols lose some of their graphics when placed inside an expression function or when I use the parse option? This happens with the symbols ¢, ¥, ¼, ½, ¾, Ø. In my case, I particularly care about the symbol Ø. I finally got the effect on the track chart (annotation at the bottom of the chart) was created from the tedious setting of two annotations next to each other. However, it completely falls apart as soon as I change the size of the chart.

Does anyone know how this can be solved?

Thank you very much for your quick reply. Unfortunately, it still doesn't work for me! :-(

library(latex2exp)
library(ggplot2)
label <- TeX("$Ø28.15^{+0.15}$")
qplot(1, "A") +
  annotate(geom = "text", label = label, x = 1, y = 1.1, size = 10) +
  xlab(label)

enter image description here

Upvotes: 2

Views: 766

Answers (2)

manro
manro

Reputation: 3677

I know how to modify this solution above, maybe not so cute, but works everywhere

library(ggplot2)
label <- "\u00D828.15\U207A\u2070\U00B7\U00B9\u2075" 
 qplot(1, "A") +
     annotate(geom = "text", label = label, x = 1, y = 1.1, size = 10) +
     xlab(label)

enter image description here

Upvotes: 0

danlooo
danlooo

Reputation: 10637

You can use LaTeX syntax within a math ($) environment to auto-generate the plotmath expression:

library(latex2exp)
library(ggplot2)

label <- TeX("$Ø28.15^{+0.15}$")
label
#> expression(`$Ø28.15^{+0.15}$` = paste("", "Ø28", ".", "15", 
#>     phantom()^{
#>         paste("+0", ".", "15")
#>     }, ""))

qplot(1, "A") +
  annotate(geom = "text", label = label, x = 1, y = 1.1, size = 10) +
  xlab(label)
#> Warning in is.na(x): is.na() applied to non-(list or vector) of type
#> 'expression'

Created on 2021-10-18 by the reprex package (v2.0.1)

Upvotes: 1

Related Questions