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.
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.
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.
The independent variable belongs on the x-axis (horizontal line) of the graph and the dependent variable belongs on the y-axis (vertical line).
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)
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With