2013-11-04 26 views
2

我無法按我的方式格式化我的y軸標籤。我希望他們四捨五入到最接近的千位。如何使用ggplot中的對數刻度來格式化我的標籤

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) 

enter image description here

如何格式化我的Y標誌,以便他們四捨五入到最接近的千?換句話說,我想顯示7,943,000而不是7,943,282。

+0

你可以試試'Y2 = signif(y,3)'將數字四捨五入到最接近的千位,然後抑制原始y標籤並將y2代替。 – alittleboy

回答

2

你可以通過改變labels參數scale_y_continuous.這裏我用round(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 

主要生產: enter image description here

相關問題