How to Format Numbers with Commas in R Using format() and scales
To format numbers with commas in R, use the big.mark argument in base R’s format() function. Adding thousand separators makes large numbers readable in console output, summary tables, and exported reports. The scales package offers convenience wrappers that integrate with ggplot2 for polished axis labels.
x <- c(1000, 1234567.89, 999999)
# Base R: use big.mark
format(x, big.mark = ",")
# [1] "1,000" "1,234,567.89" "999,999"
# With fixed decimal places
format(x, big.mark = ",", nsmall = 2)
# [1] "1,000.00" "1,234,567.89" "999,999.00"
For currency display, combine big.mark with a dollar sign prefix by pasting or by using scales::dollar(). The formatC() function offers C-style control: formatC(x, format = "f", big.mark = ",", digits = 2).
library(scales)
comma(x) # "1,000" "1,234,567.89" "999,999"
dollar(x) # "$1,000" "$1,234,567.89" "$999,999"
# In ggplot2 axis labels
library(ggplot2)
ggplot(df, aes(x = product, y = sales)) +
geom_col() +
scale_y_continuous(labels = label_comma())
prettyNum(x, big.mark = ",") is a base R alternative that works without loading any packages. The scales::label_comma() function integrates directly with ggplot2 scales for clean axis labels. For European-style formatting that uses periods as thousand separators and commas as decimal marks, pass big.mark = ".", decimal.mark = "," to format(). R’s locale settings control the default behavior, but explicitly passing these arguments keeps your output consistent regardless of where the script runs. The scales::number() function gives you control over accuracy and scale without the currency symbol, making it a middle ground between comma() and dollar() for general numeric display.