29

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

3
  • What's comma in labels=comma? Its an undefined variable, thats what it is...
    – Spacedman
    Apr 24, 2014 at 8:23
  • @Spacedman it works and it is well defined. You can also see an example of it being used in the official documentation. Do you have the newest version of ggplot? You need to use library(scales), btw.
    – Forzaa
    Apr 24, 2014 at 8:29
  • Its from the scales package. I see your edit now.
    – Spacedman
    Apr 24, 2014 at 8:34

3 Answers 3

Reset to default

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

45
p + scale_y_continuous(labels=function(x) format(x, big.mark = ".", scientific = FALSE))
4
  • Works like a charm, thanks! Didn't know you could pass on functions to labels as well, that's useful.
    – Forzaa
    Apr 24, 2014 at 8:32
  • 1
    If you type comma you'll see that it is a function very similar to the answer given. Perhaps suggest to the scales package author a dottedThousands function?
    – Spacedman
    Apr 24, 2014 at 8:37
  • Find the github repo for scales (if it exists), add new issue. Otherwise email maintainer.
    – Spacedman
    Apr 24, 2014 at 12:08
  • 2
    I'd like to add, that it's often necessary to add the trim option (see here: stackoverflow.com/questions/14665461/…) to the function on the x axis, as the format might move the axis values to the right. Thus they do not align any longer.
    – Bouncner
    Feb 9, 2016 at 13:56
15

this also works:

p + scale_y_continuous(labels = scales::comma)
11

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.