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

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

Why are they getting joined? The values are different

问题

X <- c('1.07', '2.14', '3.22', '4.29')
Y <- c(0.298, 0.272, 0.271, 0.270)
Y1 <- c(0.274, 0.269, 0.255, 0.249)

par(mar = c(0.1, 0.1, 0.1, 0.1) + 2)

plot(X, Y, xlab= "VG高度(毫米)", ylab= "阻力系数(Cd)",
      main = "VG高度 vs. 阻力系数", type= "o", pch = 20,
      lwd = 2, col = "red")

par(new = TRUE)

plot(X, Y1, xlab= "", ylab= "", main = "", axes = FALSE, type= "o",
    pch = 20, lwd = 2, col = "blue")

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

英文:
X &lt;- c(&#39;1.07&#39;, &#39;2.14&#39;, &#39;3.22&#39;, &#39;4.29&#39;)
Y &lt;- c(0.298, 0.272, 0.271, 0.270)
Y1 &lt;- c(0.274, 0.269, 0.255, 0.249)

par(mar = c(0.1, 0.1, 0.1, 0.1) + 2)   

plot(X, Y, xlab= &quot;Height of the VG (in mm)&quot;, ylab= &quot;Drag Co-efficient (Cd)&quot;, 
      main = &quot;Height of the VG vs. Drag Co-efficient&quot;, type= &quot;o&quot;, pch = 20, 
      lwd = 2, col = &quot;red&quot;)

par(new = TRUE) 

plot(X, Y1, xlab= &quot;&quot;, ylab= &quot;&quot;, main = &quot;&quot;, axes = FALSE, type= &quot;o&quot;, 
    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 的常用值,然后在图表中使用它。

X &lt;- c('1.07', '2.14', '3.22', '4.29')
Y &lt;- c(0.298, 0.272, 0.271, 0.270)
Y1 &lt;- c(0.274, 0.269, 0.255, 0.249)

# 保存默认值
old_par &lt;- par(mar = c(0.1, 0.1, 0.1, 0.1) + 2)

# 定义y轴限制
ylim &lt;- range(c(Y, Y1))

plot(
  X, Y, 
  # 设置y轴限制
  ylim = ylim,
  xlab = "VG的高度(毫米)", 
  ylab = "阻力系数(Cd)", 
  main = "VG高度与阻力系数", 
  type = "o", 
  pch = 20, 
  lwd = 2, 
  col = "red"
)

par(new = TRUE)

plot(
  X, Y1,
  # 设置y轴限制为
  # 与上述相同的值
  ylim = ylim,
  xlab = "", 
  ylab = "", 
  main = "", 
  axes = FALSE, 
  type = "o", 
  pch = 20, 
  lwd = 2, 
  col = "blue"
)
英文:

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.

X &lt;- c(&#39;1.07&#39;, &#39;2.14&#39;, &#39;3.22&#39;, &#39;4.29&#39;)
Y &lt;- c(0.298,0.272,0.271,0.270)
Y1 &lt;- c(0.274,0.269,0.255,0.249)

# save the default values
old_par &lt;- par(mar = c(0.1, 0.1, 0.1, 0.1) + 2)   

# define the y axis limits
ylim &lt;- range(c(Y, Y1))

plot(
  X, Y, 
  # set the y axis limits
  ylim = ylim,
  xlab = &quot;Height of the VG (in mm)&quot;, 
  ylab= &quot;Drag Co-efficient (Cd)&quot;, 
  main = &quot;Height of the VG vs. Drag Co-efficient&quot;,
  type= &quot;o&quot;,
  pch = 20, 
  lwd = 2, 
  col = &quot;red&quot;
)

par(new = TRUE) 

plot(
  X, Y1,
  # set the y axis limits to the
  # same value as above
  ylim = ylim,
  xlab= &quot;&quot;, 
  ylab= &quot;&quot;, 
  main = &quot;&quot;, 
  axes = FALSE, 
  type = &quot;o&quot;, pch = 20, 
  lwd = 2, 
  col = &quot;blue&quot;
)

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


# restore default values
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:

确定