问题地绘制时间序列折线图

时间:2019-08-14 15:33:17

标签: r json web-scraping time-series plotly

我有一些代码可以从API中提取储层高度。我已经对数据进行了排序并绘制了图表。当我在本机R中绘制它时,它看起来很好,并且我期望如何:

enter image description here

当我使用plotly绘制时,得到以下信息:

enter image description here

y轴似乎杂乱无章,顶部有些初始值较低?我想解决这个问题,并基本上匹配使用plot()函数产生的结果。任何帮助,将不胜感激。

library("rjson")
library('jsonlite')
library('plotly')

CurrentDate = format(Sys.Date(), "%m-%d-%Y")
StartDate = "1850-01-01"
EndDate = CurrentDate

urlWithoutStart = 
paste("https://water.usbr.gov/api/web/app.php/api/series? 
sites=hdmlc&parameters=Day.Inst.ReservoirElevation.feet,",
                    "Day.Sum.ReservoirRelease- 
Powerplant.af,Day.Avg.ReservoirRelease- 
Powerplant.cfs,Day.Inst.ReservoirStorage.af&",
                    "start=",sep="")

urlWithoutEnd = paste(urlWithoutStart,StartDate,"&end=",sep="")

urlFull = paste(urlWithoutEnd,EndDate,"&format=json",sep="")

LakeMeadAllData = fromJSON(urlFull)

LakeMeadElevation = as.data.frame(LakeMeadAllData[[1]][[18]][[2]][[1]] 
[[1]])

LakeMeadElevation$datetime = as.Date(LakeMeadElevation$datetime)

names(LakeMeadElevation) = c("Date","Elevation","Flag")

LakeMeadElevation <- LakeMeadElevation[order(LakeMeadElevation$Date),] 

plot_ly(LakeMeadElevation,x=as.Date(LakeMeadElevation$Date),y=
LakeMeadElevation$Elevation,type = 'scatter',mode="line")

plot(LakeMeadElevation$Date,LakeMeadElevation$Elevation,type='l')

1 个答案:

答案 0 :(得分:1)

将y设置为y = as.numeric(LakeMeadElevation $ Elevation)解决了该问题。