1. 首页
  2. 自学中心
  3. 软件
  4. R

R可视化可交互网络图

使用R语言中的visNetwork包和networkD3包可视化可交互的网络图。

原始程序输出的文档地址为:

RPubs – R可视化可交互网络图​rpubs.com

visNetwork可视化可交互的网络图

library(visNetwork)
library(igraph)
library(igraphdata)
library(stringr)
library(rpart)
library(sparkline)

data("karate")

karatedf <- as_data_frame(karate,what = "both")
nodedf <- karatedf$vertices
edagedf <- karatedf$edges

head(nodedf)
##         Faction    name label color
## Mr Hi         1   Mr Hi     H     1
## Actor 2       1 Actor 2     2     1
## Actor 3       1 Actor 3     3     1
## Actor 4       1 Actor 4     4     1
## Actor 5       1 Actor 5     5     1
## Actor 6       1 Actor 6     6     1

head(edagedf)
##    from      to weight
## 1 Mr Hi Actor 2      4
## 2 Mr Hi Actor 3      5
## 3 Mr Hi Actor 4      3
## 4 Mr Hi Actor 5      3
## 5 Mr Hi Actor 6      3
## 6 Mr Hi Actor 7      3

## 节点的形状
shape = c("square", "triangle", "box", "circle", "dot", "star",
          "ellipse", "database", "text", "diamond")
# 节点的颜色
color = c("orange", "darkblue", "purple","darkred", "grey")
# 节点的大小
nodesize <- degree(karate)

## 设置网络的节点数据
Newnodes <- data.frame(id=nodedf$name, # 节点的id
                       label = nodedf$label, # 节点的标签
                       group = paste("Group",nodedf$Faction), # 节点的分组
                       title = nodedf$name,# tooltip of the node
                       shape = shape[nodedf$Faction], # 节点的形状
                       color = color[nodedf$color], # 节点的颜色
                       size = 10+ nodesize /2   # 节点的大小
                       )

## 设置网络的边数据
Newedages <- data.frame(from = edagedf$from,#边的起点
                        to = edagedf$to, # 边的终点
                        # 边的标签
                        label = paste("weight",edagedf$weight,sep = "-"),
                        width = edagedf$weight, # 边的宽度,
                        color = color[edagedf$weight]
                        )



set.seed(123)
## 使用网络的节点数据进行网络图的可视化
visNetwork(Newnodes, Newedages, height = "600px", width = "100%",
           main = "网络图",background = "lightblue") %>%
  # 添加图例
  visGroups(groupname = "Group 1",color = color[1], shape = shape[1])%>%
  visGroups(groupname = "Group 2",color = color[2], shape = shape[2])%>%
  visLegend(useGroups = TRUE,width = 0.1,position = "left",main="图例")

图像如下所示:

R可视化可交互网络图
## 高亮显示周围的节点,方式1
visNetwork(Newnodes, Newedages, height = "600px", width = "100%",
           main = "网络图",background = "lightblue") %>%
  # 添加图例
  visGroups(groupname = "Group 1",color = color[1], shape = shape[1])%>%
  visGroups(groupname = "Group 2",color = color[2], shape = shape[2])%>%
  visLegend(useGroups = TRUE,width = 0.1,position = "left",main="图例")%>%
  visOptions(highlightNearest = TRUE)%>%
  visLayout(randomSeed = 4)
R可视化可交互网络图
## 高亮显示周围的节点,方式2
visNetwork(Newnodes, Newedages, height = "600px", width = "100%",
           main = "网络图",background = "lightblue") %>%
  # 添加图例
  visGroups(groupname = "Group 1",color = color[1], shape = shape[1])%>%
  visGroups(groupname = "Group 2",color = color[2], shape = shape[2])%>%
  visLegend(useGroups = TRUE,width = 0.1,position = "left",main="图例")%>%
  visOptions(highlightNearest = list(enabled = TRUE, 
                                     degree = 2, hover = TRUE))%>%
  visLayout(randomSeed = 4)
R可视化可交互网络图
## 根据节点进行选择,方式1
visNetwork(Newnodes, Newedages, height = "600px", width = "100%",
           main = "网络图",background = "lightblue") %>%
  # 添加图例
  visGroups(groupname = "Group 1",color = color[1], shape = shape[1])%>%
  visGroups(groupname = "Group 2",color = color[2], shape = shape[2])%>%
  visLegend(useGroups = TRUE,width = 0.1,position = "right")%>%
  visOptions(selectedBy = list(variable = "id", multiple = T))%>%
  visLayout(randomSeed = 4)
R可视化可交互网络图
## 根据节点进行选择,方式2
visNetwork(Newnodes, Newedages, height = "600px", width = "100%",
           main = "网络图",background = "lightblue") %>%
  # 添加图例
  visGroups(groupname = "Group 1",color = color[1], shape = shape[1])%>%
  visGroups(groupname = "Group 2",color = color[2], shape = shape[2])%>%
  visLegend(useGroups = TRUE,width = 0.1,position = "right")%>%
  visOptions(selectedBy = "group", 
             highlightNearest = TRUE, 
             nodesIdSelection = TRUE)%>%
  visLayout(randomSeed = 4)
R可视化可交互网络图
visNetwork使用图片作为节点
## 图片的路径
path_to_images <- "https://raw.githubusercontent.com/datastorm-open/datastorm-open.github.io/master/visNetwork/data/img/indonesia/"
 
nodes <- data.frame(id = 1:4, 
                    shape = c("image", "circularImage"),
                    ## 必须制定图像的URL路径
                    image = paste0(path_to_images, 1:4, ".png"),
                    label = paste("image",1:4)
                    )
  
edges <- data.frame(from = c(2,4,3,3), to = c(1,2,4,2))

visNetwork(nodes, edges, width = "100%",main = "使用图片作为节点") %>% 
  # 图像使用线包裹
  visNodes(shapeProperties = list(useBorderWithImage = TRUE)) %>%
  visLayout(randomSeed = 2)
R可视化可交互网络图
## isNetwork和igraph结合
visNetwork(Newnodes, Newedages, height = "600px", width = "100%",
           main = "圆形布局网络图",background = "lightblue") %>%
  # 添加图例
  visGroups(groupname = "Group 1",color = color[1], shape = shape[1])%>%
  visGroups(groupname = "Group 2",color = color[2], shape = shape[2])%>%
  visLegend(useGroups = TRUE,width = 0.1,position = "left")%>%
  visOptions(highlightNearest = TRUE)%>%
  ## 圆形绘图
  visIgraphLayout(layout = "layout_in_circle")%>%
  visLayout(randomSeed = 4)
R可视化可交互网络图
visIgraph(karate,smooth = TRUE)%>%
  visIgraphLayout(layout = "layout_with_fr")%>%
  visOptions(highlightNearest = list(enabled = TRUE, 
                                     degree = 2, hover = TRUE))%>%
  visLayout(randomSeed = 4)
R可视化可交互网络图

visNetwork可视化决策树模型

usedata <- read.csv("data/Titanic处理后数据.csv")
head(usedata)

##   Pclass  Name    Sex Age SibSp Parch    Fare Embarked Survived
## 1      3   Mr.   male  22     1     0  7.2500        S        0
## 2      1  Mrs. female  38     1     0 71.2833        C        1
## 3      3 Miss. female  26     0     0  7.9250        S        1
## 4      1  Mrs. female  35     1     0 53.1000        S        1
## 5      3   Mr.   male  35     0     0  8.0500        S        0
## 6      3   Mr.   male  28     0     0  8.4583        Q        0

## 建立决策树模型

model <- rpart(Survived~.,data = usedata,
               control = rpart.control(cp = 0.01))

visTree(model,main = "classification Tree", width = "100%",
        nodesPopSize = TRUE, minNodeSize = 5, 
        maxNodeSize = 20)
R可视化可交互网络图
## 模型2
model <- rpart(Survived~.,data = usedata,
               control = rpart.control(cp = 0.008))
visTree(model,main = "classification Tree", width = "100%",
        nodesPopSize = TRUE, minNodeSize = 5, 
        maxNodeSize = 20)
R可视化可交互网络图

网络图的移动和缩放等

## 网络图的移动和缩放等
visNetwork(Newnodes, Newedages, height = "600px", width = "100%",
           main = "网络图",background = "lightblue") %>%
  # 添加图例
  visGroups(groupname = "Group 1",color = color[1], shape = shape[1])%>%
  visGroups(groupname = "Group 2",color = color[2], shape = shape[2])%>%
  visLegend(useGroups = TRUE,width = 0.1,position = "right")%>%
  visOptions(highlightNearest = TRUE)%>%
  visInteraction(dragNodes = TRUE,## 可移动节点
                 dragView = TRUE, # 移动图
                 zoomView = TRUE,  # 缩放
                 navigationButtons = TRUE # 下方添加按钮
                 )%>%
  visLayout(randomSeed = 4)
R可视化可交互网络图

使用networkD3可视化

library(networkD3)

data(MisLinks)
data(MisNodes)
head(MisLinks)
##   source target value
## 1      1      0     1
## 2      2      0     8
## 3      3      0    10
## 4      3      2     6
## 5      4      0     1
## 6      5      0     1
head(MisNodes)
##              name group size
## 1          Myriel     1   15
## 2        Napoleon     1   20
## 3 Mlle.Baptistine     1   23
## 4    Mme.Magloire     1   30
## 5    CountessdeLo     1   11
## 6        Geborand     1    9
# 颜色
color <- c("orange", "darkblue", "purple","darkred", "grey")
linkcolor <- color[(MisLinks$value %% 5) +1]
# Create graph
forceNetwork(Links = MisLinks, Nodes = MisNodes, 
             Source = "source", # 起点
             Target = "target", # 终点
             Value = "value", #Links data frame 线的宽.
             NodeID = "name", # 点的名称
             Group = "group", # 点的分组
             opacity = 0.8, #不透明度比例的数值
             zoom = TRUE, # 缩放
             linkColour = linkcolor, # 边的颜色
             arrows = TRUE,  # 是否带箭头
             legend = TRUE, # 根据节点的颜色添加图例
             fontSize  = 20,   #  节点标签字体的大小
             bounded = TRUE
             )
R可视化可交互网络图
可视化桑葚图
URL <- paste0(
        "https://cdn.rawgit.com/christophergandrud/networkD3/",
        "master/JSONdata/energy.json")
Energy <- jsonlite::fromJSON(URL)

# Plot
sankeyNetwork(Links = Energy$links, Nodes = Energy$nodes, Source = "source",
             Target = "target", Value = "value", NodeID = "name",
             units = "TWh", fontSize = 12, nodeWidth = 30)
R可视化可交互网络图

 

本文来自zhihu,观点不代表一起大数据-技术文章心得立场,如若转载,请注明出处:https://zhuanlan.zhihu.com/p/88361464

联系我们

在线咨询:点击这里给我发消息

邮件:23683716@qq.com

跳至工具栏