r.e.s.
r.e.s.

Reputation: 250

How to show more decimal digits on an axis?

The code

a <- 10^-7
f <- function(x) 1+a*cos(x)
plot(f, 0, 2*pi)

produces

enter image description here

but smaller values of a (e.g. 10^-8) produce the following:

enter image description here

How can I make the y-axis in the latter case show the decimal places as in the first plot? (I do not use ggplot2.)

Upvotes: 3

Views: 4742

Answers (2)

Ott Toomet
Ott Toomet

Reputation: 1956

You can use formatC and pretty:

a <- 1e-10                             
plot(f, 0, 2*pi, yaxt="n")  # no default y axis
p <- pretty(par("usr")[3:4])  # find nice breaks at actual y range
l <- formatC(p, format="f", digits=12) # format w/12 digits
axis(2, at=p, labels=l)

labels w/12 digits

Upvotes: 1

Rafael D&#237;az
Rafael D&#237;az

Reputation: 2289

I found the answer you are needing to add more decimals to your plot

value = function(p){c(min(p$y),
                  mean(c(median(p$y),min(p$y))),
                  median(p$y),
                  mean(c(median(p$y),max(p$y))),
                  max(p$y))}

par(mar = c(5,5,4,2), mfrow = c(1,2))
options(digits=15, scipen = 999)
a <- 10^-7
f <- function(x) as.double(1+a*cos(x))

p1 = plot(f, 0, 2*pi, cex.axis = 0.7, las = 1, ylab = "", yaxt = "n", main = "a=10^-7")
axis(2, at = value(p1),labels = sprintf("%.8f", value(p1)), cex.axis = .7, las = 1)

a <- 10^-8
p2=plot(f, 0, 2*pi, cex.axis = 0.7, las = 1, ylab = "", yaxt = "n", main = "a=10^-8")
axis(2, at = value(p2),labels = sprintf("%.8f", value(p2)), cex.axis = .7, las = 1)

enter image description here

Upvotes: 1

Related Questions