图标作为R中的x轴标签

时间:2015-04-28 14:54:10

标签: r

我想绘制像这样(from this paper)的图形,其中图标(在本例中为小图)用作刻度标签。

enter image description here

我到目前为止,图标或多或少正确放置: enter image description here 这是代码:

library(igraph)    
npoints <- 15
y <- rexp(npoints)
x <- seq(npoints)

par(fig=c(0.05,1,0.3,1), new=FALSE)
plot(y, xlab=NA, xaxt='n',  pch=15, cex=2, col="red")
lines(y, col='red', lwd=2)

xspan <- 0.9
xoffset <- (0.07+0.5/npoints)*xspan
for(i in 1:npoints){  
  x1 <- (xoffset+(i-1)/npoints)*xspan
  x2 <- min(xspan*(xoffset+(i)/npoints),1)
  par(fig=c(x1,x2,0,0.5), new=TRUE)
  plot(graph.ring(i), vertex.label=NA)  
}

但是,如果点数增加(例如npoints <- 15),它就会抱怨,因为图标没有位置:

Error in plot.new() : figure margins too large

我想知道有更自然的方法来做到这一点,以便它适用于任何(合理的)点数。

欢迎任何建议。

1 个答案:

答案 0 :(得分:5)

library(igraph)    
npoints <- 15
y <- rexp(npoints)
x <- seq(npoints)

# reserve some extra space on bottom margin (outer margin)
par(oma=c(3,0,0,0))
plot(y, xlab=NA, xaxt='n',  pch=15, cex=2, col="red")
lines(y, col='red', lwd=2)

# graph numbers 
x = 1:npoints   

# add offset to first graph for centering
x[1] = x[1] + 0.4
x1 = grconvertX(x=x-0.4, from = 'user', to = 'ndc')
x2 = grconvertX(x=x+0.4, from = 'user', to = 'ndc')

# abline(v=1:npoints, xpd=NA)
for(i in x){  

  print(paste(i, x1[i], x2[i], sep='; '))

  # remove plot margins (mar) around igraphs, so they appear bigger and 
  # `figure margins too large' error is avoided
  par(fig=c(x1[i],x2[i],0,0.2), new=TRUE, mar=c(0,0,0,0))
  plot(graph.ring(i), vertex.label=NA)  

  # uncomment to draw box around plot to verify proper alignment:
  # box()

}

enter image description here