簡體   English   中英

具有 2 個變量的 R 中的時間序列 plot

[英]Timeseries plot in R with 2 variables

我有這個數據集,我想 plot R 中的 score1 和 score2 的時間序列。 我可以知道我怎么能做到這一點?

DT                 Score1  Score2
12-01-21 1:30        1       4
12-02-22 1:30        5       4
12-12-21 1:30        1       4
01-01-22 12:30       0       3
03-02-22 1:00        3       1
03-02-22 1:14        4       1

使用plot()points()

plot(df$Score1 ~ df$DT)
points(df$Score1 ~ df$DT)

再做一些調整,讓它變得更好……你可以做的還有很多

plot(df$Score1 ~ df$DT, typ="l", col="black", 
     xlab="Date", ylab="Score")
points(df$Score1 ~ df$DT, typ="l", col="red")

或者對於 ggplot ( library(ggplot2) )版本(同樣,可以進行很多調整)

data1 %>%
  ggplot(mapping=aes(x=DT, y=Score1)) +
    geom_line() +
    geom_line(y=data1$Score2, col="red")

我將datetime分開,並使用geom_line()geom_step()基於time變量創建了 plot

library(ggplot2)
library(reshape)

data <-data.frame("DT"=c("12-01-21", "12-02-22", "12-12-21", "01-01-22", "03-02-22", "03-02-22"), 
                   "time"=c("1:30","1:30","1:30","12:30","1:00","1:14"),
                   "Score 1"=c(1,5,1,0,3,4),
                   "Score 2" =c (4,4,4,3,1,1))


df<-melt(data)

(plot<-ggplot(df, aes(x=time, y=value, group=variable)) +  
    geom_line(aes(color=variable), size=1)+theme_bw()) # the case of geom_step() is  geom_step(aes(color=variable), size=1)

幾何線() 在此處輸入圖像描述

geom_steps()

在此處輸入圖像描述

以及基於DT 變量facet_wrap

(plot<-ggplot(df, aes(x=time, y=value, group=variable)) +  
    geom_line(aes(color=variable), size=1)+theme_bw()+
  facet_wrap(~DT))

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM