我希望能够在R中创建包含以下数据的时间轴:
Label Date
A 7/7/2015 18:17
B 6/24/2015 10:42
C 6/23/2015 18:05
D 6/19/2015 17:35
E 6/16/2015 15:03
像这样:--- A --- B ----------- C-D ------ E
时间轴只是水平线上的时间顺序日期,它们之间的时间差异。到目前为止,我无法使用ggplot2
或时间轴包,因为我没有数值。
请提前帮助并表示感谢!
修改
我试过的代码
Label <- c("A", "B", "C", "D", "E")
Date <- c("7/7/2015 18:17", "6/24/2015 10:42", "6/23/2015 18:05", "6/19/2015 17:35", "6/16/2015 15:03")
dat <-data.frame(cbind(Label, Date))
dat$Date <- as.POSIXct(dat$Date, format="%m/%d/%Y %H:%M")
dat$y <- 0
library(ggplot2)
ggplot(dat, aes(Date, y))+ geom_point()
答案 0 :(得分:2)
library(ggplot2)
library(scales) # for date formats
dat <- read.table(header=T, stringsAsFactors=F, text=
"Label Date
A '7/7/2015 18:17'
B '6/24/2015 10:42'
C '6/23/2015 18:05'
D '6/19/2015 17:35'
E '6/16/2015 15:03'")
# date-time variable
dat$Date <- as.POSIXct(dat$Date, format="%m/%d/%Y %H:%M")
# Plot: add label - could just use geom_point if you dont want the labels
# Remove the geom_hline if you do not want the horizontal line
ggplot(dat, aes(x=Date, y=0, label=Label)) +
geom_text(size=5, colour="red") +
geom_hline(y=0, alpha=0.5, linetype="dashed") +
scale_x_datetime(breaks = date_breaks("2 days"), labels=date_format("%d-%b"))
编辑从标签添加行到x轴
ggplot(dat, aes(x=Date, xend=Date, y=1, yend=0, label=Label)) +
geom_segment()+
geom_text(size=5, colour="red", vjust=0) +
ylim(c(0,2)) +
scale_x_datetime(breaks = date_breaks("2 days"), labels=date_format("%d-%b")) +
theme(axis.text.y = element_blank(),
axis.ticks.y=element_blank())