如何在R中使用ggplot绘制xts时间序列?

huangapple go评论72阅读模式
英文:

How to ggplot an xts time series in r?

问题

The call to plot() below works, while the call to ggplot() does not. I have very poor vision, perhaps I am missing something obvious?

library(ggplot2)
n=1000
data <- rnorm(n) #Error in rpois(n) : argument "lambda" is missing, with no default
dates <- seq(as.Date("2017-05-01"),length=n,by= "days")
ts <- xts(x=data, order.by=dates)
plot(ts)
ggplot(ts)

英文:

The call to plot() below works, while the call to ggplot() does not. I have very poor vision, perhaps I am missing something obvious?

library(ggplot2)
n=1000
data &lt;- rnorm(n) #Error in rpois(n) : argument &quot;lambda&quot; is missing, with no default
dates &lt;- seq(as.Date(&quot;2017-05-01&quot;),length=n,by= &quot;days&quot;)
ts &lt;- xts(x=data, order.by=dates)
plot(ts)
ggplot(ts)

答案1

得分: 0

以下是翻译好的部分:

我们可以这样做:

library(ggplot2)
library(xts)

n <- 1000
data <- rnorm(n)
dates <- seq(as.Date("2017-05-01"), length = n, by = "days")
ts <- xts(x = data, order.by = dates)

# 选项1:
autoplot(ts)

# 选项2,使用ggplot2:
ggplot(df, aes(x = Index, y = data)) + 
  geom_line() +
  labs(x = "Date", y = "Value")

如何在R中使用ggplot绘制xts时间序列?

英文:

We could do it this way:

library(ggplot2)
library(xts)

n &lt;- 1000
data &lt;- rnorm(n)
dates &lt;- seq(as.Date(&quot;2017-05-01&quot;), length = n, by = &quot;days&quot;)
ts &lt;- xts(x = data, order.by = dates)

# Option1: 
autoplot(ts)


# Option2 using ggplot2
ggplot(df, aes(x = Index, y = data)) + 
  geom_line() +
  labs(x = &quot;Date&quot;, y = &quot;Value&quot;)

如何在R中使用ggplot绘制xts时间序列?

huangapple
  • 本文由 发表于 2023年4月17日 03:12:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029863.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定