development

플롯 축에 과학적 표기법을 원하지 않음

big-blog 2020. 11. 15. 11:29
반응형

플롯 축에 과학적 표기법을 원하지 않음


plot명령을 사용하여 R에서 모든 종류의 산점도를 정기적으로 수행합니다 .

때로는 둘 다, 때로는 플롯 축 중 하나만 과학적 표기법으로 레이블이 지정됩니다. R이 과학적 표기법으로 전환하기로 결정한시기를 이해할 수 없습니다. 놀랍게도, 플롯에 레이블을 지정할 때 정상적인 사람이 과학적 표기법으로 쓸 수없는 숫자를 인쇄하는 경우가 많습니다. 예를 들어 5를 5e + 00으로 레이블링합니다. 로그 축이 1000까지 올라가고 있다고 가정 해 보겠습니다. 과학적 표기법은 이러한 "작은"숫자로 정당화되지 않습니다.

이 동작을 억제하고 싶습니다. 항상 R이 정수 값을 표시하기를 원합니다. 이것이 가능한가?

시도 options(scipen=10)했지만 5 대신 5.0을 쓰기 시작하고 다른 축에서 5는 여전히 5 등입니다. R 플롯에서 순수한 정수 값을 어떻게 가질 수 있습니까?

Windows 7에서 R 2.12.1을 사용하고 있습니다.


options(scipen=5)또는 다른 충분한 수를 사용하십시오 . scipen 옵션은 R이 과학적 표기법으로 전환 할 가능성을 결정하며 값이 높을수록 전환 가능성이 적습니다. 플롯을 만들기 전에 옵션을 설정하십시오. 여전히 과학적 표기법이있는 경우 더 높은 숫자로 설정하십시오.


축 레이블을 사용 format하거나 formatC에헴, 서식을 지정할 수 있습니다 .

정수의 경우

x <- 10 ^ (1:10)
format(x, scientific = FALSE)
formatC(x, digits = 0, format = "f")

숫자를 실제 정수로 변환 할 수있는 경우 (예 : 너무 크지 않음) 다음을 사용할 수도 있습니다.

formatC(x, format = "d")

축에 레이블을 가져 오는 방법은 사용중인 플로팅 시스템에 따라 다릅니다.


이 시도. 일부러 여러 부분을 나눠서 움직일 수 있도록했습니다.

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")

여기에 이미지 설명 입력


이를 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)

제공합니다 :

여기에 이미지 설명 입력

EDIT : if you want to have all of them in the same format, you can use the solution of @Richie to get them :

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))

You could try lattice:

require(lattice)
x <- 1:100000
y <- 1:100000
xyplot(y~x, scales=list(x = list(log = 10)), type="l")

여기에 이미지 설명 입력


The R graphics package has the function axTicks that returns the tick locations of the ticks that the axis and plot functions would set automatically. The other answers given to this question define the tick locations manually which might not be convenient in some situations.

myTicks = axTicks(1)
axis(1, at = myTicks, labels = formatC(myTicks, format = 'd'))

A minimal example would be

plot(10^(0:10), 0:10, log = 'x', xaxt = 'n')
myTicks = axTicks(1)
axis(1, at = myTicks, labels = formatC(myTicks, format = 'd'))

There is also an log parameter in the axTicks function but in this situation it does not need to be set to get the proper logarithmic axis tick location.


Normally setting axis limit @ max of your variable is enough

a <- c(0:1000000)
b <- c(0:1000000)

plot(a, b, ylim = c(0, max(b)))

참고URL : https://stackoverflow.com/questions/5963047/do-not-want-scientific-notation-on-plot-axis

반응형