开发者

How do I plot a graph of values against non-continuous dates using R?

I have a list of about 50 values and corresponding 开发者_如何学编程non-continuous dates in a pdf of which I need to make a time series graph in R. How do I do this?

No response can be too detailed or basic. Thanks.


library(ggplot2)
library(chron)
dataset <- data.frame(Date = as.Date(chron(runif(50, 0, 365))), Value = rnorm(50))
ggplot(dataset, aes(x = Date, y = Value)) + geom_line()


I agree with PaulHurleyuk that your question is ambiguous because of the term "pdf". It's also ambiguous in how you want to represent the non-continuous aspect. If you want to just plot values as lines and ignore spacing but do not have NA values then this works:

dataset <- data.frame(Date = as.Date(Sys.Date()+sample(1:75, 50)),  
                      Value = rnorm(50))
plot(dataset[order(dataset[,1]), ], type="l")

If you want to have discontinuities at the date where there are NA values, and you want to have gaps in the plotted values, then:

dataset <- data.frame(Date = as.Date(Sys.Date()+1:50), Value = rnorm(50))  
dataset[sample(1:50, 10), 2] <- NA
plot(dataset[order(dataset[,1]), ], type="l")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜