我想使用 ggplot2 绘制 xts 对象,但出现错误。这是我正在做的事情:
dates <- c("2014-10-01", "2014-11-01", "2014-12-01", "2015-01-01", "2015-02-01")
value <- as.numeric(c(3, 4, 5, 6, 5))
new_df <- data_frame(dates, value)
new_df$dates <- as.Date(dates)
new_df <- as.xts(new_df[,-1], order.by = new_df$dates)
现在我尝试使用 ggplot2 绘制它:
ggplot(new_df, aes(x = index, y = value)) + geom_point()
我收到以下错误:
(函数(..., row.names = NULL, check.rows = FALSE,
check.names = TRUE, : 参数意味着不同的行数:0,
5
我不太确定我做错了什么。
The autoplot.zoo
Zoo 中的方法(zoo 由 xts 自动拉入)也将使用 ggplot2 为 xts 对象创建绘图。如果您需要额外的几何图形,它支持 ggplot2 的 +...。看?autoplot.zoo
library(xts)
library(ggplot2)
x_xts <- xts(1:4, as.Date("2000-01-01") + 1:4) # test data
autoplot(x_xts, geom = "point")
动物园也有fortify.zoo
它将把 Zoo 或 xts 对象转换为 data.frame:
fortify(x_xts)
giving:
Index x_xts
1 2000-01-02 1
2 2000-01-03 2
3 2000-01-04 3
4 2000-01-05 4
The fortify
generic 在 ggplot2 中,所以如果你没有加载 ggplot2 那么使用fortify.zoo(x_xts)
直接地。
See ?fortify.zoo
了解更多信息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)