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.)
May I ask if there is a more elegant and/or dynamic solution to this?
>Solution :
You can write your own labeller function.
library(ggplot2)
label_trueminus <- function(x){
ifelse(sign(x) == -1, paste0("\u2212", abs(x)), x)
}
ggplot(mtcars, aes(mpg-20, disp)) +
geom_point() +
scale_x_continuous(labels = label_trueminus)

Created on 2022-01-25 by the reprex package (v2.0.1)
