2013-10-21 91 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