Forzaa
Forzaa

Reputation: 1545

Change the thousands separator in a ggplot

While making a bar plot with ggplot I run into troubles getting the preferred thousands separator. I would like thousands to be separated by a dot instead of a comma. Like this it gives no separator:

require(ggplot2)
require(scales) 
options(scipen=10)
D = data.frame(x=c(1,2),y=c(1000001,500000))
p = ggplot(D,aes(x,y)) + geom_bar(stat="identity") 
p

and like this it gives a comma:

p + scale_y_continuous(labels=comma)

How do you get a dot as thousands separator? I can't find documentation on which types of other labels exist besides some examples on http://docs.ggplot2.org/0.9.3.1/scale_continuous.html.

Thanks in advance,

Forza

Upvotes: 38

Views: 32385

Answers (4)

NorthNW
NorthNW

Reputation: 167

An even simpler answer than the ones provided is:

p + scale_y_continuous(labels = scales::comma_format(big.mark = '.'))

Upvotes: 1

this also works:

p + scale_y_continuous(labels = scales::comma)

Upvotes: 23

BMLopes
BMLopes

Reputation: 606

Very good answer. As you are defining a thousands separator, it is better to also define the decimal separator as a comma, to avoid errors.

p + scale_y_continuous(labels=function(x) format(x, big.mark = ".", decimal.mark = ",", scientific = FALSE))

Upvotes: 13

lukeA
lukeA

Reputation: 54237

p + scale_y_continuous(labels=function(x) format(x, big.mark = ".", scientific = FALSE))

Upvotes: 56

Related Questions