Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thousand separator in label of x or y axis

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.

like image 261
giordano Avatar asked Nov 02 '12 08:11

giordano


People also ask

How do you label the x and y axis?

The proper form for a graph title is "y-axis variable vs. x-axis variable." For example, if you were comparing the the amount of fertilizer to how much a plant grew, the amount of fertilizer would be the independent, or x-axis variable and the growth would be the dependent, or y-axis variable.

What should be the label for the x-axis?

Horizontal axis labels represent the X axis. They do not apply to pie, funnel, or gauge charts. Vertical axis labels represent the Y1 axis in a single axis chart. They represent a numeric scale, usually located on the left side of a vertical chart.

What label goes on the y axis?

The independent variable belongs on the x-axis (horizontal line) of the graph and the dependent variable belongs on the y-axis (vertical line).


2 Answers

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) 
like image 89
Sandy Muspratt Avatar answered Oct 09 '22 17:10

Sandy Muspratt


Prettify thousands using any character with basic format() function:

Example 1 (comma separated).

format(1000000, big.mark = ",", scientific = FALSE) [1] "1,000,000" 

Example 2 (space separated).

format(1000000, big.mark = " ", scientific = FALSE) [1] "1 000 000" 

Apply format() to ggplot axes labels using an anonymous function:

ggplot(x, aes(x = a, y = b)) +         geom_point(size = 4) +         scale_y_continuous(labels = function(x) format(x, big.mark = ",",                                                        scientific = FALSE)) 
like image 36
George Shimanovsky Avatar answered Oct 09 '22 18:10

George Shimanovsky