R 如何在ggplot中使用对数比例格式化标签

R 如何在ggplot中使用对数比例格式化标签,r,ggplot2,R,Ggplot2,我无法按照我想要的方式格式化y轴标签。我想把它们四舍五入到最接近的千 library(scales) library(ggplot2) df<-data.frame(x=seq(1,10,by=1),y=sample(10000000,10)) ggplot(df,aes(x=x,y=y))+geom_point()+scale_y_continuous(trans='log10', breaks=trans_breaks('log10', function(x) 10^x), labe

我无法按照我想要的方式格式化y轴标签。我想把它们四舍五入到最接近的千

library(scales)
library(ggplot2)

df<-data.frame(x=seq(1,10,by=1),y=sample(10000000,10))
ggplot(df,aes(x=x,y=y))+geom_point()+scale_y_continuous(trans='log10', breaks=trans_breaks('log10', function(x) 10^x), labels=comma)
库(比例)
图书馆(GG2)

df您可以通过将
labels
参数更改为
scale_y_continuous.
这里我使用
舍入(x,-3)
将标签值舍入到最接近的1000:

library(ggplot2)
library(scales)
df<-data.frame(x=seq(1,10,by=1),y=sample(10000000,10))
p <- ggplot(df,aes(x=x,y=y))+geom_point()
p <- p +scale_y_continuous(trans='log10', 
                           breaks=trans_breaks('log10', function(x) 10^x), 
                           labels = function(x)round(x,-3)
                           )
p
库(ggplot2)
图书馆(比例尺)

df您可以尝试使用
y2=signif(y,3)
将数字四舍五入到最接近的千位,然后抑制原始y标签,并将
y2
改为。