科学计数法在 R 中为什么在 e 后多出一个字符 “1”?

huangapple go评论95阅读模式
英文:

why scientific notation leaves an extra character "1" after e in R?

问题

以下是您要翻译的代码部分:

An extra character of "1" is printing on the figure. Why so and how to remove this?

MWE:

```R
data <- ChickWeight

par(mar = c(4,4,0.5,0.5) + 0.1, mgp = c(2.75,1,0), las = 1, cex = 1.2)

data.lm <- lm(weight ~ Time, data = data)

plot(weight ~ Time, data = data, col = "magenta", pch = 19, ylab = "Weight (gms)", xlab = "Time (days)", ylim = c(25, 375), xlim=c(0,22))

abline(data.lm, col="blue", lty = 2, lwd = 2)

a <- round(data.lm$coefficients[[1]], 2)
b <- round(data.lm$coefficients[[2]], 2)

# err <- format(mean(data.lm$residuals), scientific = TRUE, digits = 3) # its not working leaving a "1" at the end of the equation. No idea, why?

err <- round(mean(data.lm$residuals),17)
# equation of the line : 
eq <- paste("Weight =",a,"+",b,"× Time",err,cex=1)

mtext(paste(eq),side=3,line=-1.1,padj=-0.25,adj=0.05,cex=0.8)
英文:

An extra character of "1" is printing on the figure. Why so and how to remove this?

MWE:

data <- ChickWeight

par(mar = c(4,4,0.5,0.5) + 0.1, mgp = c(2.75,1,0), las = 1, cex = 1.2)

data.lm <- lm(weight ~ Time, data = data)

plot(weight ~ Time, data = data, col = "magenta", pch = 19, ylab = "Weight (gms)", xlab = "Time (days)", ylim = c(25, 375), xlim=c(0,22))

abline(data.lm, col="blue", lty = 2, lwd = 2)

a <- round(data.lm$coefficients[[1]], 2)
b <- round(data.lm$coefficients[[2]], 2)

# err <- format(mean(data.lm$residuals), scientific = TRUE, digits = 3) # its not working leaving a "1" at the end of the equation. No idea, why?

err <- round(mean(data.lm$residuals),17)
# equation of the line : 
eq <- paste("Weight =",a,"+",b,"\u00D7 Time",err,cex=1)

mtext(paste(eq),side=3,line=-1.1,padj=-0.25,adj=0.05,cex=0.8)

科学计数法在 R 中为什么在 e 后多出一个字符 “1”?

答案1

得分: 1

你可以在mtext中像这样使用cex

data <- ChickWeight

par(mar = c(4,4,0.5,0.5) + 0.1, mgp = c(2.75,1,0), las = 1, cex = 1.2)

data.lm <- lm(weight ~ Time, data = data)

plot(weight ~ Time, data = data, col = "magenta", pch = 19, ylab = "Weight (克)", xlab = "时间 (天)", ylim = c(25, 375), xlim=c(0,22))

abline(data.lm, col="blue", lty = 2, lwd = 2)

a <- round(data.lm$coefficients[[1]], 2)
b <- round(data.lm$coefficients[[2]], 2)

# err <- format(mean(data.lm$residuals), scientific = TRUE, digits = 3) # 它不能正常工作,在方程末尾留下一个"1"。不知道为什么?

err <- round(mean(data.lm$residuals),17)
# 线的方程: 
eq <- paste("Weight =",a,"+",b,"\u00D7 Time",err)

mtext(paste(eq),side=3,line=-1.1,padj=-0.25,adj=0.05,cex=1)

科学计数法在 R 中为什么在 e 后多出一个字符 “1”?<!-- -->

<sup>2023-05-17创建,使用reprex v2.0.2</sup>

英文:

You could use the cex once in your mtext like this:

data &lt;- ChickWeight

par(mar = c(4,4,0.5,0.5) + 0.1, mgp = c(2.75,1,0), las = 1, cex = 1.2)

data.lm &lt;- lm(weight ~ Time, data = data)

plot(weight ~ Time, data = data, col = &quot;magenta&quot;, pch = 19, ylab = &quot;Weight (gms)&quot;, xlab = &quot;Time (days)&quot;, ylim = c(25, 375), xlim=c(0,22))

abline(data.lm, col=&quot;blue&quot;, lty = 2, lwd = 2)

a &lt;- round(data.lm$coefficients[[1]], 2)
b &lt;- round(data.lm$coefficients[[2]], 2)

# err &lt;- format(mean(data.lm$residuals), scientific = TRUE, digits = 3) # its not working leaving a &quot;1&quot; at the end of the equation. No idea, why?

err &lt;- round(mean(data.lm$residuals),17)
# equation of the line : 
eq &lt;- paste(&quot;Weight =&quot;,a,&quot;+&quot;,b,&quot;\u00D7 Time&quot;,err)

mtext(paste(eq),side=3,line=-1.1,padj=-0.25,adj=0.05,cex=1)

科学计数法在 R 中为什么在 e 后多出一个字符 “1”?<!-- -->

<sup>Created on 2023-05-17 with reprex v2.0.2</sup>

答案2

得分: 0

已解决:

# 线性方程式:
# 在此处删除 cex=1
# eq &lt;- paste(&quot;Weight =&quot;,a,&quot;+&quot;,b,&quot;\u00D7 Time&quot;,err,cex=1)
eq &lt;- paste(&quot;Weight =&quot;,a,&quot;+&quot;,b,&quot;\u00D7 Time&quot;,err)
英文:

Solved:

# equation of the line : 
# Delete cex=1 here
# eq &lt;- paste(&quot;Weight =&quot;,a,&quot;+&quot;,b,&quot;\u00D7 Time&quot;,err,cex=1)
eq &lt;- paste(&quot;Weight =&quot;,a,&quot;+&quot;,b,&quot;\u00D7 Time&quot;,err)

huangapple
  • 本文由 发表于 2023年5月17日 17:52:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270780.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定