Palace Chan
Palace Chan

Reputation: 9203

I can't seem to round values with ggplot

I am getting a ton of decimal places for my values on the y-axis when I ggplot something using a command like this:

x <- c(39.029998779296875000, 39.080001831054687500, 38.990001678466796875, 39.000000000000000000, 38.990001678466796875)
example <- data.frame(i=1:5, X=x)
ggplot(example, aes(i, y=foo, color=variable)) + geom_line(aes(y=X, color='X'))

What is the appropiate way to tell ggplot to adjust the values to not have that many decimal places?

Here is my output from this command:

This image is the output!

Here is the output of sessionInfo() - nothing looks fishy here to me:

> sessionInfo()
R version 2.15.0 (2012-03-30)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
[1] C

attached base packages:
[1] grid      grDevices graphics  stats     datasets  utils     methods   base

other attached packages:
[1] ggplot2_0.9.0

loaded via a namespace (and not attached):
 [1] MASS_7.3-17        RColorBrewer_1.0-5 colorspace_1.1-1   dichromat_1.2-4    digest_0.5.2       memoise_0.1        munsell_0.3        plyr_1.7.1     \

 [9] proto_0.3-9.2      reshape2_1.2.1     scales_0.2.0       stringr_0.6        tools_2.15.0

Upvotes: 1

Views: 1817

Answers (1)

James
James

Reputation: 66844

I think this is an issue with your digits option. I can reproduce if I set options(digits=22), rather than use the factory default of 7, which gives only 2 digits after the decimal points with this data:

print(x,digits=7)
[1] 39.03 39.08 38.99 39.00 38.99

So, try restoring it with:

options(digits=7)

Upvotes: 3

Related Questions