使用ggplot2绘制xts对象

Gre*_*tin 4 r ggplot2 xts

我想用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)
Run Code Online (Sandbox Code Playgroud)

现在我尝试使用ggplot2绘制它:

ggplot(new_df, aes(x = index, y = value)) + geom_point()
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

错误(函数(...,row.names = NULL,check.rows = FALSE,check.names = TRUE,:参数意味着不同的行数:0,5

我不太确定我做错了什么.

use*_*479 6

将小写'指数'改为大写'指数'

ggplot(new_df, aes(x = Index, y = value)) + geom_point()
Run Code Online (Sandbox Code Playgroud)


G. *_*eck 6

autoplot.zoo动物园中的方法(动物园由xts自动拉入)也将使用ggplot2为xts对象创建绘图.它支持ggplot2的+ ...如果你需要额外的geoms.看到?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")
Run Code Online (Sandbox Code Playgroud)

zoo还有fortify.zoo将zoo或xts对象转换为data.frame:

fortify(x_xts)
Run Code Online (Sandbox Code Playgroud)

赠送:

       Index x_xts
1 2000-01-02     1
2 2000-01-03     2
3 2000-01-04     3
4 2000-01-05     4
Run Code Online (Sandbox Code Playgroud)

fortify通用在GGPLOT2所以如果你没有GGPLOT2调用,那么使用fortify.zoo(x_xts)直接.

有关?fortify.zoo更多信息,请参阅