如何使用ggplot来绘制三个不同长度的系列

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

How to use ggplot for plotting three series of different length

问题

以下是代码的翻译部分:

library(ggplot2)

p <- c(1.98139389065475e-08, 3.96278778130931e-08, 7.92557556261902e-08,
       1.58511511252372e-07, 3.17023022504761e-07, 6.3404604500949e-07,
       1.26809209001904e-06, 2.53618418003796e-06, 5.07236836007617e-06,
       1.01447367201518e-05, 2.02894734403047e-05, 4.05789468806074e-05,
       8.11578937612187e-05, 0.00450692446282339, 0.0331829332673235,
       0.0324267128112546, 0.536844723615457, 0.29007911330816,
       0.0821356435493368, 0.0206616530120613)

px <- c(0.0265956149070529, 0.100877042157315, 0.331154315180395,
        0.51467306280507, 0.0147007753919285, 0.0119991895582382)

q <- c(0.01765199, 0.06695391, 0.21979307, 0.34159776, 0.00975717,
       0.2448383, 0.0994078)

df <- data.frame(x = 1:length(p), p = p, px = px, q = q)

ggplot(df, aes(x = x)) +
  geom_line(aes(y = p, color = "p")) +
  geom_line(aes(y = px, color = "px")) +
  geom_line(aes(y = q, color = "q")) +
  labs(x = "Index", y = "Value") +
  scale_color_manual(values = c("p" = "red", "px" = "blue", "q" = "green")) +
  theme_minimal()

希望这能帮助您解决问题。

英文:

The following code

library(ggplot2)

p &lt;- c(1.98139389065475e-08, 3.96278778130931e-08, 7.92557556261902e-08,
       1.58511511252372e-07, 3.17023022504761e-07, 6.3404604500949e-07,
       1.26809209001904e-06, 2.53618418003796e-06, 5.07236836007617e-06,
       1.01447367201518e-05, 2.02894734403047e-05, 4.05789468806074e-05,
       8.11578937612187e-05, 0.00450692446282339, 0.0331829332673235,
       0.0324267128112546, 0.536844723615457, 0.29007911330816,
       0.0821356435493368, 0.0206616530120613)

px &lt;- c(0.0265956149070529, 0.100877042157315, 0.331154315180395,
        0.51467306280507, 0.0147007753919285, 0.0119991895582382)

q &lt;- c(0.01765199, 0.06695391, 0.21979307, 0.34159776, 0.00975717,
       0.2448383, 0.0994078)

df &lt;- data.frame(x = 1:length(p), p = p, px = px, q = q)

ggplot(df, aes(x = x)) +
  geom_line(aes(y = p, color = &quot;p&quot;)) +
  geom_line(aes(y = px, color = &quot;px&quot;)) +
  geom_line(aes(y = q, color = &quot;q&quot;)) +
  labs(x = &quot;Index&quot;, y = &quot;Value&quot;) +
  scale_color_manual(values = c(&quot;p&quot; = &quot;red&quot;, &quot;px&quot; = &quot;blue&quot;, &quot;q&quot; = &quot;green&quot;)) +
  theme_minimal()

returns an error because, of course, the arguments of dataframe df imply a different number of rows: 20, 6, 7. How could I fix this problem? I would like to plot the series so that the p-series is drawn up to step 20, the q-series up to step 7 and the px-series up to step 6, i.e. up to their maximum length.

答案1

得分: 1

max_length <- max(sapply(list(p, px, q), length))
pad_vec <- function(x) { c(x, rep(NA, max_length - length(x)))}
df <- data.frame(x = 1:max_length, p = pad_vec(p), px = pad_vec(px), q = pad_vec(q))

英文:
max_length &lt;- max(sapply(list(p, px, q), length))
pad_vec &lt;- function(x) { c(x, rep(NA, max_length - length(x)))}
df &lt;- data.frame(x = 1:max_length, p = pad_vec(p), px = pad_vec(px), q = pad_vec(q))

But it would probably be more idiomatic for ggplot2 if you made one longer data frame stacking the series, with a column denoting which series each observation belongs to. Then you could use geom_line(aes(x = x, y = value, color = src)) in place of the three geom_line calls.

如何使用ggplot来绘制三个不同长度的系列

huangapple
  • 本文由 发表于 2023年6月22日 00:27:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76525386.html
匿名

发表评论

匿名网友

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

确定