我仍然是R的初学者,但是设法通过这里的另一篇文章策划时间表:Chronological timeline with points in time and format date
我已经将我的类别的事件编码在不同的类别中,并希望将它们安排在不同的级别上,而不是在同一行,就像现在的情况一样,使时间轴更容易阅读。为简单起见,我在这个问题上只选择了一个月而不是一年。
数据示例(我现在对“编码”部分不感兴趣):
第1行:“日期”“分类”“编码11 - 失败,22通过”“分类2”
第2行:“01.02.07”“21”“NA”“1”
等
categn
中有一些元素,其中一些是子类别categ2n
的元素。我有三个问题(最后一个不太重要):
categn
的元素放在时间轴中的不同级别?categn
与categ2n
的元素合并,并在时间轴中绘制这些元素? (再次在不同的层面 - 如果你对我有一些建议,我会非常感激。)这是dput(mydata):
structure(list(Dates = structure(c(13515, 13524, 13529, 13529, 13530, 13534, 13534, 13536, 13543, 13543, 13544, 13544, 13544, 13545, 13548, 13550, 13551, 13552, 13553, 13553, 13553, 13557, 13557, 13558, 13559, 13562, 13566, 13566, 13569, 13572, 13572, 13573, 13577, 13578, 13578, 13578, 13580, 13580, 13581, 13581, 13583, 13584, 13586, 13587, 13587, 13592, 13594, 13595, 13596, 13599, 13600, 13517, 13606, 13612, 13614, 13616, 13619, 13621, 13621, 13622, 13626, 13627, 13627, 13628, 13630, 13632, 13633, 13634, 13637, 13642, 13642, 13648, 13649, 13650, 13651, 13653, 13654, 13662, 13664, 13669, 13675, 13678, 13686, 13691, 13696, 13703, 13704, 13705, 13706, 13706, 13712, 13712, 13712, 13713, 13714, 13716, 13717, 13719, 13721, 13725, 13725, 13725, 13725, 13725, 13725, 13728, 13728, 13728, 13739, 13742, 13746, 13746, 13753, 13757, 13757, 13757, 13769, 13770, 13781, 13781, 13781, 13781, 13782, 13784, 13784, 13791, 13796, 13797, 13798, 13802, 13803, 13809, 13809, 13810, 13811, 13811, 13811, 13812, 13816, 13822, 13824, 13824, 13824, 13826, 13831, 13832, 13837, 13840, 13848, 13848, 13850, 13852, 13853, 13853, 13854, 13856, 13858, 13859, 13859, 13861, 13865, 13866, 13866, 13875, 13875), class = "Date"),
Event = structure(c(2L, 23L, 39L, 28L, 23L, 28L, 28L, 44L,
23L, 21L, 11L, 39L, 28L, 28L, 28L, 21L, 23L, 39L, 2L, 2L,
36L, 23L, 19L, 23L, 23L, 9L, 19L, 21L, 21L, 33L, 31L, 19L,
28L, 39L, 47L, 32L, 39L, 32L, 39L, 28L, 2L, 31L, 19L, 15L,
12L, 39L, 43L, 10L, 46L, 23L, 2L, 9L, 19L, 27L, 10L, 2L,
19L, 23L, 2L, 38L, 9L, 20L, 39L, 28L, 28L, 28L, 19L, 45L,
36L, 47L, 1L, 19L, 39L, 41L, 18L, 28L, 28L, 28L, 37L, 16L,
41L, 23L, 36L, 39L, 9L, 30L, 10L, 23L, 23L, 6L, 23L, 23L,
23L, 39L, 23L, 38L, 24L, 23L, 23L, 41L, 41L, 3L, 23L, 10L,
32L, 39L, 47L, 32L, 5L, 34L, 26L, 20L, 28L, 22L, 7L, 4L,
20L, 13L, 41L, 3L, 19L, 20L, 28L, 28L, 10L, 17L, 23L, 19L,
6L, 8L, 6L, 23L, 12L, 8L, 47L, 9L, 19L, 6L, 23L, 6L, 23L,
12L, 14L, 6L, 23L, 8L, 6L, 8L, 22L, 35L, 25L, 8L, 6L, 47L,
8L, 9L, 31L, 40L, 8L, 10L, 6L, 9L, 29L, 42L, 30L),
Categ = c(21L,
5L, 43L, 111L, 5L, 111L, 13L, 1L, 5L, 23L, 5L, 43L, 13L,
13L, 131L, 23L, 5L, 3L, 21L, 21L, 81L, 5L, 23L, 5L, 5L, 81L,
23L, 23L, 23L, 23L, 23L, 23L, 13L, 41L, 22L, 22L, 3L, 22L,
3L, 11L, 21L, 23L, 23L, 5L, 5L, 3L, NA, 83L, 100L, 5L, 21L,
81L, 23L, 101L, 83L, 21L, 23L, 5L, 21L, 81L, 81L, 23L, 43L,
13L, 13L, 13L, 23L, 83L, 81L, 23L, 81L, 23L, 3L, 31L, 23L,
131L, 13L, 131L, 23L, 23L, 31L, 5L, 81L, 41L, 81L, 23L, 83L,
5L, 5L, 21L, 5L, 5L, 5L, 43L, 5L, 81L, 71L, 5L, 5L, 31L,
31L, 21L, 5L, 83L, 21L, 41L, 22L, 22L, 21L, 81L, 100L, 23L,
111L, 131L, 83L, 21L, 23L, 5L, 31L, 21L, 23L, 23L, NA, NA,
83L, 23L, 5L, 23L, 21L, 22L, 21L, 5L, 5L, 22L, 22L, 81L,
23L, 21L, 5L, 21L, 5L, 5L, 5L, 21L, 5L, 22L, 21L, 22L, 131L,
81L, 71L, 22L, 21L, 22L, 22L, 21L, 23L, 32L, 22L, 83L, 21L,
81L, 83L, 35L, 23L), Coding.11...failed..22.passed = c(NA,
NA, 11L, NA, NA, NA, NA, NA, NA, NA, NA, 11L, NA, NA, NA,
NA, NA, 11L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 11L, NA, NA, 11L, NA, 11L, NA, NA, NA, NA,
NA, NA, 11L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, 11L, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 11L, 11L, NA, NA, NA, NA, NA, NA, 11L, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, 11L, NA, NA, NA, NA, NA,
11L, 11L, NA, NA, NA, NA, 11L, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, 11L, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
11L, NA, NA, NA, NA, NA, 11L, NA), Categ2 = c(1L, NA, 3L,
1L, NA, 1L, 3L, 1L, NA, 3L, 3L, 3L, 3L, 3L, 3L, 3L, NA, 1L,
1L, 1L, 1L, NA, 3L, NA, NA, 1L, 3L, 3L, 3L, 2L, 3L, 3L, 3L,
1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 3L, 3L, NA, NA, 1L, NA, 3L,
1L, NA, 1L, 1L, 3L, 1L, 3L, 1L, 3L, NA, 1L, 1L, 1L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 1L, 2L, 1L, 3L, 1L, 1L, 3L, 3L, 3L, 3L,
2L, 3L, 1L, NA, 1L, 1L, 1L, 3L, 3L, NA, NA, 1L, NA, NA, NA,
3L, NA, 1L, 1L, NA, NA, 1L, 1L, 1L, NA, 3L, 1L, 1L, 2L, 2L,
1L, 1L, 3L, 3L, 1L, 3L, 3L, 1L, 3L, NA, 1L, 1L, 3L, 3L, NA,
NA, 3L, 3L, NA, 3L, 1L, 2L, 1L, NA, NA, 2L, 2L, 1L, 3L, 1L,
NA, 1L, NA, NA, NA, 1L, NA, 2L, 1L, 2L, 3L, 1L, 3L, 2L, 1L,
2L, 2L, 1L, 3L, 1L, 2L, 3L, 1L, 1L, 3L, 1L, 3L)), .Names = c("Dates", "Event", "Categ", "Coding.11...failed..22.passed", "Categ2"), row.names = c(NA,-165L), class = "data.frame")
这是我的代码:
mydata = read.csv("/Users/Timeline.csv",sep=";")
mydata[[1]] <- as.Date(mydata[[1]], "%m.%d.%y")
timespan <- c("01.01.07","01.31.07")
timespan <- as.Date(timespan, "%m.%d.%y")
dateselection <- mydata[((mydata$Dates>=timespan[[1]]) &
(mydata$Dates<=timespan[[2]])), ]
categn<-c(0, 1, 11, 111, 12, 121, 13, 131, 2, 21, 22, 23, 3, 31, 32, 33, 34,
41, 42, 43, 5, 51, 52, 53, 6, 70, 71, 72, 73, 81, 82, 83, 91, 92,
93, 100, 101)
categ2n<-c(1, 2, 3)
categntest<-c(21)
categn2test<-c(1)
categselection2 <- dateselection[dateselection$Categ %in% categntest,]
categselection22 <- dateselection[(dateselection$Categ %in% categntest) &
(dateselection$Categ2 %in% categn2test),]
rangeDates<- range(dateselection$Dates)
plot(NA,ylim=c(-1,1),xlim=rangeDates,ann=FALSE,axes=FALSE)
abline(h=0,lwd=2,col="#5B7FA3")
ypts <- rep_len(c(-1,1), length.out=nrow(dateselection))
txtpts <- rep_len(c(1,3), length.out=nrow(dateselection))
segments(dateselection$Dates,0,dateselection$Dates,ypts,col="gray80")
axis.Date(
1,
at=seq.Date(rangeDates[1],rangeDates[2],by="day"),
format="%m-%d",
cex.axis=0.6,
pos=0,
lwd=0,
lwd.tick=2,
col="#5B7FA3",
font=2
)
points(dateselection$Dates,y=ypts, pch="-", cex=1.5, col="#5B7FA3")
par(xpd=NA)
text(
dateselection$Dates, y=ypts,
labels=paste(dateselection$Categ,sep="\n"), cex=0.7, pos=txtpts
)
par(xpd=FALSE)
]