Chronological timeline with points in time and format date

后端 未结 3 1967
爱一瞬间的悲伤
爱一瞬间的悲伤 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 10:01

    Here's another attempt:

    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_len(c(-1,1), length.out=nrow(df))
    txtpts <- rep_len(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="month"),
     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)
    

    enter image description here

提交回复
热议问题