Reputation: 21625
I'm having trouble formatting my y-axis labels the way I want. I'd like them to be rounded to the nearest thousand.
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)
How do I format my y labels so that they are rounded to the nearest thousand? In other words, I'd like 7,943,000 displayed instead of 7,943,282.
Upvotes: 2
Views: 2322
Reputation: 22496
You can do that by changing the labels
argument to scale_y_continuous.
Here I used round(x,-3)
to round the label value to the nearest 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
Which produces:
Upvotes: 2