Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thousand separator in histogram axis in R

Tags:

r

histogram

I would like to see the x-axis numbers in my histogram in thousand separated format. So for example,

y <- seq(10000, 100000, 10000) 
hist(y)

in this plot, I want to see 10,000 20,000, etc. on the x-axis. Any simple way to get it?

like image 888
Baykal Avatar asked Dec 06 '22 07:12

Baykal


2 Answers

Draw the histogram without an x-axis, then add it manually with axis:

y <- seq(10000, 100000, 10000) 
hist(y, xaxt="n")
axis(side=1, at=axTicks(1), 
     labels=formatC(axTicks(1), format="d", big.mark=','))

axTicks calculates the tickmark locations, and formatC formats the numbers. Here is the result:

Hisogram

like image 67
Noam Ross Avatar answered Dec 31 '22 23:12

Noam Ross


The scales library has a function called comma which formats the numbers how you want:

library(scales)

Not quite what you wanted, but a start:

q<-quantile(y,prob=seq(0,1,.1));hist(y,breaks=q,labels=comma(q))

Better version, using lattice:

q<-quantile(y,prob=seq(0,1,.1));histogram(~y,breaks=q,scales=list(at=q,labels=comma(q)))
like image 37
Ari B. Friedman Avatar answered Jan 01 '23 00:01

Ari B. Friedman