2017-08-14 154 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