Thousand separator in label of x or y axis

giordano picture giordano · Nov 2, 2012 · Viewed 18.6k times · Source

I would like to have pretty labels on the y-axis. For example, I prefer to have 1,000 instead of 1000. How can I perform this in ggplot? Here is a minimum example:

x <- data.frame(a=c("a","b","c","d"), b=c(300,1000,2000,4000))
ggplot(x,aes(x=a, y=b))+
               geom_point(size=4)

Thanks for any hint.

Answer

Sandy Muspratt picture Sandy Muspratt · Nov 2, 2012

With the scales packages, some formatting options become available: comma, dollar, percent. See the examples in ?scale_y_continuous.

I think this does what you want:

library(ggplot2)
library(scales)

x <- data.frame(a=c("a","b","c","d"), b=c(300,1000,2000,4000))

ggplot(x, aes(x = a, y = b)) + 
  geom_point(size=4) +
  scale_y_continuous(labels = comma)