具有时间点和格式日期的时间线 [英] Chronological timeline with points in time and format date

查看:32
本文介绍了具有时间点和格式日期的时间线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 R 和 ggplot2 的新手,我想知道如何使用 R 在给定时间生成时间线绘图点?我的日期也有问题.(我不确定我是否应该将其作为两个问题发布,但在这里).

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 not sure if I should post this as two questions, but here goes).

我有一个数据框,其中年和月作为字符,格式为 YYYYMM,两个人的名字和发生的事件.

I have a data frame with the year and month as characters in the format YYYYMM, names of two people and the event that took place.

像这样:

> data

YearMonth   Person1   Person2    Event
200606       Alice      Bob      event1
200606       Bob        Alice    event2
200608       Alice      Bob      event3
200701       Alice      Bob      event3
200703       Bob        Alice    event2
200605       Alice      Bob      event4

日期最初是整数,我使用 as.character() 将其转换为字符.我正在尝试将其转换为格式化的日期.我使用了 as.Date() 并尝试了不同的方式来格式化日期.我最接近的是 data$YearMonth <- as.Date(data$YearMonth,"%Y"),但这让我得到了 '2006-12-20' 和 '2007-12-20' 分别代表所有 2006xx 和 2007xx 行.有什么办法可以让我得到类似YYYY-MM"或YYYY/MM"的信息?

The dates were originally integers, which I converted to characters using as.character(). I am trying to convert it to a formatted date. I used as.Date() and tried different ways to format the date. The closest I came was with data$YearMonth <- as.Date(data$YearMonth,"%Y"), but this got me ‘2006-12-20’ and ‘2007-12-20’ for all the 2006xx and 2007xx rows, respectively. Is there any way to do this so that I get something like ‘YYYY-MM’ or ‘YYYY/MM’?

我也试过 data$YearMonth <- strptime(data$YarMonth, "%Y%m"),但这给了我 值.

I also tried data$YearMonth <- strptime(data$YarMonth, "%Y%m"), but that gave me <NA> values.

但我的主要问题是时间线.

But my main problem is the timeline.

下图是我想要的那种格式:

The following image is the sort of format I want:

但 x 轴显示月份和年份(如 2006-06、2006-07 … 2007-06),而轴外的线标有事件、Person1 和 Person2.

but with the x axis showing the month and year (like 2006-06, 2006-07 … 2007-06), and the lines coming off the axis labelled with the Event, Person1 and Person2.

我查看了 ?timeline 中的timeline"包,但我拥有的数据框没有时间段(开始和结束日期)的数据.我只有一个时间点(年月).

I have looked at the ‘timeline’ package at ?timeline but the data frame I have doesn’t have data for the time periods (start and end dates). I just have a point in time (YearMonth).

我还尝试了 使用 ggplot2 绘制时间线 中的示例ggplot2.但是我没有 y 轴的错位,我希望事件线离开 x 轴.

I also tried the example at Draw a chronological timeline with ggplot2 using ggplot2. However I don’t have the dislocations for a y-axis and I wanted the event lines coming off the x axis.

注意:这是一个非常简化的示例,因为我在 2006 年 6 月至 2007 年 6 月的时间段内有大约一千行.是否有可能用这么多数据来制作时间线?

Note: This is a very simplified example as I have about a thousand rows for the time period June 2006 – June 2007. Is it even possible to make the timeline with this much data?

非常感谢任何帮助.感谢您的时间!

Any help is much appreciated. Thanks for your time!

推荐答案

这是另一个尝试:

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="
"), cex=0.7, pos=txtpts
)
par(xpd=FALSE)

这篇关于具有时间点和格式日期的时间线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆