绘制多个图的平均值的图
plot graph of average of multiple graphs
我有以下代码。
bereik = c(0, 0.05, 0.5, 0.95, 1)
#xvalues of plots
xval1 = c(0, 400, 4000, 10000, 10960)
xval2 = c(0, 1101, 2760, 4550, 10960)
xval3 = c(0, 1000, 3000, 5000, 10960)
xval4 = c(0, 2000, 5000, 8000, 10960)
xval5 = c(0, 400, 1250, 5000, 10960)
#plot 5 graphs
plot(xval1, bereik, type="l", col ="blue")
par(new=TRUE)
plot(xval2, bereik, type="l", col="red")
par(new=TRUE)
plot(xval3, bereik, type="l", col="yellow")
par(new=TRUE)
plot(xval4, bereik, type="l", col="green")
par(new=TRUE)
plot(xval5, bereik, type="l", col="purple")
我怎样才能在这个图中包含第六张图,它是 5 个给定图的平均值。也就是说,调用 graph1 F1,graph2 F2,...,图 5 F5,如何在图中包含图 F6 = 0.2F1 + 0.2F2 + ... + 0.2* F5?
这样做的一种方法是创建一个宽数据框,并使用 rowwise()
和 dplyr
中的 mutate()
创建一个包含均值的新列。
library(dplyr)
df <- data.frame(bereik, xval1, xval2, xval3, xval4, xval5)
df <- df %>%
rowwise() %>%
mutate(xmean = mean(c(xval1, xval2, xval3, xval4, xval5)))
plot(df$xmean, df$bereik, type="l")
您可以 cbind
向量到矩阵,然后 cbind
rowMeans
。制作一个空 plot
并在之后的 for
循环中使用 lines
。
mat <- do.call(cbind, mget(ls(pattern='^xval\d$')))
(mat <- cbind(mat, ave=rowMeans(mat)))
# xval1 xval2 xval3 xval4 xval5 ave
# [1,] 0 0 0 0 0 0.0
# [2,] 400 1101 1000 2000 400 980.2
# [3,] 4000 2760 3000 5000 1250 3202.0
# [4,] 10000 4550 5000 8000 5000 6510.0
# [5,] 10960 10960 10960 10960 10960 10960.0
plot(xval1, bereik, type="n", xlab='xval')
for (i in seq_len(ncol(mat))) lines(mat[, i], bereik, col=i)
legend('bottomright', lty=1, col=seq_len(ncol(mat)), legend=colnames(mat))
编辑
也许,您可能想尝试将 bereik 插入到唯一的 xval 中(即反过来),看看它是否更好。
mat2 <- do.call(cbind, mget(ls(pattern='^xval\d$')))
xval <- sort(unique(as.vector(mat2)))
f <- \(x) approx(x, bereik, xval)$y
(mat2_ip <- `rownames<-`(apply(mat2, 2, f), xval))
# xval1 xval2 xval3 xval4 xval5
# 0 0.000000 0.00000000 0.0000000 0.0000000 0.0000000
# 400 0.050000 0.01816530 0.0200000 0.0100000 0.0500000
# 1000 0.125000 0.04541326 0.0500000 0.0250000 0.3676471
# 1101 0.137625 0.05000000 0.0727250 0.0275250 0.4211176
# 1250 0.156250 0.09041591 0.1062500 0.0312500 0.5000000
# 2000 0.250000 0.29385172 0.2750000 0.0500000 0.5900000
# 2760 0.345000 0.50000000 0.4460000 0.1640000 0.6812000
# 3000 0.375000 0.56033520 0.5000000 0.2000000 0.7100000
# 4000 0.500000 0.81173184 0.7250000 0.3500000 0.8300000
# 4550 0.541250 0.95000000 0.8487500 0.4325000 0.8960000
# 5000 0.575000 0.95351014 0.9500000 0.5000000 0.9500000
# 8000 0.800000 0.97691108 0.9751678 0.9500000 0.9751678
# 10000 0.950000 0.99251170 0.9919463 0.9837838 0.9919463
# 10960 1.000000 1.00000000 1.0000000 1.0000000 1.0000000
plot(xval1, bereik, type="n", xlab='xval')
for (i in seq_len(ncol(mat2))) lines(mat2[, i], bereik, col=8)
lines(xval, rowMeans(mat2_ip), type='l', col='red')
我有以下代码。
bereik = c(0, 0.05, 0.5, 0.95, 1)
#xvalues of plots
xval1 = c(0, 400, 4000, 10000, 10960)
xval2 = c(0, 1101, 2760, 4550, 10960)
xval3 = c(0, 1000, 3000, 5000, 10960)
xval4 = c(0, 2000, 5000, 8000, 10960)
xval5 = c(0, 400, 1250, 5000, 10960)
#plot 5 graphs
plot(xval1, bereik, type="l", col ="blue")
par(new=TRUE)
plot(xval2, bereik, type="l", col="red")
par(new=TRUE)
plot(xval3, bereik, type="l", col="yellow")
par(new=TRUE)
plot(xval4, bereik, type="l", col="green")
par(new=TRUE)
plot(xval5, bereik, type="l", col="purple")
我怎样才能在这个图中包含第六张图,它是 5 个给定图的平均值。也就是说,调用 graph1 F1,graph2 F2,...,图 5 F5,如何在图中包含图 F6 = 0.2F1 + 0.2F2 + ... + 0.2* F5?
这样做的一种方法是创建一个宽数据框,并使用 rowwise()
和 dplyr
中的 mutate()
创建一个包含均值的新列。
library(dplyr)
df <- data.frame(bereik, xval1, xval2, xval3, xval4, xval5)
df <- df %>%
rowwise() %>%
mutate(xmean = mean(c(xval1, xval2, xval3, xval4, xval5)))
plot(df$xmean, df$bereik, type="l")
您可以 cbind
向量到矩阵,然后 cbind
rowMeans
。制作一个空 plot
并在之后的 for
循环中使用 lines
。
mat <- do.call(cbind, mget(ls(pattern='^xval\d$')))
(mat <- cbind(mat, ave=rowMeans(mat)))
# xval1 xval2 xval3 xval4 xval5 ave
# [1,] 0 0 0 0 0 0.0
# [2,] 400 1101 1000 2000 400 980.2
# [3,] 4000 2760 3000 5000 1250 3202.0
# [4,] 10000 4550 5000 8000 5000 6510.0
# [5,] 10960 10960 10960 10960 10960 10960.0
plot(xval1, bereik, type="n", xlab='xval')
for (i in seq_len(ncol(mat))) lines(mat[, i], bereik, col=i)
legend('bottomright', lty=1, col=seq_len(ncol(mat)), legend=colnames(mat))
编辑
也许,您可能想尝试将 bereik 插入到唯一的 xval 中(即反过来),看看它是否更好。
mat2 <- do.call(cbind, mget(ls(pattern='^xval\d$')))
xval <- sort(unique(as.vector(mat2)))
f <- \(x) approx(x, bereik, xval)$y
(mat2_ip <- `rownames<-`(apply(mat2, 2, f), xval))
# xval1 xval2 xval3 xval4 xval5
# 0 0.000000 0.00000000 0.0000000 0.0000000 0.0000000
# 400 0.050000 0.01816530 0.0200000 0.0100000 0.0500000
# 1000 0.125000 0.04541326 0.0500000 0.0250000 0.3676471
# 1101 0.137625 0.05000000 0.0727250 0.0275250 0.4211176
# 1250 0.156250 0.09041591 0.1062500 0.0312500 0.5000000
# 2000 0.250000 0.29385172 0.2750000 0.0500000 0.5900000
# 2760 0.345000 0.50000000 0.4460000 0.1640000 0.6812000
# 3000 0.375000 0.56033520 0.5000000 0.2000000 0.7100000
# 4000 0.500000 0.81173184 0.7250000 0.3500000 0.8300000
# 4550 0.541250 0.95000000 0.8487500 0.4325000 0.8960000
# 5000 0.575000 0.95351014 0.9500000 0.5000000 0.9500000
# 8000 0.800000 0.97691108 0.9751678 0.9500000 0.9751678
# 10000 0.950000 0.99251170 0.9919463 0.9837838 0.9919463
# 10960 1.000000 1.00000000 1.0000000 1.0000000 1.0000000
plot(xval1, bereik, type="n", xlab='xval')
for (i in seq_len(ncol(mat2))) lines(mat2[, i], bereik, col=8)
lines(xval, rowMeans(mat2_ip), type='l', col='red')