代码之家  ›  专栏  ›  技术社区  ›  Luca_brasi

轴上的R igraph格式日期

  •  1
  • Luca_brasi  · 技术社区  · 7 年前

    在下面的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 回复  |  直到 7 年前
        1
  •  1
  •   DJack    6 年前

    您可以按说明用自己的值替换轴 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