为什么它们会被连接在一起?这些值是不同的。

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

Why are they getting joined? The values are different

问题

  1. X <- c('1.07', '2.14', '3.22', '4.29')
  2. Y <- c(0.298, 0.272, 0.271, 0.270)
  3. Y1 <- c(0.274, 0.269, 0.255, 0.249)
  4. par(mar = c(0.1, 0.1, 0.1, 0.1) + 2)
  5. plot(X, Y, xlab= "VG高度(毫米)", ylab= "阻力系数(Cd)",
  6. main = "VG高度 vs. 阻力系数", type= "o", pch = 20,
  7. lwd = 2, col = "red")
  8. par(new = TRUE)
  9. plot(X, Y1, xlab= "", ylab= "", main = "", axes = FALSE, type= "o",
  10. pch = 20, lwd = 2, col = "blue")

我尝试生成两个Y轴。但是,两条线都连接在一起,尽管它们具有不同的值。

英文:
  1. X &lt;- c(&#39;1.07&#39;, &#39;2.14&#39;, &#39;3.22&#39;, &#39;4.29&#39;)
  2. Y &lt;- c(0.298, 0.272, 0.271, 0.270)
  3. Y1 &lt;- c(0.274, 0.269, 0.255, 0.249)
  4. par(mar = c(0.1, 0.1, 0.1, 0.1) + 2)
  5. plot(X, Y, xlab= &quot;Height of the VG (in mm)&quot;, ylab= &quot;Drag Co-efficient (Cd)&quot;,
  6. main = &quot;Height of the VG vs. Drag Co-efficient&quot;, type= &quot;o&quot;, pch = 20,
  7. lwd = 2, col = &quot;red&quot;)
  8. par(new = TRUE)
  9. plot(X, Y1, xlab= &quot;&quot;, ylab= &quot;&quot;, main = &quot;&quot;, axes = FALSE, type= &quot;o&quot;,
  10. pch = 20, lwd = 2, col = &quot;blue&quot;)

I'm trying to generate two y axes. But, both the lines are getting joined, even though with different value.

答案1

得分: 1

问题出在y轴限制上,图表未使用相同的数值。首先创建一个名为 ylim 的向量,其中包含 YY1 的常用值,然后在图表中使用它。

  1. X &lt;- c('1.07', '2.14', '3.22', '4.29')
  2. Y &lt;- c(0.298, 0.272, 0.271, 0.270)
  3. Y1 &lt;- c(0.274, 0.269, 0.255, 0.249)
  4. # 保存默认值
  5. old_par &lt;- par(mar = c(0.1, 0.1, 0.1, 0.1) + 2)
  6. # 定义y轴限制
  7. ylim &lt;- range(c(Y, Y1))
  8. plot(
  9. X, Y,
  10. # 设置y轴限制
  11. ylim = ylim,
  12. xlab = "VG的高度(毫米)",
  13. ylab = "阻力系数(Cd)",
  14. main = "VG高度与阻力系数",
  15. type = "o",
  16. pch = 20,
  17. lwd = 2,
  18. col = "red"
  19. )
  20. par(new = TRUE)
  21. plot(
  22. X, Y1,
  23. # 设置y轴限制为
  24. # 与上述相同的值
  25. ylim = ylim,
  26. xlab = "",
  27. ylab = "",
  28. main = "",
  29. axes = FALSE,
  30. type = "o",
  31. pch = 20,
  32. lwd = 2,
  33. col = "blue"
  34. )
英文:

The problem is in the y axis limits, the plots are not using the same values. Below I first create a vector ylim with values common to Y and Y1, then use it in the plots.

  1. X &lt;- c(&#39;1.07&#39;, &#39;2.14&#39;, &#39;3.22&#39;, &#39;4.29&#39;)
  2. Y &lt;- c(0.298,0.272,0.271,0.270)
  3. Y1 &lt;- c(0.274,0.269,0.255,0.249)
  4. # save the default values
  5. old_par &lt;- par(mar = c(0.1, 0.1, 0.1, 0.1) + 2)
  6. # define the y axis limits
  7. ylim &lt;- range(c(Y, Y1))
  8. plot(
  9. X, Y,
  10. # set the y axis limits
  11. ylim = ylim,
  12. xlab = &quot;Height of the VG (in mm)&quot;,
  13. ylab= &quot;Drag Co-efficient (Cd)&quot;,
  14. main = &quot;Height of the VG vs. Drag Co-efficient&quot;,
  15. type= &quot;o&quot;,
  16. pch = 20,
  17. lwd = 2,
  18. col = &quot;red&quot;
  19. )
  20. par(new = TRUE)
  21. plot(
  22. X, Y1,
  23. # set the y axis limits to the
  24. # same value as above
  25. ylim = ylim,
  26. xlab= &quot;&quot;,
  27. ylab= &quot;&quot;,
  28. main = &quot;&quot;,
  29. axes = FALSE,
  30. type = &quot;o&quot;, pch = 20,
  31. lwd = 2,
  32. col = &quot;blue&quot;
  33. )

为什么它们会被连接在一起?这些值是不同的。<!-- -->

  1. # restore default values
  2. par(old_par)

<sup>Created on 2023-06-12 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年6月13日 03:20:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459714.html
匿名

发表评论

匿名网友

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

确定