R:以时间序列绘制重复图

时间:2013-08-30 15:46:42

标签: r ggplot2 time-series

准备一个愚蠢的问题......

我有一大堆看起来有点像这样的数据..

structure(list(V1 = structure(c(4L, 3L, 6L, 5L, 1L, 2L), .Label = c("1012", 
"225", "58", "602", "62", "818"), class = "factor"), V2 = structure(c(4L, 
3L, 6L, 5L, 1L, 2L), .Label = c("1012", "249", "58", "603", "62", 
"824"), class = "factor"), V3 = structure(c(6L, 2L, 5L, 4L, 1L, 
3L), .Label = c("1014", "117", "290", "442", "831", "992"), class = "factor"), 
V4 = structure(c(6L, 3L, 5L, 2L, 1L, 4L), .Label = c("1033", 
"1055", "166", "377", "831", "992"), class = "factor"), V5 = structure(c(3L, 
4L, 6L, 2L, 1L, 5L), .Label = c("1033", "1067", "1575", "190", 
"378", "832"), class = "factor"), V6 = structure(c(3L, 4L, 
6L, 2L, 1L, 5L), .Label = c("1034", "1069", "1575", "221", 
"379", "833"), class = "factor"), V7 = structure(c(3L, 5L, 
6L, 2L, 1L, 4L), .Label = c("1063", "1092", "2351", "379", 
"406", "834"), class = "factor")), .Names = c("V1", "V2", 
"V3", "V4", "V5", "V6", "V7"), class = "data.frame", row.names = c(NA, 
6L))

每行代表一个主题,沿列移动的每个值代表一只老鼠按下一个杠杆的会话内的时间(以秒为单位)。我想用ggplot来制作一个类似于this的数字。然而我似乎无法弄清楚如何绘制时间序列,因为ggplot似乎想要一个离散的,名为x和y。我可以想到劳动密集型的方法让它发挥作用,但我知道我只是缺少一些简单的东西。

1 个答案:

答案 0 :(得分:0)

DWin在上面的评论中提供了答案。我想我可以稍微扩展一下,以防万一有人因为缺乏经验而偶然发现这一点。我的解决方案是安装reshapeGUI包作为学习参数结构的快速方法。

加载GUI后,我拿了上面的数据,将行号放到一个名为“Subjects”的新列中并运行以下

test.melt <- melt(data = test, id.vars=c('Subjects'),     
measure.vars=c('V1','V2','V3','V4','V5','V6','V7'))

给了我一个下面描述的结构,它很容易被绘制成一个时间序列,显示每个重复/主题的值......

structure(list(subject = c("1", "2", "3", "4", "5", "6", "1", 
"2", "3", "4", "5", "6", "1", "2", "3", "4", "5", "6", "1", "2", 
"3", "4", "5", "6", "1", "2", "3", "4", "5", "6", "1", "2", "3", 
"4", "5", "6", "1", "2", "3", "4", "5", "6"), response = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 
6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L), .Label = c("V1", "V2", "V3", 
"V4", "V5", "V6", "V7"), class = "factor"), time = c("602", "58", 
"818", "62", "1012", "225", "603", "58", "824", "62", "1012", 
"249", "992", "117", "831", "442", "1014", "290", "992", "166", 
"831", "1055", "1033", "377", "1575", "190", "832", "1067", "1033", 
"378", "1575", "221", "833", "1069", "1034", "379", "2351", "406", 
"834", "1092", "1063", "379")), .Names = c("subject", "response", 
"time"), row.names = c(NA, -42L), class = "data.frame")

感谢DWin。