不要在绘图轴上使用科学记数法

67 math graphics r

我经常使用plot命令在R中做各种散点图.

有时两者,有时只有一个绘图轴用科学记数法标记.我不明白何时R决定改用科学记数法.令人惊讶的是,它通常会在标记绘图时打印出人类不会用科学记数法书写的数字,例如它将5标记为5e + 00.假设您的对数轴上升到1000,科学符号与这样的"小"数字是不合理的.

我想抑制那种行为,我总是希望R显示整数值.这可能吗?

我尝试options(scipen=10)但是它开始写5.0而不是5,而在另一个轴5仍然是5等.我怎样才能在我的R图中有纯整数值?

我在Windows 7上使用R 2.12.1.

Gre*_*now 50

使用options(scipen=5)或其他一些足够高的数字.scipen选项确定R切换到科学记数法的可能性,值越高,切换的可能性越小.在制作绘图之前设置选项,如果仍有科学记数法,请将其设置为更高的数字.

  • 如果您确实阅读过我的原始问题,那么您会读到“我尝试了options(scipen = 10),但随后它开始写5.0而不是5”。我希望数字写成5而不是5.0,依此类推。 (2认同)

Ric*_*ton 18

您可以使用format或者formatCahem,ahem,格式化轴标签.

对于整数,请尝试

x <- 10 ^ (1:10)
format(x, scientific = FALSE)
formatC(x, digits = 0, format = "f")
Run Code Online (Sandbox Code Playgroud)

如果数字可以转换为实际整数(即,不是太大),您也可以使用

formatC(x, format = "d")
Run Code Online (Sandbox Code Playgroud)

如何将标签放到轴上取决于您使用的绘图系统.

  • 这并不是特别有用,因为 OP 已经声明他们正在使用 `plot()` (2认同)

Jor*_*eys 11

您可以使用该axis()命令,例如:

x <- 1:100000
y <- 1:100000
marks <- c(0,20000,40000,60000,80000,100000)
plot(x,y,log="x",yaxt="n",type="l")
axis(2,at=marks,labels=marks)
Run Code Online (Sandbox Code Playgroud)

给出:

在此输入图像描述

编辑:如果你想让所有这些格式都相同,你可以使用@Richie的解决方案来获取它们:

x <- 1:100000
y <- 1:100000
format(y,scientific=FALSE)
plot(x,y,log="x",yaxt="n",type="l")
axis(2,at=marks,labels=format(marks,scientific=FALSE))
Run Code Online (Sandbox Code Playgroud)


bil*_*080 11

试试这个.我故意打破各个部分,以便你可以移动东西.

library(sfsmisc)

#Generate the data
x <- 1:100000
y <- 1:100000

#Setup the plot area
par(pty="m", plt=c(0.1, 1, 0.1, 1), omd=c(0.1,0.9,0.1,0.9))

#Plot a blank graph without completing the x or y axis
plot(x, y, type = "n", xaxt = "n", yaxt="n", xlab="", ylab="", log = "x", col="blue")
mtext(side=3, text="Test Plot", line=1.2, cex=1.5)

#Complete the x axis
eaxis(1, padj=-0.5, cex.axis=0.8)
mtext(side=1, text="x", line=2.5)

#Complete the y axis and add the grid
aty <- seq(par("yaxp")[1], par("yaxp")[2], (par("yaxp")[2] - par("yaxp")[1])/par("yaxp")[3])
axis(2, at=aty, labels=format(aty, scientific=FALSE), hadj=0.9, cex.axis=0.8, las=2)
mtext(side=2, text="y", line=4.5)
grid()

#Add the line last so it will be on top of the grid
lines(x, y, col="blue")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述