Pregunta

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

¿Fue útil?

Solución

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

Otros consejos

this also works:

p + scale_y_continuous(labels = scales::comma)

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))

An even simpler answer than the ones provided is:

p + scale_y_continuous(labels = scales::comma_format(big.mark = '.'))
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top