繁体   English   中英

图标作为R中的x轴标签

[英]Icons as x-axis labels in R

我想绘制这样的东西(来自本文) ,其中图标(在本例中为小图)用作刻度标签。

在此输入图像描述

我到目前为止,图标或多或少正确放置: 在此输入图像描述 这是代码:

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

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

欢迎任何建议。

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()

}

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM