GGRAPHE/ggplot2[R]中的手动图例

GGRAPHE/ggplot2[R]中的手动图例,r,ggplot2,ggraph,R,Ggplot2,Ggraph,除此之外,我在这里问一些新问题,如何在由ggraph构建的图例中进行调整 以下是生成当前绘图的脚本: ## Packages library(igraph) library(tidygraph) library(ggraph) library(ggplot2) library(tidyverse) ## Edge and node edge <- data.frame(from=c(0,0,0,0,1,2,3),

除此之外,我在这里问一些新问题,如何在由
ggraph
构建的图例中进行调整

以下是生成当前绘图的脚本:

    ## Packages
    library(igraph)
    library(tidygraph)
    library(ggraph)
    library(ggplot2)
    library(tidyverse)

    ## Edge and node
    edge <- data.frame(from=c(0,0,0,0,1,2,3),
                       to=c(0,1,2,3,0,0,0),
                       weight=c(1,3,1,1,3,1,1))
    node <- data.frame(id=c(0,1,2,3),
                       p=c(9,1,0,0),
                       w=c(0,2,0,0),
                       s=c(0,1,1,1),
                       size=c(9,3,1,1),
                       gr=c(0,1,1,2))

    ## Load data frames as tbl_graph class
    edge <- edge %>% mutate(from=from+1,to=to+1)
    net <- tbl_graph(nodes=node,edges=edge,directed=TRUE)

    ## Set arrows
    ar <- arrow(angle=30,length=unit(5,"mm"),ends="last",type="closed")
    ## Plot
    ggraph(net,layout="graphopt") +
    ## Edges
    geom_edge_link(aes(start_cap=circle(log(node1.size)+2,unit="native"),
                       end_cap=circle(log(node2.size)+2,unit="native"),
                       width=weight,label=weight),
        position="identity",angle_calc="along",force_flip=TRUE,
        label_dodge=unit(4.0,"mm"),label_push=unit(-0.4,"mm")) +
        ## Width scale
        scale_edge_width(range=c(0.4,4),breaks=c(1:10),name="Movements\nbetween zones") +
    ## Add arrows separately
    geom_edge_link(arrow=ar,aes(start_cap=circle(log(node1.size)+1,unit="native"),
        end_cap=circle(log(node2.size)+1,unit="native"))) +
    ## Nodes
        ## Plot location id
        geom_node_label(aes(label=id,hjust=log(size+6),vjust=log(size+6)),repel=TRUE,
                        label.padding=unit(0.8,"mm"),label.r=unit(0.0,"mm"),label.size=0.1,size=3.5) +
        ## Circle
        geom_node_circle(aes(r=log(size)+1),color="black",fill="white",size=0.4) +
        ## Plot work activity
        geom_node_text(aes(size=w,hjust=log(w)+0.4),label="w",color="red",
                       position="identity",vjust=0.4,fontface="bold") +
        ## Plot school activity
        geom_node_text(aes(size=s,hjust=-log(s)-0.2),label="s",color="blue",
                       position="identity",vjust=0.4,fontface="bold") +
        ## Size scale
        scale_size(range=c(0,5),breaks=c(1:100),name="Numberof\nActivities",
                   guide=guide_legend(override.aes=list(label="a",color="black"))) +
        # scale_color() +
    ## Theme
    theme_graph() + coord_fixed()

然而,由于它仍然是丑陋的代码,我仍在等待您的建议,以更适当的操作

## Packages
    library(igraph)
    library(tidygraph)
    library(ggraph)
    library(ggplot2)
    library(tidyverse)

    ## Edge and node
    edge <- data.frame(from=c(0,0,0,0,1,2,3),
                       to=c(0,1,2,3,0,0,0),
                       weight=c(1,3,1,1,3,1,1))
    node <- data.frame(id=c(0,1,2,3),
                       p=c(9,1,0,0),
                       w=c(0,2,0,0),
                       s=c(0,1,1,1),
                       size=c(9,3,1,1),
                       gr=c(0,1,1,2))

    ## Load data frames as tbl_graph class
    edge <- edge %>% mutate(from=from+1,to=to+1)
    # THIS IS QUITE STRANGE OPERATION FOR CHANGING COLOR
    node <- node %>% mutate_at(vars(p,w,s,size),funs(ifelse(.==0,NA,.)))
    net <- tbl_graph(nodes=node,edges=edge,directed=TRUE)

## Set arrows
ar <- arrow(angle=30,length=unit(5,"mm"),ends="last",type="closed")
## Plot
ggraph(net,layout="graphopt") +
    ## Edges
    geom_edge_link(aes(start_cap=circle(log(node1.size)+2,unit="native"),
                       end_cap=circle(log(node2.size)+2,unit="native"),
                       width=weight,label=weight),
        position="identity",angle_calc="along",force_flip=TRUE,
        label_dodge=unit(4.0,"mm"),label_push=unit(-0.4,"mm")) +
        ## Width scale
        scale_edge_width(range=c(0.4,4),breaks=c(1:10),name="Movements\nbetween zones") +
    ## Add arrows separately
    geom_edge_link(arrow=ar,aes(start_cap=circle(log(node1.size)+1,unit="native"),
        end_cap=circle(log(node2.size)+1,unit="native"))) +
    ## Nodes
        ## Plot location id
        geom_node_label(aes(label=id,hjust=log(size+6),vjust=log(size+6)),repel=TRUE,
                        label.padding=unit(0.8,"mm"),label.r=unit(0.0,"mm"),label.size=0.1,size=3.5) +
        ## Circle
        geom_node_circle(aes(r=log(size)+1),color="black",fill="white",size=0.4) +
        ## Plot work activity: NOT KNOW WHY IT WORKS!!
        geom_node_text(aes(size=ifelse(is.na(w),0,w),hjust=log(w)+0.4,color=ifelse(w==0,"white","red")),label="w",
                       position="identity",vjust=0.4,fontface="bold") +
        ## Plot school activity
        geom_node_text(aes(size=ifelse(is.na(s),0,s),hjust=-log(s)-0.2,color=ifelse(s==0,"white","blue")),label="s",
                       position="identity",vjust=0.4,fontface="bold") +
        ## Edit legend
        scale_size_continuous(range=c(0,5),breaks=c(1:100),name="Numberof\nActivities",
                   guide=guide_legend(override.aes=list(label="a",color="black"))) +
        scale_color_manual(name="Type of Activity",guide=guide_legend(override.aes=list(label=c("w","s"))),
                           values=c("red","blue"),labels=c("Work","School")) +
    ## Theme
    theme_graph() + coord_fixed()