Chronological timeline with points in time and format date

旧巷老猫 提交于 2019-11-27 14:13:20

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)

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"

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!