'True negative sign in continuous y-axis scale

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.)

enter image description here

May I ask if there is a more elegant and/or dynamic solution to this?



Solution 1:[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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Statistican