data.frame散点图

时间:2013-03-20 10:54:26

标签: r dataframe scatter-plot

我创建了一个数据帧,如下所示:

                  DATE        R1        R2       R3       R4        R5 HOUR    SVALUE
1  2003-01-25 00:00:00  352.2828  285.2375 141.5788 232.4384  356.0331    0  623.7061
13 2003-01-25 06:00:00 1085.2570  918.6110 989.0535 727.4486 1080.8820   06 1009.7621
25 2003-01-26 00:00:00  518.2674  934.3774 323.4014 542.6376  365.3300    0  475.1363
37 2003-01-26 06:00:00 1015.1920 1272.1173 941.1970 922.3203 1030.0044   06  679.9303
49 2003-01-27 00:00:00  637.3027  674.5031 457.4424 803.0685  547.2401    0  387.9804
61 2003-01-27 06:00:00  763.5950  639.5652 726.1546 528.9499  787.2823   06  692.8306

我想针对SVALUE列(在X轴上)生成所有列R1到R5(在Y轴上)的单个散点图(不是矩阵图)。所以R1-SVALUE,R2-SVALUE等......

我已经通过使用for循环实现了这一点,我只是想知道是否有更直接的方法可以在不旋转列的情况下这样做。

1 个答案:

答案 0 :(得分:1)

如果您以正确的格式获取数据,这实际上并不太难。最简单的方法是使用stack

修复您的数据

toPlot <- data.frame(SVALUE = temp$SVALUE, stack(temp[2:6]))
## Here are the first 10 rows
head(toPlot, 10)
#       SVALUE    values ind
# 1   623.7061  352.2828  R1
# 2  1009.7621 1085.2570  R1
# 3   475.1363  518.2674  R1
# 4   679.9303 1015.1920  R1
# 5   387.9804  637.3027  R1
# 6   692.8306  763.5950  R1
# 7   623.7061  285.2375  R2
# 8  1009.7621  918.6110  R2
# 9   475.1363  934.3774  R2
# 10  679.9303 1272.1173  R2

绘制新数据

plot(toPlot[1:2])

enter image description here


将来,如果您以下列形式共享数据会有所帮助:

temp <- structure(list(DATE = structure(1:6, .Label = c("2003-01-25 00:00:00", 
  "2003-01-25 06:00:00", "2003-01-26 00:00:00", "2003-01-26 06:00:00", 
  "2003-01-27 00:00:00", "2003-01-27 06:00:00"), class = "factor"), 
      R1 = c(352.2828, 1085.257, 518.2674, 1015.192, 637.3027, 
      763.595), R2 = c(285.2375, 918.611, 934.3774, 1272.1173, 
      674.5031, 639.5652), R3 = c(141.5788, 989.0535, 323.4014, 
      941.197, 457.4424, 726.1546), R4 = c(232.4384, 727.4486, 
      542.6376, 922.3203, 803.0685, 528.9499), R5 = c(356.0331, 
      1080.882, 365.33, 1030.0044, 547.2401, 787.2823), HOUR = c(0L, 
      6L, 0L, 6L, 0L, 6L), SVALUE = c(623.7061, 1009.7621, 475.1363, 
      679.9303, 387.9804, 692.8306)), .Names = c("DATE", "R1", 
  "R2", "R3", "R4", "R5", "HOUR", "SVALUE"), class = "data.frame", row.names = c("1", 
  "13", "25", "37", "49", "61"))
然后,其他人可以轻松地复制并粘贴到他们的R会话中并试用您的代码,识别任何问题,等等。