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

huangapple go评论59阅读模式
英文:

How do I edit these features of a network plot in R?

问题

我正在对社会流动性数据进行一些社交网络分析。我正在使用此程序 来识别相关职业的聚类(即具有高代际传递水平的职业聚类)。

这个程序使用了 ggplot 的版本来创建其社交网络图。我正在使用的代码如下:

# MONECA分析以查找段数并分配职业到段(mob.mat是包含原始和目标职业交叉表的数据框)
segments <- moneca(mx = mob.mat, segment.levels = 3, cut.off = 1, mode = "symmetric",
       delete.upper.tri = TRUE)

# 网络图
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网络图

但是,我想以一些方式更改图表,但我在这方面感到困难。这是一个特殊的软件包,我想很少有人使用,但我希望可以从网络图的工作原理(或ggplot的更一般工作原理)中得出一些一般化的东西。

首先,我想向聚类添加自定义标签(border.labels)。目前,它是从 'segments' 中绘制的,该 'segments' 只包含聚类的代码编号。然而,我想手动为每个聚类创建一个标签并将其添加到图表中。

其次,我想更改节点的填充为灰度。目前,它是基于节点所属的聚类(段)而着色的。我希望保持这个方案,但将其内部颜色变为灰度。

非常感谢任何帮助!我主要是 Stata 用户,所以在涉及 R 时有些陌生。

为了修改颜色,我尝试使用 moneca 软件包中的 segment.colors 命令,该命令可以创建一个段的灰度。但是,我不知道如何将此命令的输出传递给图表。

英文:

I am conducting some social network analysis on social mobility data. I am using this program to identify clusters of related occupations (i.e. clusters of occupations that have high levels of intergenerational transfer between them).

This program uses a version of ggplot to create its social network plots. The code I am using is as follows:

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

#network plot
edges &lt;- segment.edges(segments, cut.off = 1, method = &quot;all&quot;, segment.reduction = 0, level = 1)

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

This works well enough and produces the follow plot:

moneca network plot

However, I would like to alter the plot in a couple of ways and I am struggling to do this. This is an idiosyncratic package that I imagine few people have used, but I'm hoping something can be generalised from a knowledge of how network plots work (or how ggplot works more generally).

First, I would like to add custom labels to the clusters (border.labels). Currently this is drawn from 'segments', which just has the code numbers for the clusters. However, I would like to manually create a label for each cluster and add it to the plot.

Second, I would like to change the fill of the nodes to be greyscale. It is currently coloured based on the cluster (segment) the node belongs to. I would like to maintain this scheme but make the colours within it greyscale.

Any help would be very much appreciated! I am mostly a Stata person so I an somewhat at sea when it comes to R.

To amend the colours, I have tried using the segment.colors command from within the moneca package, which says it creates a grey scale for the segments. However, I have no idea how to pass the output of this command through to the plot.

答案1

得分: 1

我以前没有使用过这个包,但由于它生成了一个ggplot对象,即使包装函数的参数不完全符合您的要求,也可以事后进行修改。

假设我想用以下标签替换节点:

labels &lt;- c(&quot;阿甘正传&quot;, &quot;肖申克的救赎&quot;, 
            &quot;壁花少年&quot;, 
            &quot;黑暗骑士&quot;, &quot;变脸&quot;, &quot;这个男孩的生活&quot;, 
            &quot;生活多美好&quot;, 
            &quot;羊群的沉默&quot;, &quot;八英里&quot;, &quot;早餐俱乐部&quot;, 
            &quot;被解救的迪凯伦&quot;, 
            &quot;乌云背后的幸福线&quot;, &quot;闪灵&quot;, &quot;七宗罪&quot;, 
            &quot;美国丽人&quot;, 
            &quot;低俗小说&quot;, &quot;零黑30&quot;, &quot;独立日&quot;, &quot;拆弹部队&quot;, 
            &quot;教父&quot;, &quot;小镇暴徒&quot;, &quot;无间道风云&quot;, &quot;尖叫&quot;, 
            &quot;高空救援&quot;, 
            &quot;吉尔伯特葛莱普有什么问题&quot;, &quot;迷失东京&quot;,
            &quot;降临&quot;, 
            &quot;朱诺&quot;, &quot;陪我直到世界尽头&quot;, &quot;绿里奇迹&quot;, &quot;超能少年8&quot;, &quot;"
            饥饿游戏" 
            &quot;Misery&quot;, &quot;搏击俱乐部&quot;, &quot;福尔摩斯之福尔摩斯游戏&quot;, &quot;无法无天&quot;, 
            &quot;冰雪之骨&quot;, 
            &quot;出租车司机&quot;, &quot;拯救大兵瑞恩&quot;, &quot;黑天鹅&quot;,
            &quot;盗梦空间&quot;, &quot;波吉之夜&quot;, &quot;五十五十&quot;, &quot;兄弟连&quot;, &quot;血钻&quot;, 
            &quot;尽善尽美&quot;, &quot;角斗士&quot;, &quot;守法市民&quot;, 
            &quot;湖畔骚扰&quot;, 
            &quot;荣耀之路&quot;, &quot;Rate&quot;)

原来标签存储在输出的第8和第10层的aes_paramslabel参数中。因此,我们可以按如下方式进行覆盖:

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

现在,我们可以绘制finalMONECA,按要求将填充颜色设为灰度(尽管实际上有40种灰度要上色,所以这几乎没有用处):

finalMONECA + scale_fill_grey()

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

英文:

I haven't used this package before, but since it produces a ggplot object, it can be amended post-hoc even if the parameters of the wrapper function don't do all you want them to.

Suppose I want to replace the nodes with the following labels:

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

It turns out the labels are stored in the label parameter of the aes_params of the 8th and 10th layers of the output. So we can over-write them as follows:

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

Now we can plot finalMONECA, making the fill grayscale as requested (though there are literally 40 shades of gray to be colored, so this is next to useless)

finalMONECA + scale_fill_grey()

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

huangapple
  • 本文由 发表于 2023年2月23日 23:48:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定