英文:
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 <- 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()
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 <- 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))
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论