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

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

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

问题

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

  1. An extra character of "1" is printing on the figure. Why so and how to remove this?
  2. MWE:
  3. ```R
  4. data <- ChickWeight
  5. par(mar = c(4,4,0.5,0.5) + 0.1, mgp = c(2.75,1,0), las = 1, cex = 1.2)
  6. data.lm <- lm(weight ~ Time, data = data)
  7. plot(weight ~ Time, data = data, col = "magenta", pch = 19, ylab = "Weight (gms)", xlab = "Time (days)", ylim = c(25, 375), xlim=c(0,22))
  8. abline(data.lm, col="blue", lty = 2, lwd = 2)
  9. a <- round(data.lm$coefficients[[1]], 2)
  10. b <- round(data.lm$coefficients[[2]], 2)
  11. # 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?
  12. err <- round(mean(data.lm$residuals),17)
  13. # equation of the line :
  14. eq <- paste("Weight =",a,"+",b,"× Time",err,cex=1)
  15. 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:

  1. data <- ChickWeight
  2. par(mar = c(4,4,0.5,0.5) + 0.1, mgp = c(2.75,1,0), las = 1, cex = 1.2)
  3. data.lm <- lm(weight ~ Time, data = data)
  4. plot(weight ~ Time, data = data, col = "magenta", pch = 19, ylab = "Weight (gms)", xlab = "Time (days)", ylim = c(25, 375), xlim=c(0,22))
  5. abline(data.lm, col="blue", lty = 2, lwd = 2)
  6. a <- round(data.lm$coefficients[[1]], 2)
  7. b <- round(data.lm$coefficients[[2]], 2)
  8. # 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?
  9. err <- round(mean(data.lm$residuals),17)
  10. # equation of the line :
  11. eq <- paste("Weight =",a,"+",b,"\u00D7 Time",err,cex=1)
  12. mtext(paste(eq),side=3,line=-1.1,padj=-0.25,adj=0.05,cex=0.8)

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

答案1

得分: 1

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

  1. data <- ChickWeight
  2. par(mar = c(4,4,0.5,0.5) + 0.1, mgp = c(2.75,1,0), las = 1, cex = 1.2)
  3. data.lm <- lm(weight ~ Time, data = data)
  4. plot(weight ~ Time, data = data, col = "magenta", pch = 19, ylab = "Weight (克)", xlab = "时间 (天)", ylim = c(25, 375), xlim=c(0,22))
  5. abline(data.lm, col="blue", lty = 2, lwd = 2)
  6. a <- round(data.lm$coefficients[[1]], 2)
  7. b <- round(data.lm$coefficients[[2]], 2)
  8. # err <- format(mean(data.lm$residuals), scientific = TRUE, digits = 3) # 它不能正常工作,在方程末尾留下一个"1"。不知道为什么?
  9. err <- round(mean(data.lm$residuals),17)
  10. # 线的方程:
  11. eq <- paste("Weight =",a,"+",b,"\u00D7 Time",err)
  12. 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:

  1. data &lt;- ChickWeight
  2. par(mar = c(4,4,0.5,0.5) + 0.1, mgp = c(2.75,1,0), las = 1, cex = 1.2)
  3. data.lm &lt;- lm(weight ~ Time, data = data)
  4. 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))
  5. abline(data.lm, col=&quot;blue&quot;, lty = 2, lwd = 2)
  6. a &lt;- round(data.lm$coefficients[[1]], 2)
  7. b &lt;- round(data.lm$coefficients[[2]], 2)
  8. # 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?
  9. err &lt;- round(mean(data.lm$residuals),17)
  10. # equation of the line :
  11. eq &lt;- paste(&quot;Weight =&quot;,a,&quot;+&quot;,b,&quot;\u00D7 Time&quot;,err)
  12. 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

已解决:

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

Solved:

  1. # equation of the line :
  2. # Delete cex=1 here
  3. # eq &lt;- paste(&quot;Weight =&quot;,a,&quot;+&quot;,b,&quot;\u00D7 Time&quot;,err,cex=1)
  4. 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:

确定