Reputation: 26218
Indian style thousand separators are used like this. First separator at 3 digits (thousands) but thereafter separator at every two digits.
1
10
100
1,000
10,000
1,00,000
10,00,000
1,00,00,000
10,00,00,000
I know that I can change/format axes in ggplot2 chart by using scale_y_continuous(labels = scales::comma)
But how can I change the thousand separator placeholder in r ggplot2 chart axes according to Indian formats as described above.
A sample example
library(tidyverse)
iris %>%
mutate(Petal.Length= Petal.Length*100000) %>%
ggplot(aes(x= Species, y = Petal.Length)) +
geom_col() +
scale_y_continuous(labels = scales::comma)
Created on 2021-06-28 by the reprex package (v2.0.0)
Upvotes: 3
Views: 239
Reputation: 2748
You can define your own formatting function and supply this as the labels
argument to scale_y_continuous()
. Here is an example using the base prettyNum()
function:
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)
Here's an alternative implementation using regular expressions. On balance I think I prefer the first option for clarity, but this is quite elegant:
indian_comma <- function(x) {
x <- prettyNum(x, scientific = FALSE)
gsub("(?<!^)(?=(\\d{2})+\\d$)", ",", x, perl = TRUE)
}
Upvotes: 9
Reputation: 44788
This post: https://stackoverflow.com/a/62037466/2554330 defines a function format2()
. It doesn't quite work as is, but with some minor fixes, this works:
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)
Created on 2021-06-28 by the reprex package (v2.0.0)
Upvotes: 2
Reputation: 2906
You can set the breaks for the y-axis, and then label them according to the Indian system:
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"))
Upvotes: 2