【发布时间】:2014-01-20 14:40:39
【问题描述】:
我有一个使用以下代码生成的数据框,
x <- c(1:10)
y <- x^3
z <- y-20
s <- z/3
t <- s*6
q <- s*y
x1 <- cbind(x,y,z,s,t,q)
x1 <- data.frame(x1)
数据框x1因此具有以下数据,
x y z s t q
1 1 1 -19 -6.333333 -38 -6.333333
2 2 8 -12 -4.000000 -24 -32.000000
3 3 27 7 2.333333 14 63.000000
4 4 64 44 14.666667 88 938.666667
5 5 125 105 35.000000 210 4375.000000
6 6 216 196 65.333333 392 14112.000000
7 7 343 323 107.666667 646 36929.666667
8 8 512 492 164.000000 984 83968.000000
9 9 729 709 236.333333 1418 172287.000000
10 10 1000 980 326.666667 1960 326666.666667
现在我想在同一个图中绘制列 x vs y、z vs s 和 t vs q,所以为此我使用以下代码,
p <- ggplot() +
geom_line(data = x1, aes(x = x1[,1], y = x1[,2], color = "red")) +
geom_line(data = x1, aes(x = x1[,3], y = x1[,4], color = "blue")) +
geom_line(data = x1, aes(x = x1[,5], y = x1[,6], color = "green")) +
xlab('x') +
ylab('y')
虽然上面的代码对于只有 6 列的数据框可以正常工作,但我想对有很多列的数据框执行相同的操作。例如,如果数据框中有 20 列,则应该生成一个包含 col 1 vs 2、col 3 vs 4、col 5 vs 6 等的图,直到 col 19 vs 20。为此,我使用下面这段代码,
p <- ggplot() + geom_line(data = x1, aes(x = x1[,1], y = x1[,2], color = "red")) + xlab('x') + ylab('y')
ctr <- 1
for (iz in seq(3, ncol(x1), by = 2))
{
p$ctr <- p + geom_line(data = x1, aes(x = x1[,iz], y = x1[,iz+1], color = "green"))
ctr <- ctr+1
}
因此,这些图应该逐渐分层,最后一个对象应该包含整个图。使用上面的代码,每次循环运行时,绘图都会被覆盖,有人可以指出如何捕获完整数据。我也想为每个情节显示一个图例。
谢谢
【问题讨论】:
标签: r loops plot ggplot2 legend