使用DiagrammeR绘制igraph网络

时间:2017-06-25 13:44:54

标签: r plot igraph diagrammer

我想创建这个简单的igraph图:

library(igraph)
mydata <- data.table(from=c("John", "John", "Jim"),to=c("John", "Jim", "Jack"))
mygraph <- graph_from_data_frame(d=mydata, directed=T)
plot(mygraph, vertex.label.dist=2)

enter image description here

使用diagrammeR

library(DiagrammeR)
mygraph2 <- from_igraph(mygraph)
grViz(mygraph2)

产生此错误

  

file.exists(图表)出错:无效&#39;文件&#39;参数

我也试过

grViz(readLines(mygraph2)) 

和其他组合或命令图()但我找不到合适的方法。

我该怎么做?

我开了一个新问题,直接用DiagrammeR得到相同的结果,没有igraph:

How to create a network graph with DiagrammeR?

2 个答案:

答案 0 :(得分:3)

似乎还有很多事情要发生。

import UIKit

class PageSliderViewController: UIViewController , UIPageViewControllerDataSource, UIPageViewControllerDelegate {

    @IBOutlet weak var pageControl: UIPageControl!

    var pageContainer: UIPageViewController!

    // The pages it contains
    var pages = [UIViewController]()

    // Track the current index
    var currentIndex: Int?
    private var pendingIndex: Int?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Setup the pages
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let page1: UIViewController! = storyboard.instantiateViewController(withIdentifier: "Page1")
        let page2: UIViewController! = storyboard.instantiateViewController(withIdentifier: "Page2")
        let page3: UIViewController! = storyboard.instantiateViewController(withIdentifier: "Page3")
        let page4: UIViewController! = storyboard.instantiateViewController(withIdentifier: "Page4")
        let page5: UIViewController! = storyboard.instantiateViewController(withIdentifier: "Page5")
        pages.append(page1)
        pages.append(page2)
        pages.append(page3)
        pages.append(page4)
        pages.append(page5)
        // Create the page container
        pageContainer = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
        pageContainer.delegate = self
        pageContainer.dataSource = self
        pageContainer.setViewControllers([page1], direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)

        currentIndex = 0

        // Add it to the view
        view.addSubview(pageContainer.view)

        // Configure our custom pageControl
        self.view.bringSubview(toFront: self.loginButton)
        self.view.bringSubview(toFront: self.signUpButton)
        self.view.bringSubview(toFront: self.pageControl)
        pageControl.numberOfPages = pages.count
        pageControl.currentPage = 0

        Timer.scheduledTimer(timeInterval: 5,
                             target: self,
                             selector: #selector(self.next(_:)),
                             userInfo: nil,
                             repeats: true)

    }

    func next(_ timer: Timer) {
        pageContainer.goToNextPage()
        currentIndex = currentIndex! + 1
        if currentIndex == pageControl.numberOfPages{
            currentIndex = 0
        }
        pageControl.currentPage = currentIndex!
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - UIPageViewController delegates

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        let currentIndex = pages.index(of: viewController)!
        if currentIndex == 0 {
            return nil
        }
        let previousIndex = abs((currentIndex - 1) % pages.count)
        return pages[previousIndex]
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        let currentIndex = pages.index(of: viewController)!
        if currentIndex == pages.count-1 {
            return nil
        }
        let nextIndex = abs((currentIndex + 1) % pages.count)
        return pages[nextIndex]
    }

    func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
        pendingIndex = pages.index(of: pendingViewControllers.first!)
    }

    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        if completed {
            currentIndex = pendingIndex
            if let index = currentIndex {
                pageControl.currentPage = index
            }
        }
    }
}

extension UIPageViewController {
    func goToNextPage(animated: Bool = true, completion: ((Bool) -> Void)? = nil) {
        if let currentViewController = viewControllers?[0] {
            if let nextPage = dataSource?.pageViewController(self, viewControllerAfter: currentViewController) {
                setViewControllers([nextPage], direction: .forward, animated: animated, completion: completion)
            }
        }
    }
}

以下代码会抛出警告

library(igraph)
library(DiagrammeR)

mydata <- data.table(from=c("John", "John", "Jim"),to=c("John", "Jim", "Jack"))
mygraph <- graph_from_data_frame(d=mydata, directed=TRUE)
  

警告讯息:   1:在data.frame中(from = as.integer(igraph :: ends(igraph,igraph :: E(igraph))[,:     强制引入的NA

如果您查看mygraph2 <- from_igraph(mygraph) ,则没有节点或边缘信息,并且它不会呈现:mygraph2。但是警告是有用的,因为它指向代码行(render_graph(mygraph2):也许我们想要names = FALSE),所以尝试删除名称,但设置标签

as.integer(ends(mygraph, E(mygraph), names=TRUE))

无警告和渲染

V(mygraph)$label = V(mygraph)$name
V(mygraph)$name = factor(V(mygraph)$name, levels=as.character(V(mygraph)$name))

如果您想查看mygraph2 <- from_igraph(mygraph) render_graph(mygraph2) 代码,可以使用dot,然后将其传递给generate_dot,但这是grViz正在执行的操作。

render_graph

答案 1 :(得分:1)

您的流程中存在两个问题。

第一个是命令from_igraph。我不确定,也许这是包中的一个错误,也许这是我的设置有问题,但我无法使用它来获得所需的结果。以下工作在我的机器上。

mygraph3 <- from_adj_matrix(as.matrix(get.adjacency(mygraph)), mode = "directed")

然后您需要render_graph或类似的东西来获取图表,grViz用于图表的字符串表示,而不是图表本身。

render_graph(mygraph3)