x轴或y轴标签中的千位分隔符

gio*_*ano 22 r ggplot2 axis-labels

我想在y轴上有漂亮的标签.例如,我更喜欢1000而不是1000.如何在ggplot中执行此操作?这是一个最小的例子:

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)
Run Code Online (Sandbox Code Playgroud)

谢谢你的任何提示.

San*_*att 34

使用这些scales包,可以使用一些格式选项:逗号,美元,百分比.请参阅中的示例?scale_y_continuous.

我认为这样做你想要的:

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)
Run Code Online (Sandbox Code Playgroud)


Geo*_*sky 8

使用具有基本format()功能的任何字符美化数千个:

示例 1(逗号分隔)。

format(1000000, big.mark = ",", scientific = FALSE)
[1] "1,000,000"
Run Code Online (Sandbox Code Playgroud)

示例 2(空格分隔)。

format(1000000, big.mark = " ", scientific = FALSE)
[1] "1 000 000"
Run Code Online (Sandbox Code Playgroud)

format()使用匿名函数应用于ggplot 轴标签:

ggplot(x, aes(x = a, y = b)) +
        geom_point(size = 4) +
        scale_y_continuous(labels = function(x) format(x, big.mark = ",",
                                                       scientific = FALSE))
Run Code Online (Sandbox Code Playgroud)