简体   繁体   中英

Icons as x-axis labels in R

I would like to plot something like this (from this paper) where icons, in this case small graphs, are used as tick labels.

在此输入图像描述

I get this far, where icons are more or less properly placed: 在此输入图像描述 This is the code:

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

However, if the number of points grows (eg npoints <- 15 ) it complains because there is no place for the icons:

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

I wonder wether there is a more natural way to do this so that it works for any (reasonable) number of points.

Any advice is welcome.

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

}

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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