如何在轴上显示更多十进制数字?

时间:2018-04-07 14:24:13

标签: r plot

代码

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

产生

enter image description here

但较小的a值(例如10^-8)会产生以下结果:

enter image description here

如何在后一种情况下使y轴显示第一个图中的小数位? (我不使用ggplot2。)

2 个答案:

答案 0 :(得分:1)

我找到了你需要为你的情节添加更多小数的答案

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

答案 1 :(得分:1)

您可以使用 a b c 0 NaN NaN NaN 1 1.0 NaN NaN 2 1.0 1.0 NaN 3 0.0 1.0 NaN 4 0.0 0.0 0.0 formatC

pretty

labels w/12 digits