Chronological timeline with points in time and format date

后端 未结 3 1969
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-03 09:31

I am new to R and ggplot2 and I was wondering how can I produce a timeline plotting points at a given time using R? I am also having some trouble with the dates I have. (I’m

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-03 09:53

    With some slight changes to answer of @thelatemail you can finetune the axis to print indicator for event dates and also manage the overlap of events that occur on same date..or manage the overlap arising due to the amount of data you have in your df..

    df$YM <- as.Date(paste0("01",df$YearMonth), format="%d%Y%m")
    rangeYM <- range(df$YM)
    plot(NA,ylim=c(-1,1),xlim=rangeYM,ann=FALSE,axes=FALSE)
    abline(h=0,lwd=2,col="#5B7FA3")
    ypts <- rep(c(-1,-0.5,0.5,1), length.out=nrow(df))
    txtpts <- rep(c(1,3), length.out=nrow(df))
    segments(df$YM,0,df$YM,ypts,col="gray80")
    axis.Date( 1,at=seq.Date(rangeYM[1],rangeYM[2],by="days"),
    format="%Y-%m",
    cex.axis=0.6, pos=0, lwd=0, lwd.tick=2, col="#5B7FA3", font=2)
    points(df$YM,y=ypts, pch="-", cex=1.5, col="#5B7FA3")
    par(xpd=NA)
    text( df$YM, y=ypts,labels=paste(df$Person1,df$Person2,df$Event,sep="\n"),cex=0.7, pos=txtpts)
    par(xpd=FALSE)
    

提交回复
热议问题