2017-08-14 158 views
0

我有一個已熔化的數據表,其中包含'date'/'variable'/'value'列。使用ggplot在r中繪製多變量線圖的問題

至於我可以告訴大家,行所有的數量匹配完美,但是想要繪製在一起使用時:

ggplot(data = subset(data_long, !is.na(value)), 
     aes(x=date, y=value, group = variable)) + 
    scale_x_date(labels = "%Y-%m-%d") + 
    geom_line() 

這將返回:

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

我有一個關於這可能發生的原因很少。第三列中的一些值是不適用的(儘管我現在已經使用!is.na表達式刪除了這些行),並且日期格式爲年 - 月 - 日時間(例如'2017-06-29 00:00:00.0' )。然而,我不知道這些是否是原因,並且對於如何進行的想法我堅持不懈。

任何幫助,將不勝感激。

編輯:

我如何使用重塑包

pivot = cast(table, workingdate ~ trader, value = variable1) 
cumulative = cumsum(pivot[,-1]) #taking the cumulative sum of all columns except the date 
data = data.frame(pivot$workingdate,cumulative) 

data_long <- melt(data, id="pivot.workingdate") # convert to long format 
data_long$pivot.workingdate = as.Date(data_long$pivot.workingdate, "%Y-%m-%d") 

而且從dput(head(data_long))(變量名稱進行替換)輸出生成我的數據表從原始數據:

structure(list(pivot.workingdate = structure(c(17171, 17172, 
17175, 17176, 17177, 17178), class = "Date"), variable = structure(c(1L, 
1L, 1L, 1L, 1L, 1L), .Label = c("A", "B", 
"C", "D", "E", "F", 
"G", "H", "I", "J", 
"K", "L", "M", "N", "O", 
"P", "Q", "R", "S", 
"T", "U", "V", "W"), class = "factor"), 
    value = c(-0.324163711670048, 0.133077732043205, 0.520368058673496, 
    0.513543560907851, 0.36852295463088, 0.515249684437591)), .Names = c("pivot.workingdate", 
"variable", "value"), row.names = c(NA, 6L), class = "data.frame") 

回答

0
ggplot(data = subset(data_long, !is.na(value)), 
     aes(x=pivot.workingdate, y=value, group = variable)) + 
    scale_x_date(date_labels = "%Y-%m-%d") + 
    geom_line() 

enter image description here

+0

我不認爲這改變任何東西......仍然給出確切的同樣的錯誤。 – alex1stef2

+0

您能否提供數據的一個子集,以便我們重現錯誤? PS但是需要包含附加的'+'。 – Lyngbakr

+0

從RStudio表查看器的簡單複製粘貼是否足夠? – alex1stef2