【问题标题】:tkplot in latex via knitr and igraph通过 knitr 和 igraph 在乳胶中的 tkplot
【发布时间】:2012-10-10 01:37:23
【问题描述】:

这可能是一个疯狂的奇怪梦想。我梦想我可以通过knitrigraph 中的tkplot 放入乳胶文档中。我知道一辉是知道动画的,所以我想也许这是可能的。谷歌搜索没有显示我所追求的,所以这是一个无效的尝试:

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

【问题讨论】:

  • 应该可以,但是我没有足够的时间写答案;关键是在块挂钩中调用tkplot.export.postscript;看看我是如何定义 hook_rgl 来捕捉 rgl 图的:github.com/yihui/knitr/blob/master/R/hooks-extra.R
  • @Yihui 感谢您的回复。 hook_rgl 是快照还是 pdf 输出中嵌入的动画?
  • 请问您为什么要这样做? tkplot 比简单的 plot 有什么额外的功能?如果这就是你想要的,你可以制作plot() 的背景。 :)
  • @Gabor 可以操纵 tkplot。即它是准动画的。
  • @TylerRinker 我明白了。这确实很酷!看这个:tinyurl.com/7ae3awm底部是PDF,需要Adobe Reader。这不适用于tkplot(),因为在 PDF AFAIK 中不支持 Tcl/Tk。但是,将类似的东西放在一起可能并不难,只需使用 JavaScript 在 3d 中查看和飞越网络。如果你想单独移动顶点/边,那就更难了,你需要在 JavaScript 中重新实现tkplot()。您可以在此处提交功能请求::) bugs.launchpad.net/igraph

标签: r igraph knitr tkplot


【解决方案1】:

好的,一个快速而肮脏的答案:

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

请随意使用hook_plot_custom 完善它。

【讨论】:

  • 我很想在 Knitr 中使用 Diagrammer 图表,但它不起作用,有人说这是因为使用了 htmlwidgets。您知道使用 R+knitr -> latex 或 pdf 更轻松地创建图表的解决方案或任何其他软件包吗?
猜你喜欢
  • 1970-01-01
  • 2020-01-28
  • 1970-01-01
  • 2014-01-18
  • 1970-01-01
  • 1970-01-01
  • 2018-10-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多