Ricardo Cruz
Ricardo Cruz

Reputation: 3593

ggplot format italic annotation

Can I use markup in a ggplot annotation?

Let's say, I have this graph:

p <- function(i) 8*i
a <- function(i) 1+4*i*(i-1)

library(ggplot2)
library(reshape2)

i <- 1:(8*365/7)
d <- data.frame(i=i,p=p(i),a=sapply(i,a))
d <- melt(d, id.vars='i')
p <- ggplot(d, aes(i, value, linetype=variable)) +
    geom_hline(yintercept=700^2) +
    geom_line() +
    scale_linetype_manual(values=c(2,1)) +
    #geom_point() +
    scale_x_continuous(breaks=(0:20)*365/7, labels=0:20) +
    #scale_y_continuous(breaks=c(0,700^2), labels=c(0,expression(L^2)))
    scale_y_sqrt() +
    #scale_y_log10() +
    annotate('text', 8*365/7, 1e3, label="P(i)=8i", hjust=1, size=3) +
    annotate('text', 8*365/7, 2.5e5, label="A(i)=1+4i(i-1)", hjust=1, size=3)
print(p + theme_classic())

output

I know I can use fontface=3 and put everything in italic. But I do not want the numbers in italic, only the variable i. Preferably, P and A would not be in italic as well.

Any ideas?

Upvotes: 18

Views: 30811

Answers (4)

Matt L.
Matt L.

Reputation: 2974

For simple text formatting, use the ggtext package

If you do not regularly use plotmath, and find it easier to use simple markdown/html style text formatting- use the recently added ggtext package

Adding the reference to the newer ggtext package here- even though this example is simpler than the OP question (along the lines of @pbnelson)

library(ggplot2)
library(ggtext)
#> Warning: package 'ggtext' was built under R version 4.0.3
library(tibble)
#> Warning: package 'tibble' was built under R version 4.0.3
dfx <- tibble(x=seq(0,3.14,0.01))
ggplot(dfx, aes(x, sin(x))) +
  geom_line() + # works the same for qplot or ggplot
    geom_richtext(x = 1.5, 
                  y = 0.5, 
                  label.color = NA,
                  label = "<i><b>Hello</i></b>, World<br>You can even do 
                  Some <span style='color:blue'>blue text **in bold.**</span><br>And *italics text.*"
                  )

Created on 2020-12-17 by the reprex package (v0.3.0)

Upvotes: 3

Johannes Titz
Johannes Titz

Reputation: 1001

Best rated answer is just fine, but in a more complex scenario with linebreaks it did not work for me, so I simply used Unicode italic characters instead. For your example:

library(Unicode)

italic_i <- u_char_inspect(u_char_from_name("MATHEMATICAL ITALIC SMALL I"))["Char"]
label1 <- paste("P(", italic_i, ")=8", italic_i, sep="")
label2 <- paste("A(", italic_i, ")=1+4", italic_i, "(", italic_i, "-1)", sep="")

i <- 1:(8*365/7)
d <- data.frame(i=i,p=p(i),a=sapply(i,a))
d <- melt(d, id.vars='i')
p <- ggplot(d, aes(i, value, linetype=variable)) +
  geom_hline(yintercept=700^2) +
  geom_line() +
  scale_linetype_manual(values=c(2,1)) +
  #geom_point() +
  scale_x_continuous(breaks=(0:20)*365/7, labels=0:20) +
  #scale_y_continuous(breaks=c(0,700^2), labels=c(0,expression(L^2)))
  scale_y_sqrt() +
  #scale_y_log10() +
  annotate('text', 8*365/7, 1e3, label=label1, hjust=1, size=3) +
  annotate('text', 8*365/7, 2.5e5, label=label2, hjust=1, size=3)
print(p + theme_classic())

Italic annotation ggplot2

Edit: I have just noticed that saving a pdf with pdf() doest not render unicode correctly, but you can simply use cairo_pdf() instead, which works just fine (see: Unicode Characters in ggplot2 PDF Output)

Upvotes: 7

pbnelson
pbnelson

Reputation: 1759

Right now this page is the top search result on google for ggplot annotate italic. For the benefit of those who simply want to italicize an entire annotation, I'm writing this post. Use annotate's fontface option. Example:

seq(0,3.14,0.01)
qplot(x, sin(x)) +   # works the same for qplot or ggplot
annotate(geom = 'text', 
         x = 1.5, 
         y = 0.5, 
         hjust = 0.5,
         label = 'Hello, World', 
         fontface = 'italic')

enter image description here

Upvotes: 31

Backlin
Backlin

Reputation: 14872

Use parse=TRUE and supply a string formatted according to ?plotmath.

p <- ggplot(d, aes(i, value, linetype=variable)) +
    geom_hline(yintercept=700^2) +
    geom_line() +
    scale_linetype_manual(values=c(2,1)) +
    scale_x_continuous(breaks=(0:20)*365/7, labels=0:20) +
    scale_y_sqrt() +
    annotate('text', 8*365/7, 1e3,
             label="P(italic(i))==8~italic(i)", parse=TRUE,
             hjust=1, size=3) +
    annotate('text', 8*365/7, 2.5e5,
             label="A(italic(i))==1+4~italic(i)(italic(i)-1)", parse=TRUE, 
             hjust=1, size=3)

enter image description here

Upvotes: 25

Related Questions