1

In the following igraph there are dates to be plotted as marks on the x-axis. Below I provided an example. As the dates are specified in the label matrix they are formatted into an atomic value. How do I get the dates on the x-axis to be displayed in a regular date format?

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)
)
Luca_brasi
  • 89
  • 7

1 Answers1

1

You can replace the axis by your own values as explained here. Using your code, it gives:

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

DJack
  • 4,850
  • 3
  • 21
  • 45