2

I'm plotting stable isotope data in R with the ggplot2 package and wish to have the lowercase delta symbols on the axis titles printed in italics. I am using expression() to build the axis titles but the delta symbol will not print in italics when called as 'delta' or prints as d if i use unicode'\u03B4'

A little background: The symbol delta (δ) is a legitimate SI quantity symbol and therefore should always be printed in italic font. See Coplen, T. B. (2011). Guidelines and recommended terms for expression of stable-isotope-ratio and gas-ratio measurement results. Rapid Communications in Mass Spectrometry, 25(17), 2538-2560. doi:10.1002/rcm.5129 https://onlinelibrary.wiley.com/doi/full/10.1002/rcm.5129

Some reproducible code:

require(ggplot2)

set.seed(20)
df <- data.frame(d13C = rnorm(20, -23, 5),
                 DIC = rnorm(20, 4, 0.2),
                 d13CDIC = rnorm(20, -8, 2))

ggplot(df, aes(x = d13C, y = d13CDIC)) +
  geom_point(aes(fill = DIC), pch = 21, cex = 5) +
  labs(x = expression(italic(delta)^13*C~("\211"~VPDB)),
       y = expression(italic("\u03B4")^13*C[DIC]~("\211"~VPDB))) +
  theme_bw()

gives the following plot: enter image description here

And a follow-up question: How do i go about saving the plot to a svg device? It plots correctly when using ggsave to save to .png. However i get the following error when plotting to a .svg device:

ggsave(filename = 'isotope_plot.svg', width = 5, height = 3, units = "in")

Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, : Metric information not available for this family/device

Claus Wilke
  • 16,992
  • 7
  • 53
  • 104
Jordan
  • 173
  • 1
  • 2
  • 9
  • Copying and pasting the δ symbol into the script also returns *d* where *δ* is desired. e.g. expression(italic(δ)^13*C[DIC]~("\211"~VPDB)) – Jordan Nov 12 '19 at 05:32

1 Answers1

5

You can do this with ggtext. Also probably makes writing the entire code for the labels a little simpler.

library(ggplot2)
library(ggtext) # remotes::install_github("clauswilke/ggtext")

set.seed(20)
df <- data.frame(d13C = rnorm(20, -23, 5),
                 DIC = rnorm(20, 4, 0.2),
                 d13CDIC = rnorm(20, -8, 2))

ggplot(df, aes(x = d13C, y = d13CDIC)) +
  geom_point(aes(fill = DIC), pch = 21, cex = 5) + 
  labs(
    x = "*&delta;*<sup>13</sup>C (&permil; VPDB)",
    y = "*&delta;*<sup>13</sup>C<sub>DIC</sub> (&permil; VPDB)"
  ) +
  theme_bw() +
  theme(
    axis.title.x = element_markdown(),
    axis.title.y = element_markdown()
  )

Created on 2019-11-12 by the reprex package (v0.3.0)

Claus Wilke
  • 16,992
  • 7
  • 53
  • 104
  • Nice one @Claus, Does the trick. Will this be a cran supported package in the future? I'd prefer to use base, ggplot2, or a cran package if possible – Jordan Nov 13 '19 at 00:01
  • Yes, absolutely. Most likely after the next ggplot2 release. It needs some ggplot2 fixes to support all features I want to add. – Claus Wilke Nov 13 '19 at 07:53