将R中的时间线绘制为单个带标签的条形图,并在x轴标签处显示日期 [英] Plot timeline in R as single labeled bar and with dates at the x-axis labels
问题描述
我想用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)
时间轴(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
现在看起来像这样:
编辑:
所以也许这是一个失败的原因,因为这个软件包还没有完成。
我确实设法解除了图形。看来必须定义一个组变量。
但仍然无法弄清楚如何更改标签并添加图例。看起来这个情节对平常的情节功能没有反应。
我编辑了上面的代码,这里是当前图:
显然,我想要做的事情甚至可能只有ggplot2,所以我想知道为什么有人甚至打扰创建一个新的包。
我花了几个小时试图找到正确的选项和东西,但是在这里:
require(data。 table)
require(ggplot2)
require(grid)
setsTimeline< - data.table(Set = c(x,y,z, xyzxyzxyzx)
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)]
中断<-c(1380708900,1395100800,1402963200,1410912000,1418860800,1430985600)
标签< - as.POSIXct(breaks,tz =UTC,origin =1970-01 -01)
ggplot(setsTimeline,aes(color = Set))+
geom_segment(aes(x = StartDate,xend = EndDate,y =group,yend =group ),size = 10)+
主题(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
aspect.ratio = 5e -02,
axis.text.x = element_text(color ='black',angle = 45,size = 16,hjust = 1,vjust = 1),
legend.text = element_text(color = 'black',size = 16),
legend.title = element_text(color ='black',size = 0),
legend.position ='top',
plot.title = element_text(color ='black',size = 18),
panel.margin = unit(1,cm))+
xlab(NULL)+
ylab(NULL)+
ggtitle(带标记集的数据时间轴)+
coord_cartesian(xlim = c(1380708900,1430985600),ylim = NULL)+
scale_x_continuous(breaks = breaks,labels = labels)
结果为:
现在我的睡眠时间已经过去了,我有一个演示文稿给出明天......
I want to create a nice timeline plot using the timeline package in R, but I am not sure how to fix these issues I am having with it. One is that these bars stack upwards, but I'd like them to be on the same level. The y-axis should be left out. The x-axis should use the labels from the StartLabel column. How do I get it there?
Here is the code and data:
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")
Something like this is what I'd like to see: http://timelyportfolio.github.io/rCharts_time_series/assets/fig/unnamed-chunk-3.png
But now it looks like this:
Edit: So maybe this is a lost cause, because this package is just not finished yet. I did manage to get the graph unstacked. It appears one has to define a group variable. But still I am unable to figure out how to change the labels and add a legend. It appears this plot does not respond to the usual plot functions. I edited the code above and here is the current graph:
So apparently the thing I wanted to do is even possible with just ggplot2, so I am wondering why someone is even bothering creating a new package for it. I spent a few more hours trying to find the right options and stuff but here goes:
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)
results in:
if someone comes by who knows how to get rid of the y-axis and how to get the first x-axis label to remain within the plot area without having to move/turn it, please help me out. I tried plot.margin and panel.margin, but one did nothing and the other kept erroring out.
it is now way past my bedtime and I have a presentation to give tomorrow...
这篇关于将R中的时间线绘制为单个带标签的条形图,并在x轴标签处显示日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!