Chronological timeline with points in time and format date

后端 未结 3 1910
爱一瞬间的悲伤
爱一瞬间的悲伤 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)
    
    0 讨论(0)
  • 2020-12-03 10:00

    Why not this:

    
    >YearMonth = c(200506,200509) 
    
    >dt = as.POSIXct(strptime(paste0(YearMonth, 15), "%Y%m%d"))
    >z = rep(0, length(dt))
    >y = rep(c(-1,1), out=length(dt))
    >plot(dt,y, axes=FALSE, ylab="", xlim=c(min(dt)-10e6, max(dt)+10e6), ylim=c(-2,2), pch=15, col="darkblue", xlab="Date")
    >arrows(x0=dt,y0= z, x1=dt, y1=y, length=0, angle=30, col="blue")
    >arrows(min(dt), 0, max(dt), length=0, col="blue")
    >text(dt, y*1.5, c("Ben Franklin arose\nfrom the dead", "Atlantis found"), adj=1)
    >axis.POSIXct(1, dt, format="%y/%m")
    >dt
    [1] "2005-06-15 EDT" "2005-09-15 EDT"
    

    enter image description here

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题