ggplot2图表轴中的印度风格千位分隔符

Indian Style thousand separator in ggplot2 chart axes

Indian style thousand separators是这样使用的。第一个分隔符在 3 位数字(千位),但之后每两个数字分隔符。

1
10
100
1,000
10,000
1,00,000
10,00,000
1,00,00,000
10,00,00,000

我知道我可以使用 scale_y_continuous(labels = scales::comma)

在 ggplot2 图表中 change/format 轴

但是如何根据印度格式更改 r ggplot2 图表轴中的千位分隔符占位符,如上所述。

示例

library(tidyverse)
iris %>%
  mutate(Petal.Length= Petal.Length*100000) %>%
  ggplot(aes(x= Species, y = Petal.Length)) +
  geom_col() +
  scale_y_continuous(labels = scales::comma)

reprex package (v2.0.0)

于 2021-06-28 创建

你可以设置y轴的breaks,然后按照印度制标注:

iris %>%
    mutate(Petal.Length= Petal.Length*100000) %>%
    ggplot(aes(x= Species, y = Petal.Length)) +
    geom_col() +
    scale_y_continuous(breaks = c(0,10000000,20000000),labels = c("0","1,00,00000","2,00,00,000"))

您可以定义自己的格式化函数并将其作为 labels 参数提供给 scale_y_continuous()。下面是一个使用基础 prettyNum() 函数的例子:

library(ggplot2)

indian_comma <- function(x) {
  
  # Format the number, first dividing by 10 to place the first comma at the 
  # right point
  out <- prettyNum(x %/% 10, big.interval = 2L, big.mark = ",", scientific = FALSE)
  out <- paste0(out, x %% 10)
  
  # Switch between formatted and un-formatted depending on the size of the
  # number
  ifelse(
    x < 1000, x, out
  )
  
}

iris %>%
  mutate(Petal.Length= Petal.Length*100000) %>%
  ggplot(aes(x= Species, y = Petal.Length)) +
  geom_col() +
  scale_y_continuous(labels = indian_comma)

编辑

以下函数使用了正则表达式,我认为它更好:

indian_comma <- function(x) {
  x <- prettyNum(x, scientific = FALSE)
  gsub("(?<!^)(?=(\d{2})+\d$)", ",", x, perl = TRUE)
}

这个post:定义了一个函数format2()。它不能按原样工作,但通过一些小的修复,它可以工作:

format2 <- function(x, ..., big.mark = "", big.interval = c(3L, 2L), decimal.mark = ".") {
  intervene <- !is.na(x) && x > 0 && (log(abs(x), 10) >= sum(big.interval)) && nzchar(big.mark)
  cl <- match.call()
  cl[[1]] <- substitute(format)
  if (intervene) {
    cl$x <- x %/% 10^big.interval[1]
    cl$big.interval <- big.interval[2]
    bigx <- eval.parent(cl)
    cl$x <- x 
    cl$big.interval <- big.interval[1]
    mostx <- eval.parent(cl)
    mostx <- 
      substr(mostx,
             1L + nchar(x %/% 10^big.interval[1]) +
               trunc(trunc(log(abs(x %/% 10^big.interval[1]), 10L)) / big.interval[1]),
             nchar(mostx))
    return( paste0(bigx, mostx) )
  } else eval.parent(cl)
}

f <- function(x) {
  sapply(x, format2, scientific = FALSE, big.mark = ",")
}

library(tidyverse)
iris %>%
  mutate(Petal.Length= Petal.Length*100000) %>%
  ggplot(aes(x= Species, y = Petal.Length)) +
  geom_col() +
  scale_y_continuous(labels = f)

reprex package (v2.0.0)

于 2021 年 6 月 28 日创建