I am attempting to use seq()
to define my breaks on a plot. Dummy example below.
ggplot(data, aes(x=cat, y=dog) +
scale_y_continuous(breaks=seq(-0.3, 0.3, by=0.1))
For some reason, seq()
is giving me output numbers that are off by minute amounts. This behavior occurs within and outside of my plotting device. As evidenced below, it appears to be a problem with generating negative numbers. It can produce them, but that's where the issue appears.
seq(0.3, 0.9, by=0.1) # test with positives
seq(-0.3, 0.3, by = 0.1) # test with negatives
format(seq(-0.3, 0.3, by = 0.1), scientific = F) # show full number
I read the documentation and couldn't find anything talking about negatives so I'm not sure how to fix it. Is there something I'm doing wrong or excluding? Is there a workaround or another function I should be using?
edit Marked as duplicate but the duplicate doesn't explicitly provide a solution to this. Here's a few:
# i went with this solution as given in comments to keep it all contained within seq()
seq(-0.3, 0.3, length.out=7)
# from the answers
seq(-3, 3, by=1)/10
# didn't work for my case but should work as a general rule
round(x, digits=n) # x would be the seq(-0.3, 0.3, by = 0.1) and n=1 in my case)
For a workaround you could try seq(-3,3,1)/10