0

When plotting this data the x and y labels the default label points have multiple decimal places e.g. 0.02999999999999999989 rather than a sensible point like 0.03.

I've tried specifying limits and breaks with scale_y_continuous() function amongst other things.

ggplot(data, aes(x, y, col = z)) + 
    geom_point(size = 2) + 
    scale_y_continuous(limits = c(-0.03, 0.03), breaks = seq(-0.03, 0.03, 0.01))

I am able to change the position of label points by playing with breaks = seq(). In the case of the above code I expect the y-axis to go from -0.03 to 0.03 with label points every 0.01. However they still don't plot exactly as specified and display values with numerous decimal places as in the default plot. e.g I expect a label at 0.01 but get 0.0100000000000000019

Edit: The error is replicated when running M. Viking's code to plot the iris dataset.

CM3
  • 3
  • 2
  • 3
    You'll need to add an example of your dataset to make your question reproducible. That way others can run your code and see if they can reproduce your problem. :) There are some good hints on how to include your dataset at [this link](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). The way you describe your issues makes me wonder if your `y` variable is a factor instead of numeric (or something else along those lines). – aosmith Jul 25 '19 at 23:31
  • I agree with aosmith, might be something with your data. My `iris` dataset test looks ok `ggplot(iris, aes(Sepal.Length, Sepal.Width/100-.03, col = Petal.Width)) + geom_point(size = 2) + scale_y_continuous(limits = c(-0.03, 0.03), breaks = seq(-0.03, 0.03, 0.01))` – M.Viking Jul 25 '19 at 23:40
  • May be that `y` is not numeric in your data. What does `str(data)` show ? – neilfws Jul 25 '19 at 23:48
  • I have checked my y is numeric. I get the same error when using M. Viking's code to plot the iris dataset. – CM3 Jul 25 '19 at 23:58
  • I had no issues with the iris example. Perhaps some option has been set for your local environment. In any case, we can't help further without data. – neilfws Jul 26 '19 at 01:07

1 Answers1

0

Without your data, I assume that you have set options() somewhere in your system, something like: options(digits = 19) although you might not have explicitly done that. To fix this problem, you can either change your default options() or just specify options() before this ggplot:

To reproduce this problem:

options(digits = 19) 

library(tidyverse)
x <- seq(from = -.03, to = 0.03, 0.001) 
y <- (1.01*x) 
z <- y>0
data <- data.frame(y, x,z)

ggplot(data, aes(x, y, col = z)) + 
  geom_point(size = 2) + 
  scale_y_continuous(
    limits = c(-0.03, 0.03), breaks = seq(-0.03, 0.03, 0.01))

To fix the above problem:

options(digits = 5) 
ggplot(data, aes(x, y, col = z)) + 
  geom_point(size = 2) + 
  scale_y_continuous(
    limits = c(-0.03, 0.03), breaks = seq(-0.03, 0.03, 0.01)) 
Zhiqiang Wang
  • 6,206
  • 2
  • 13
  • 27