9

This may be a wild strange dream. I dreampt that I could put a tkplot from igraph inside a latex document via knitr. I know Yihui is know for animation stuff so I thought maybe this is possible. A google search didn't show what I was after so here's a non working attempt:

\documentclass[a4paper]{scrartcl}
\begin{document}

<<setup, include=FALSE, cache=FALSE>>=
library(igraph)
@

<<network>>=
edges <- structure(c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", 
    "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "A", "B", "C", 
    "D", "E", "F", "G", "H", "I", "J", "E", "G", "G", "F", "H", "G", 
    "D", "J", "J", "D", "B", "C", "D", "I", "I", "H", "A", "B", "G", 
    "I", "F", "D", "F", "J", "D", "B", "E", "E", "A", "E"), .Dim = c(30L, 
    2L), .Dimnames = list(NULL, c("person", "choice")))

g <- graph.data.frame(edges, directed=TRUE)
tkplot(g)
@ 

\end{document}
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • 2
    it should be possible, but I do not have enough time to write the answer; the key is to call `tkplot.export.postscript` in a chunk hook; see how I defined `hook_rgl` to capture an rgl plot: https://github.com/yihui/knitr/blob/master/R/hooks-extra.R – Yihui Xie Oct 10 '12 at 05:05
  • @Yihui Thank you for your response. Is the hook_rgl a snap shot or is the animation embedded in the pdf output? – Tyler Rinker Oct 10 '12 at 13:18
  • May I ask you why you would want to do this? What extra does `tkplot` have over simple `plot`? You can make the background of `plot()` if that's what you are going for. :) – Gabor Csardi Oct 10 '12 at 14:27
  • @Gabor the tkplot can be manipulated. ie it's quasi animated. – Tyler Rinker Oct 10 '12 at 15:20
  • Hmmm, I still don't get it, sorry. OK, it can be manipulated. But you want to put it in a PDF, right? You would have an animated plot in the PDF? Really? Or you would simply have multiple snapshots of the network in the PDF? Because the latter can be done with `plot()` as well. – Gabor Csardi Oct 10 '12 at 15:23
  • @Gabor sorry I wasn't clear in my thoughts. What you're getting at is my question; can I put the manipulable plot from your package into a pdf? I want the reader of a paper to be able to interact with the plot like I can when I use `tkplot`, as sometimes a network graph can get messy. The static plot may obscure stuff that the manipulable plot allows you to see. I'm guessing this is crazy talk but the idea seems pretty nice, particularly as journals start to be online it would be nice if the reader could interact. – Tyler Rinker Oct 10 '12 at 15:41
  • 1
    @TylerRinker I see. This would be cool indeed! Look at this: http://tinyurl.com/7ae3awm The PDF is at the bottom, and you need Adobe Reader. This will not work with `tkplot()` because there is no support for Tcl/Tk in PDFs AFAIK. But it would be probably not too hard to put together something similar, just to view and fly over the network in 3d, using JavaScript. If you want to move the vertices/edges individually, that is much harder, you would need to reimplement `tkplot()` in JavaScript. You can submit a feature request here: :) https://bugs.launchpad.net/igraph – Gabor Csardi Oct 10 '12 at 15:57

1 Answers1

3

OK, a quick and dirty answer:

\documentclass{article}
\begin{document}

<<setup, include=FALSE, cache=FALSE>>=
library(igraph)
library(tcltk)
knit_hooks$set(igraph = function(before, options, envir) {
  if (before) return()
  path = knitr:::fig_path('.eps')
  tkpostscript(igraph:::.tkplot.get(options$igraph)$canvas,
                     file = path)
  sprintf('\\includegraphics{%s}', path)
})
@

<<network, igraph=1>>=
edges <- structure(c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", 
    "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "A", "B", "C", 
    "D", "E", "F", "G", "H", "I", "J", "E", "G", "G", "F", "H", "G", 
    "D", "J", "J", "D", "B", "C", "D", "I", "I", "H", "A", "B", "G", 
    "I", "F", "D", "F", "J", "D", "B", "E", "E", "A", "E"), .Dim = c(30L, 
    2L), .Dimnames = list(NULL, c("person", "choice")))

g <- graph.data.frame(edges, directed=TRUE)
tkplot(g)
@ 

\end{document}

Feel free to polish it with hook_plot_custom.

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • I would kike to use Diagrammer diagrams with Knitr but it doesn't work, some people says it's because of the use of htmlwidgets. Do you know the solution or any other package to create diagrams easier with R+knitr -> latex or pdf? – skan Oct 29 '15 at 17:18