轴上的R igraph格式日期

时间:2018-03-29 07:17:24

标签: r date matrix plot igraph

在下面的igraph中,有些日期要在x轴上标记为标记。下面我提供了一个例子。由于日期在标签矩阵中指定,因此它们被格式化为原子值。如何让x轴上的日期以常规日期格式显示?

library(igraph)

nodes=data.frame(
  c(0,1,2,3),
  c("A","B","C","D")
)

colnames(nodes) = c("id","name")

links = data.frame(
  c(0,0,1,2),
  c(1,2,3,3)
)

colnames(links) = c("from","to")

layout = matrix(
  c(as.Date('2010-01-01'),1, as.Date('2010-01-02'),1, as.Date('2010-01-02'),2, as.Date('2010-01-06'),1), byrow = TRUE, nrow=4
)

net = graph.data.frame(links, vertices = nodes)

plot.igraph(
  net, xaxt="n",layout=layout,axes=TRUE,asp=0, rescale=FALSE,xlim=c(as.Date('2010-01-01'),as.Date('2010-01-06')),ylim=c(1,2)
)

1 个答案:

答案 0 :(得分:0)

您可以按照here说明用您自己的值替换轴。 使用您的代码,它提供:

layout <- data.frame(Date = as.Date(c('2010-01-01','2010-01-02','2010-01-02','2010-01-06')), value = c(1,2,1,1))

plot.igraph(
  net, 
  layout = layout, 
  rescale = FALSE,
  axis = FALSE, 
  asp = 0,
  xlim = as.Date(c('2010-01-01', '2010-01-06')),
  ylim = c(1,2)
)
axis(1, at = as.numeric(layout$Date), labels = layout$Date, cex.axis = 0.9)
axis(2, at = 1:max(layout$value), labels = 1:max(layout$value))

enter image description here