Home > Enterprise >  True negative sign in continuous y-axis scale
True negative sign in continuous y-axis scale

Time:01-26

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 enter image description here

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

CodePudding user response:

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)

CodePudding user response:

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)
  •  Tags:  
  • Related