使用 ggplot2 reshape 数据以在 R 中绘制

标签 r plot ggplot2 reshape

我想使用 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/

相关文章:

R ggplot反转轴并控制条形方向

R 在对维基百科的 readHTMLTable 调用时崩溃

r - 如何在 R 中插入数据

r - 在 R 中转换 data.frame

r - 使用 R 中的现有数据创建并填充新表

r - distr包-如何在一个窗口中绘制两个图?

python - sympy 函数图下方的颜色区域

c# - WPF 中的图形绘制(功能)

r - 在 R 中使用 facet_wrap 规范化 ggplot2 密度

r - ggplot2 facet_grid 与 facet 标题