通过knitr和igraph在乳胶中的tkplot [英] tkplot in latex via knitr and igraph

查看:89
本文介绍了通过knitr和igraph在乳胶中的tkplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个疯狂的奇怪梦想.我梦想着可以通过knitrigraph中的tkplot放入乳胶文档中.我知道艺熙以动画方面的知识而闻名,所以我认为这可能是可能的. Google搜索未显示我的搜索结果,因此这是无效的尝试:

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}

推荐答案

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

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}

随时用hook_plot_custom对其进行抛光.

这篇关于通过knitr和igraph在乳胶中的tkplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆