Thousand separator in label of x or y axis

﹥>﹥吖頭↗ 提交于 2019-11-29 16:24:30

问题


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.


回答1:


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)



回答2:


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


来源:https://stackoverflow.com/questions/13191601/thousand-separator-in-label-of-x-or-y-axis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!