我想使用 ggplot2 绘制 3 条线。
我的数据看起来像这样
print(x)
V1 V2 V3 V4
1 -4800 25195.73 7415.219 7264.28
2 -2800 15195.73 5415.219 7264.28
从这个例子中,我知道我需要将我的数据 reshape 成这样的东西: id x y
1 1 -4800 25195.73
2 1 -2800 15195.73
3 2 -4800 7415.219
4 2 -2800 5415.219
5 3 -4800 7264.28
6 3 -2800 7264.28
我该如何进行这种 reshape ?
最佳答案
使用reshape2
library(reshape2)
x$id <- seq_len(nrow(x))
melted <- melt(x, id.vars = c('id','V1'))
# rename
names(melted) <- c('id', 'x', 'variable', 'y')
关于使用 ggplot2 reshape 数据以在 R 中绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13250872/