使用ggplot绘制多列的时间序列 [英] Plotting time series for multiple columns using ggplot
问题描述
我有一个数据帧,其中包含一列时间序列数据和9个其他变量,每次均具有信号强度值,请参见下文:
头(RTWP_Columns)周期开始时间DU0362U09A3 DU0362U09B3 DU0362U09C3 DU0362U21A1 DU0362U21A2 DU0362U21B1 DU0362U21B2 DU0362U21C11 01.16.2017 00:00:00 -104.54 -106.43 -104.40 -104.48 -103.04 -104.50 -103.58 -104.102 01.16.2017 00:15:00 -104.98 -106.49 -104.48 -104.47 -103.40 -104.50 -103.81 -104.223 01.16.2017 00:30:00 -105.34 -106.45 -104.50 -104.50 -103.23 -104.50 -104.01 -104.264 01.16.2017 00:45:00 -105.30 -106.48 -104.48 -104.50 -103.38 -104.41 -104.10 -104.325 01.16.2017 01:00:00 -104.99 -106.49 -104.50 -104.50 -103.44 -104.50 -104.36 -104.246 01.16.2017 01:15:00 -105.33 -106.49 -104.49 -104.50 -103.82 -104.50 -104.39 -104.39DU0362U21C21 -97.482 -99.183 -101.044 -101.765 -101.756 -101.97
我希望在单个图形中将每个变量绘制为一条线,并根据其变量名称为其着色.
使用mill的plot()和qplot()进行绘图是可以的,但是在美学方面,由于我是R新手,所以我遇到了很大的困难.
如何绘制数据框,使时间位于X轴上,并且各列显示为单独的线?既然我的时间很重要,这是我悲伤的原因吗?我为自己的时间创建了一个单独的posixct,但是我无法在ggplot中将其绘制出来.
下面是我的数据框的结构:
str(RTWP_Columns)'data.frame':672磅.10个变量中:$ Period.Start.Time:带672级的因子"01.16.2017 00:00:00",..:1 2 3 4 5 6 7 8 9 10 ...$ DU0362U09A3:编号-105 -105 -105 -105 -105 ...$ DU0362U09B3:编号-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:编号-104 -104 -104 -104 -104 ...$ DU0362U21C1:num -104 -104 -104 -104 -104 ...$ DU0362U21C2:编号-97.5 -99.2 -101 -101.8 -101.8 ...
我使用了以下绘图功能,但无法将其转换为等效的ggplot:
plot(times,RTWP_Columns $ DU0362U09A3,类型="l",lwd = 2,col ="red",xlab =时间",ylab ="RTWP(dBm)",main ="DU0362的RTWP图")行(次,RTWP_Columns $ DU0362U09B3,col ="green",lwd = 2)行(时间,RTWP_Columns $ DU0362U09C3,col ="blue",lwd = 2)行数(times,RTWP_Columns $ DU0362U21A1,col ="orange",lwd = 2)行数(times,RTWP_Columns $ DU0362U21A2,col ="purple",lwd = 2)行数(times,RTWP_Columns $ DU0362U21B1,col ="black",lwd = 2)行(时间,RTWP_Columns $ DU0362U21B2,col =青色",lwd = 2)行数(times,RTWP_Columns $ DU0362U21C1,col ="yellow",lwd = 2)行数(times,RTWP_Columns $ DU0362U21C2,col ="pink",lwd = 2)
其中时间与RTWP_Columns中的period.start.time相等.
如果您能提供正确方向的指针,我将不胜感激.
类似的东西
库(ggplot2)库(reshape2)熔化的<-熔化的(df,id ="Time")ggplot(meltdf,aes(x = Time,y = value,color = variable,group = variable))+ geom_line()
检查此项以获取详细信息在ggplot中绘制多个时间序列 >
I have a data frame which contains a column of time series data and 9 other variables with signal strength values for each time, see below:
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
I wish to plot each variable as a line in a single graph and colour them according to their variable name.
Plotting using the run of the mill plot() and qplot() is fine but when it comes to aesthetics I have great difficulty as I am a novice R user.
How can I plot my data frame such that time is on the X axis and my columns are shown as separate lines? Since my time is of type factor is this the cause of my grief? I have created a separate posixct for my time but I can't plot this either in ggplot.
Below is the structure of my dataframe:
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 ...
I've used the following plot function but I can't translate it to a ggplot equivalent:
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)
where times is my posixct equivalent of the period.start.time in RTWP_Columns.
If you could provide a pointer in the right direction I would be very grateful.
Something like this
library(ggplot2)
library(reshape2)
meltdf <- melt(df,id="Time")
ggplot(meltdf,aes(x=Time,y=value,colour=variable,group=variable)) + geom_line()
Check this for details Plotting multiple time-series in ggplot
这篇关于使用ggplot绘制多列的时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!