英文:
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")
这个代码足够好,可以生成如下图:
但是,我想以一些方式更改图表,但我在这方面感到困难。这是一个特殊的软件包,我想很少有人使用,但我希望可以从网络图的工作原理(或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 & 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")
This works well enough and produces the follow 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 <- c("阿甘正传", "肖申克的救赎",
"壁花少年",
"黑暗骑士", "变脸", "这个男孩的生活",
"生活多美好",
"羊群的沉默", "八英里", "早餐俱乐部",
"被解救的迪凯伦",
"乌云背后的幸福线", "闪灵", "七宗罪",
"美国丽人",
"低俗小说", "零黑30", "独立日", "拆弹部队",
"教父", "小镇暴徒", "无间道风云", "尖叫",
"高空救援",
"吉尔伯特葛莱普有什么问题", "迷失东京",
"降临",
"朱诺", "陪我直到世界尽头", "绿里奇迹", "超能少年8", ""
饥饿游戏"
"Misery", "搏击俱乐部", "福尔摩斯之福尔摩斯游戏", "无法无天",
"冰雪之骨",
"出租车司机", "拯救大兵瑞恩", "黑天鹅",
"盗梦空间", "波吉之夜", "五十五十", "兄弟连", "血钻",
"尽善尽美", "角斗士", "守法市民",
"湖畔骚扰",
"荣耀之路", "Rate")
原来标签存储在输出的第8和第10层的aes_params
的label
参数中。因此,我们可以按如下方式进行覆盖:
finalMONECA$layers[[8]]$aes_params$label <- labels[1:40]
finalMONECA$layers[[10]]$aes_params$label <- labels[41:51]
现在,我们可以绘制finalMONECA
,按要求将填充颜色设为灰度(尽管实际上有40种灰度要上色,所以这几乎没有用处):
finalMONECA + scale_fill_grey()
英文:
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 <- 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")
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 <- labels[1:40]
finalMONECA$layers[[10]]$aes_params$label <- 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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论