1

The units package in R is very useful in dealing with unit conversions and can be used for plotting in base plot and using the ggplot + ggforce combo. However, in the current form, units that should include Greek letters are not typeset correctly. Is there a way to have Greek letters in units displayed on the graph using the units package?

library(units)
library(ggplot2)
library(ggforce)

df = cars
df$Distance = set_units(df$dist, ft)/1000
df$Speed = set_units(df$speed, mph)

qplot(x=Speed, y=Distance, data=df) +
  scale_y_unit(unit = "um") 
# The Y-axis labels uses the latin u instead of the Greek mu
ekatko1
  • 163
  • 8

2 Answers2

1

This answer shows how to use greek letters in ggplot. mu can be added to the y axis label by using expression

ggplot(df, aes(x=Speed, y=Distance)) +
  geom_point() +
  scale_y_unit(name = expression(paste("Distance [", mu, "m]")), unit = "um")

However, the origin text [um] in the label can not be removed. scale_*_unit add [<unit>] to the axis label by default.

yang
  • 719
  • 3
  • 11
  • So it seems that the answer to the question is "no". I've filed an issue on the GitHub page: https://github.com/r-quantities/units/issues/238 – ekatko1 Apr 16 '20 at 13:03
  • IMP, this is caused by `scale_y_unit()` in ggforce package – yang Apr 16 '20 at 13:21
  • I agree that this could probably be fixed through ggforce, but the behavior is the same in base plot. – ekatko1 Apr 16 '20 at 13:31
0

It is now possible to insert Greek letters directly into R code which should allow one to have Greek letters on the axes.

qplot(x=Speed, y=Distance, data=df) +
  scale_y_unit(unit = "μm")

This did not work for me with R version 3.5.1 (2018-07-02) (see error message below). In this case, upgrade your R installation.

Error: In ‘µm’, ‘µm’ is not recognized by udunits.
See a table of valid unit symbols and names with valid_udunits().
Add custom user-defined units with install_symbolic_unit().

Credits to Edzer Pebesma for finding the solution (see https://github.com/r-quantities/units/issues/238)

ekatko1
  • 163
  • 8