绘制日期与累积字符值的关系图

时间:2013-10-21 07:42:36

标签: r

我有以下数据集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

1 个答案:

答案 0 :(得分: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 here enter image description here