2017-04-11 5 views
1

ggplot2를 사용하여 xts 객체를 플롯하려하지만 오류가 발생합니다. 나는 다음과 같은 오류 얻을ggplot2를 사용하여 xts 객체 플로팅

ggplot(new_df, aes(x = index, y = value)) + geom_point() 

:

Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows: 0, 5

을 내가 아주 확실하지 않다

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 그것을 사용하여 플롯하려고 : 여기에 내가 뭘하는지입니다 내가 잘못하고있는 게 뭐지? 대문자로

답변

4

변화 소문자 '인덱스' '색인'

ggplot(new_df, aes(x = Index, y = value)) + geom_point() 
0

당신이 xts 객체를 사용해야합니까?

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) 

require(scales) 
ggplot(new_df, aes(x = dates, y = value)) + geom_point() + 
scale_x_date(labels = date_format("%Y-%m-%d"), breaks = date_breaks("1 month")) + 
    theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) 
ggsave("time_plot.png", height = 4, width = 4) 

enter image description here

2

동물원에있어서 autoplot.zoo (동물원 자동 XTS 의해 당겨) XTS위한 ggplot2을 사용하여 그래프를 작성 너무 개체. 추가 기하 구조가 필요한 경우 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이 있거나 XTS는 data.frame에 반대 :

fortify(x_xts) 

주는 :

 Index x_xts 
1 2000-01-02  1 
2 2000-01-03  2 
3 2000-01-04  3 
4 2000-01-05  4 

fortify 제네릭이 ggplot2에 너무 ggplot2가로드되어 있지 않으면 fortify.zoo(x_xts)을 직접 사용하십시오.

자세한 내용은 ?fortify.zoo을 참조하십시오.