0

我创建如下数据框:

                  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 循环实现了这一点,我只是想知道是否有更直接的方法可以做到这一点而无需旋转列。

4

1 回答 1

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])

在此处输入图像描述


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

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 会话中,并尝试您的代码、识别任何问题等等。

于 2013-03-21T17:01:06.873 回答