使用ggplot

时间:2017-08-14 11:51:52

标签: r ggplot2

我有一个熔化的数据表,其中包含'date'/'variable'/'value'列。

据我所知,所有人的行数完全匹配,但是当尝试使用以下方法将它们一起绘制时:

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

我对于为什么会发生这种情况有一些想法。第三列中的一些值是n / a(尽管我现在已经使用!is.na表达式删除了这些行),日期的格式为年 - 月 - 日时间(例如'2017-06-29 00:00:00.0' )。但是我不知道其中任何一个是否是原因,而且我仍然不知道如何继续进行。

任何帮助都将不胜感激。

编辑:

我是如何使用reshape包

从原始数据生成数据表的
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")
  

1 个答案:

答案 0 :(得分: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