2013-10-21 54 views
0

我有以下数据组称为someDat示出用户登记日期:绘图日期对累积字符值

 dates users 
11/06/2013 alfred 
12/06/2013 andrew 
12/06/2013 john 
15/06/2013 jojo 
15/06/2013 jeff 
15/06/2013 samson 
18/06/2013 dave 
18/06/2013 hamsa 
19/06/2013 kambua 

现在,我想绘制日期与用户的累计数,如图在图像中。我试图将users转换成因子,然后使用函数cumsum,但它只是没有给我正确的图表。

usersSum <- cumsum(as.numeric(factor(someDat$users))); usersSum 
plot(someDat$date,someDat$users, type= "b") 

我不知道我要去哪里出错或是否使用正确的功能。任何帮助将不胜感激。

enter image description here

回答

2
someDat <- read.table(text='  dates users 
11/06/2013 alfred 
12/06/2013 andrew 
12/06/2013 john 
15/06/2013 jojo 
15/06/2013 jeff 
15/06/2013 samson 
18/06/2013 dave 
18/06/2013 hamsa 
19/06/2013 kambua',header=TRUE) 
someDat$cumsum <- 1:nrow(someDat) 
someDat$date2 <- as.POSIXct(as.character(someDat$dates),format='%d/%m/%Y') 
# as lines (left plot) 
plot(someDat[!duplicated(someDat$dates, fromLast=TRUE),c('date2','cumsum')],type='l') 
# as steps (right plot, following DWin) 
plot(someDat[!duplicated(someDat$dates, fromLast=TRUE),c('date2','cumsum')],type='l') 

enter image description hereenter image description here