按时间顺序排列的时间点和格式日期 [英] Chronological timeline with points in time and format date

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

问题描述

我是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"),但是对于所有2006xx和2007xx行,分别获得了"2006-12-20"和"2007-12-20".有什么方法可以使我得到"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"),但这给了我<NA>值.

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),并且从该轴出来的行分别标记为Event,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处的时间轴"数据包,但我的数据框没有该时间段(开始和结束日期)的数据.我只有一个时间点(YearMonth).

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月的时间段内大约有1000行.甚至有可能用这么多的数据制作时间表吗?

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

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

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