无法将系列添加到情节

Cannot add series to plot

我需要帮助来制作一个非常简单的情节。它只是一个带有一组不同价格的伴随线的折线图(它们都是时间序列,每种商品都有一条线。X = 价格,Y = 时间)。所以我有一个遵循格式的数据集:

#Date    prices1   prices2

日期均为YYYY-MM-DD格式,价格两列为数字。我检查了所有三列的 class 以确保它们符合预期(分别为 "Date" 、 "numeric" 和 "numeric" )。还有一些我觉得应该提到的事情:

现在,我没有让它们成为时间序列对象,而是尝试将它们简单地绘制成普通数据框。我可以自己绘制 both of them ,但我不能为我的生活绘制一个并为另一个使用 lines() 函数。我可能会错过什么?如果 NA 是问题所在,那么为什么我无法使用 Quandl 数据绘制两线图,而我的测试数据却很好?

鉴于问题的情况,我决定分享Quandl脚本和测试脚本。

#Original Script with issues
#Retrieving Data1
library(dplyr)
library(zoo)
library("Quandl")

data.1 = Quandl("JODI/OIL_TCPRKL_VEN")
#Putting data in chronological order

      #not in order
      print(data.1$Date[1])
      print(data.1$Date[length(data.1$Date)])

data.1 = data.frame(
  data.1$Date[length(data.1$Date):1],
  data.1$Value[length(data.1$Value):1]
)
names(data.1) = c("Date", "Value1")

      #Now in order
      print(data.1$Date[1])
      print(data.1$Date[length(data.1$Date)])





#Retrieving data2
data.2 = Quandl("JODI/OIL_TCPRKB_IRQ")

      #not in order
      print(data.2$Date[1])
      print(data.2$Date[length(data.2$Date)])

data.2 = data.frame(
  data.2$Date[length(data.2$Date):1],
  data.2$Value[length(data.2$Value):1]
)
names(data.2) = c("Date", "Value2")

      #now in order 
      print(data.2$Date[1])
      print(data.2$Date[length(data.2$Date)])


#join the data
data.join = data.frame(full_join(data.1, data.2))


plot(data.join$Date, data.join$Value1,
     col = "blue",
     main = "Should have both lines",
     type = "l",
     sub = "only one of them shows up though. Why?",
     xlab = "Date",
     ylab = "Values")
lines(data.join$Value2)
#plot only has one line. Why??

这也是我制作的一个测试脚本,我似乎没有问题

library(dplyr)
library(zoo)


time.a = as.Date(c(10:30))
time.b = as.Date(c(20:40))
time.c = as.Date(c(30:50))

value.a = as.numeric(seq(10,30,1))
value.b = as.numeric(seq(20,60,2))
value.c = as.numeric(seq(20,30,.5))

    length(time.a)
    length(time.b)
    length(time.c)

    length(value.a)
    length(value.b)
    length(value.c)


    print(time.a)
    print(time.b)
    print(time.c)

    print(value.a)
    print(value.b)
    print(value.c)



data.a = data.frame(time.a, value.a)
data.b = data.frame(time.b, value.b)
data.c = data.frame(time.c, value.c)

names(data.a) = c("Date", "Value.a")
names(data.b) = c("Date", "Value.b")
names(data.c) = c("Date", "Value.c")

all.data = full_join(data.a, data.b)
all.data = full_join(all.data, data.c)


plot(all.data$Date, all.data$Value.a,
     type = "l",
     main = "plot",
     xlab = "Date",
     ylab = "Values")

lines(all.data$Date, all.data$Value.b,
      col = "blue")


lines(all.data$Date, all.data$Value.c,
      col = "red")

我真的很想了解为什么第一个脚本不起作用,而我的第二个脚本却起作用。任何帮助或提示将不胜感激。 Why doesn't it work?

您的问题是 y 范围。

使用您的示例数据:

df = structure(list(Date = structure(c(14275, 14303, 14334, 14364, 14395,
 14425), class = "Date"), Value1 = c(14347.197, 12856.3706, 14623.1995,
 13998.8553, 14381.6974, 13688.8295), Value2 = c(68603, 62440, 73439,
 69930, 72850, 73500)), .Names = c("Date", "Value1", "Value2"), row.names = 
 c(NA, 6L), class = "data.frame")

我们可以看到范围几乎没有重叠,因此您需要预先定义绘图的范围:

df_range = range(c(df$Value1, df$Value2), na.rm = T)
plot(df$Date, df$Value1, type = "l", ylim = df_range))
lines(df$Date, df$Value2, col = "firebrick4")