使用 ggplot 绘制多列的时间序列

The*_*oat 2 r time-series ggplot2

我有一个数据框,其中包含一列时间序列数据和 9 个其他变量,每次都有信号强度值,见下文:

    head(RTWP_Columns)
    Period.Start.Time DU0362U09A3 DU0362U09B3 DU0362U09C3 DU0362U21A1 DU0362U21A2 DU0362U21B1 DU0362U21B2 DU0362U21C1
1 01.16.2017 00:00:00     -104.54     -106.43     -104.40     -104.48     -103.04     -104.50     -103.58     -104.10
2 01.16.2017 00:15:00     -104.98     -106.49     -104.48     -104.47     -103.40     -104.50     -103.81     -104.22
3 01.16.2017 00:30:00     -105.34     -106.45     -104.50     -104.50     -103.23     -104.50     -104.01     -104.26
4 01.16.2017 00:45:00     -105.30     -106.48     -104.48     -104.50     -103.38     -104.41     -104.10     -104.32
5 01.16.2017 01:00:00     -104.99     -106.49     -104.50     -104.50     -103.44     -104.50     -104.36     -104.24
6 01.16.2017 01:15:00     -105.33     -106.49     -104.49     -104.50     -103.82     -104.50     -104.39     -104.39
  DU0362U21C2
1      -97.48
2      -99.18
3     -101.04
4     -101.76
5     -101.75
6     -101.97
Run Code Online (Sandbox Code Playgroud)

我希望将每个变量绘制为单个图形中的一条线,并根据变量名称为它们着色。

使用 mill plot() 和 qplot() 的运行进行绘图很好,但是在美学方面我有很大的困难,因为我是 R 新手用户。

如何绘制我的数据框,使时间在 X 轴上并且我的列显示为单独的行?既然我的时间是类型因素,这就是我悲伤的原因吗?我已经为我的时间创建了一个单独的 posixct,但我也无法在 ggplot 中绘制它。

以下是我的数据框的结构:

   str(RTWP_Columns)
'data.frame':   672 obs. of  10 variables:
 $ Period.Start.Time: Factor w/ 672 levels "01.16.2017 00:00:00",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ DU0362U09A3      : num  -105 -105 -105 -105 -105 ...
 $ DU0362U09B3      : num  -106 -106 -106 -106 -106 ...
 $ DU0362U09C3      : num  -104 -104 -104 -104 -104 ...
 $ DU0362U21A1      : num  -104 -104 -104 -104 -104 ...
 $ DU0362U21A2      : num  -103 -103 -103 -103 -103 ...
 $ DU0362U21B1      : num  -104 -104 -104 -104 -104 ...
 $ DU0362U21B2      : num  -104 -104 -104 -104 -104 ...
 $ DU0362U21C1      : num  -104 -104 -104 -104 -104 ...
 $ DU0362U21C2      : num  -97.5 -99.2 -101 -101.8 -101.8 ...
Run Code Online (Sandbox Code Playgroud)

我使用了以下 plot 函数,但无法将其转换为 ggplot 等效项:

plot(times,RTWP_Columns$DU0362U09A3,
       type = "l",
       lwd=2,col="red",
       xlab = "Time",
       ylab = "RTWP (dBm)",
       main = "RTWP Plot for DU0362")
lines(times,RTWP_Columns$DU0362U09B3, col="green",lwd=2)
lines(times,RTWP_Columns$DU0362U09C3, col="blue",lwd=2)
lines(times,RTWP_Columns$DU0362U21A1, col="orange",lwd=2)
lines(times,RTWP_Columns$DU0362U21A2, col="purple",lwd=2)
lines(times,RTWP_Columns$DU0362U21B1, col="black",lwd=2)
lines(times,RTWP_Columns$DU0362U21B2, col="cyan",lwd=2)
lines(times,RTWP_Columns$DU0362U21C1, col="yellow",lwd=2)
lines(times,RTWP_Columns$DU0362U21C2, col="pink",lwd=2)
Run Code Online (Sandbox Code Playgroud)

其中时间是我的 posixct 等效于 RTWP_Columns 中的 period.start.time。

如果您能提供正确方向的指针,我将不胜感激。

Man*_*dar 5

像这样的东西

library(ggplot2)
library(reshape2)
meltdf <- melt(df,id="Time")
ggplot(meltdf,aes(x=Time,y=value,colour=variable,group=variable)) + geom_line()
Run Code Online (Sandbox Code Playgroud)

检查此详细信息Plotting multiple time-series in ggplot