我想使用R中的时间轴包创建一个漂亮的时间线图,但我不知道如何解决我遇到的这些问题。 一个是这些酒吧向上堆叠,但我希望它们处于同一水平。 应忽略y轴。 x轴应使用StartLabel列中的标签。 我怎么去那里?
以下是代码和数据:
require(data.table)
require(timeline)
setsTimeline <- data.table(Set=c("x","y","z","x","y","z","x","y","z","x","y","z","x"),
StartDate=c(1380708900,1402963200,1420070400,1421280000,1410912000,1396310400,1397520000,1418860800,1404172800,1405382400,1395100800,1412121600,1413331200),
EndDate= c(1395099900,1404171900,1421279100,1430985600,1412120700,1397519100,1402962300,1420069500,1405381500,1410911100,1396309500,1413330300,1418859900))
setsTimeline[,StartLabel:=as.POSIXct(StartDate,tz="UTC",origin="1970-01-01")]
setsTimeline[,Group:=c("Timeline")]
setkey(setsTimeline,StartDate)
timeline(setsTimeline,label.col="Set",start.col="StartDate",end.col="EndDate",group.col="Group")
我希望看到类似的内容:http://timelyportfolio.github.io/rCharts_time_series/assets/fig/unnamed-chunk-3.png
但现在它看起来像这样:
编辑: 也许这是一个失败的原因,因为这个包还没有完成。 我确实设法将图形取消堆叠。似乎必须定义一个组变量。 但我仍然无法弄清楚如何更改标签和添加图例。看来这个情节对通常的情节函数没有反应。 我编辑了上面的代码,这是当前的图表:
答案 0 :(得分:1)
显然,我想做的事情甚至可以只用ggplot2,所以我想知道为什么有人甚至打扰为它创建一个新包。 我花了几个小时试图找到正确的选项和东西,但这里是:
require(data.table)
require(ggplot2)
require(grid)
setsTimeline <- data.table(Set=c("x","y","z","x","y","z","x","y","z","x","y","z","x"),
StartDate=c(1380708900,1402963200,1420070400,1421280000,1410912000,1396310400,1397520000,1418860800,1404172800,1405382400,1395100800,1412121600,1413331200),
EndDate= c(1395099900,1404171900,1421279100,1430985600,1412120700,1397519100,1402962300,1420069500,1405381500,1410911100,1396309500,1413330300,1418859900))
setsTimeline[,StartLabel:=as.POSIXct(StartDate,tz="UTC",origin="1970-01-01")]
breaks <- c(1380708900,1395100800,1402963200,1410912000,1418860800,1430985600)
labels <- as.POSIXct(breaks,tz="UTC",origin="1970-01-01")
ggplot(setsTimeline, aes(colour=Set)) +
geom_segment(aes(x=StartDate, xend=EndDate, y="group", yend="group"), size=10) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
aspect.ratio=5e-02,
axis.text.x = element_text(colour='black', angle = 45, size = 16, hjust = 1, vjust = 1),
legend.text = element_text(colour='black', size = 16),
legend.title = element_text(colour='black', size = 0),
legend.position = 'top',
plot.title = element_text(colour='black', size = 18),
panel.margin = unit(1, "cm")) +
xlab(NULL) +
ylab(NULL) +
ggtitle("Data timeline with marked sets") +
coord_cartesian(xlim = c(1380708900,1430985600), ylim = NULL) +
scale_x_continuous(breaks=breaks,labels=labels)
导致:
如果某人知道如何摆脱y轴以及如何让第一个x轴标签保持在绘图区域内而不必移动/转动它,请帮助我。我尝试了plot.margin和panel.margin,但是其中一个没有做任何事情而另一个一直没有错误。
现在已经过了我的睡觉时间,明天我会做一个演示......
答案 1 :(得分:0)
有一个新的包完成这项工作:vistime
。 devtools的版本执行以下操作:
install.package("devtools")
devtools::install_github("shosaco/vistime")
library(vistime)
setsTimeline <- data.table(Set=c("x","y","z","x","y","z","x","y","z","x","y","z","x"),
StartDate=c(1380708900,1402963200,1420070400,1421280000,1410912000,1396310400,1397520000,1418860800,1404172800,1405382400,1395100800,1412121600,1413331200),
EndDate= c(1395099900,1404171900,1421279100,1430985600,1412120700,1397519100,1402962300,1420069500,1405381500,1410911100,1396309500,1413330300,1418859900))
setsTimeline[,StartLabel:=as.POSIXct(StartDate,tz="UTC",origin="1970-01-01")]
setsTimeline[,EndLabel:=as.POSIXct(EndDate,tz="UTC",origin="1970-01-01")]
vistime(setsTimeline, events="Set", start="StartLabel", end="EndLabel")