如何在R中编辑网络图的这些特征?

zqdjd7g9  于 2023-02-26  发布在  其他
关注(0)|答案(1)|浏览(155)

我正在对社会流动性数据进行一些社会网络分析,我使用this program来识别相关职业的集群(即具有高水平代际转移的职业集群)。
这个程序使用ggplot的一个版本来创建它的社交网络图。我正在使用的代码如下:

#MONECA analysis to find number of segments & assign occupations to segments (mob.mat is a dataframe containing the cross-tabulation of origin and destination occupations)
segments<- moneca(mx = mob.mat, segment.levels = 3, cut.off = 1, mode = "symmetric",
       delete.upper.tri = TRUE)

#network plot
edges <- segment.edges(segments, cut.off = 1, method = "all", segment.reduction = 0, level = 1)

finalMONECA <- gg.moneca(segments, level = seq(segments$segment.list),
                         layout = layout.matrix(segments), edges = edges,
                         mode = "directed", vertex.size = "total", vertex.fill = "segment",
                         vertex.alpha = 1, vertex.color = "black", vertex.shape = 21,
                         show.edges = TRUE, edge.size = 1, edge.alpha = "weight",
                         edge.color = "grey76", edge.line = "solid", show.text = FALSE,
                         text.size = 3, text.color = "black", text.alpha = 1, text.vjust = 1.5,
                         show.borders = TRUE, border.size = 1, border.fill = NA,
                         border.color = "black", border.alpha = 1, border.padding = 0.7,
                         border.text = TRUE, border.labels = "segments", border.text.size = 4,
                         border.text.color = "black", border.text.vjust = -0.2,
                         border.text.hjust = 1, midpoints = TRUE, 
                         midpoint.arrow = arrow(angle = 20, length = unit(0.33, "cm"), ends = "last", type = "closed"),
                         edge.text = FALSE, edge.text.size = 3, edge.text.alpha = 0.9, legend = "side")

这样做效果很好,并生成以下曲线:
moneca network plot
然而,我想用几种方法来改变图,我正在努力做到这一点。这是一个独特的包,我想很少有人用过,但我希望一些东西可以从网络图如何工作的知识(或ggplot更普遍的工作方式)。
首先,我想给聚类添加自定义标签(border.labels)。目前这是从“segments”中提取的,它只有聚类的代码。但是,我想手动为每个聚类创建一个标签,并将其添加到图中。
第二,我想改变填充的节点是灰度。它目前的颜色基于集群(段)的节点属于。我想保持这个方案,但使其内部的颜色灰度。
任何帮助都将非常感谢!我主要是一个Stata的人,所以我有点茫然,当谈到R。
为了修改颜色,我试着使用moneca包中的segment.colors命令,它会为线段创建一个灰度,但是,我不知道如何将这个命令的输出传递到绘图中。

jyztefdp

jyztefdp1#

我以前没有使用过这个包,但是由于它生成了一个ggplot对象,所以即使 Package 器函数的参数没有完成您希望它们完成的所有任务,也可以对它进行事后修改。
假设我想用以下标签替换节点:

labels <- c("Forrest Gump", "The Shawshank Redemption", 
            "The Perks of Being a Wallflower", 
            "The Dark Knight", "Changeling", "This Boy's Life", 
            "It's a Wonderful Life", 
            "The Silence of the Lambs", "8 Mile", "The Breakfast Club", 
            "Django Unchained", 
            "Silver Linings Playbook", "The Shining", "Seven", 
            "American Beauty", 
            "Pulp Fiction", "Zero Dark Thirty", "Argo", "The Hurt Locker", 
            "The Godfather", "The Town", "The Departed", "Scream", 
            "Up in the Air", 
            "What's Eating Gilbert Grape", "Lost in Translation",
            "The Conjuring", 
            "Juno", "Stand by Me", "The Green Mile", "Super 8", "
            Jarhead", 
            "Misery", "Fight Club", "Shutter Island", "Lawless", 
            "Winter's Bone", 
            "Taxi Driver", "Saving Private Ryan", "Black Swan",
            "Inception", "Boogie Nights", "50/50", "Brothers", "Blood Diamond", 
            "A Few Good Men", "Gladiator", "Law Abiding Citizen", 
            "Lakeview Terrace", 
            "Glory Road", "Rate")

原来标签存储在输出的第8层和第10层的aes_paramslabel参数中,所以我们可以如下所示覆盖它们:

finalMONECA$layers[[8]]$aes_params$label <- labels[1:40]
finalMONECA$layers[[10]]$aes_params$label <- labels[41:51]

现在我们可以绘制finalMONECA,按照要求填充灰度(尽管实际上有40种灰度要着色,所以这几乎是无用的)

finalMONECA + scale_fill_grey()

相关问题