3
$\begingroup$

I want to make a graphical representation of the people with the location, but coding is a little bit challenging.

Here is my input {Person,Location,Influences}

list = {{"John", 
"Michigan", {"Kelly", "Ralph", "James", "Kim", "Brown"}}, 
{"Kelly", "Minnesota", {"James", "Tim"}}, {"Kale", 
"Texas", {"Paul", "Claro"}}, {"Claro", "Texas", {}}, {"Ralph", 
"France", {"William", "Daniel"}}, {"Edwin", 
"France", {"William", "Kale"}}};

and I want my output to be graphical image such as

enter image description here

What is a good way to start?

$\endgroup$
7
$\begingroup$

You can use CommunityGraphPlot after constructing an edge list and community structure based on list:

edges = Flatten[Thread[First[#] -> Last[#]] & /@ list]
groups = GroupBy[list, #[[2]] &, Flatten[#[[All, {1, -1}]]] &]

CommunityGraphPlot[Graph[edges, VertexLabels -> "Name"], 
 Values @ groups, 
 CommunityLabels -> Keys[groups],
 CommunityRegionStyle -> (Opacity[.5, #]& /@ RandomColor[Length@Keys@groups]), 
 ImageSize->Large]

enter image description here

An alternative definition of groups produces something closer to your picture:

groups = GroupBy[list, #[[2]] &, #[[All, 1]] &];
CommunityGraphPlot[Graph[edges, VertexLabels -> "Name"], 
   Values @ groups, 
   CommunityLabels -> Keys[groups],
   CommunityRegionStyle -> (Opacity[.5, #] & /@ RandomColor[Length @ Keys @ groups]), 
   ImageSize -> Large] /. BezierCurve -> ( Line[#[[{1, -1}]]] &)

enter image description here

Since this is a Graphics object you can double-click on it and make changes;, e.g., you can move the node "William" outside the "France" blob to get

enter image description here

$\endgroup$
  • $\begingroup$ Thank you for your answer. I do not know why it does not work in my MMA. hmm. $\endgroup$ – Saesun Kim Jun 20 at 22:25
  • $\begingroup$ @SaesunKim what version/os are you using? Are you getting an error message? $\endgroup$ – kglr Jun 20 at 22:26
  • $\begingroup$ I copied your code (nice solution) and CommunityGraphPlot isn't working for me with this code. The intermediate values seem fine. Am I doing something wrong? And I'm not getting an error message, but rather, CommunityGraphPlot isn't evaluating. I'm using V12.0.0 on a mac. If I just use CommunityGraphPlot[Graph[edges, VertexLabels -> "Name"]] it seems fine. $\endgroup$ – Mark R Jun 20 at 22:27
  • $\begingroup$ @kglr I am using version 11.0. I do not get any error message, and I can plot the CommunityGraphPlot. But when I the code, communicityGroupplot isn't working $\endgroup$ – Saesun Kim Jun 20 at 22:28
  • 1
    $\begingroup$ Got it - you fixed a typo in the original that had "Claro" and "Calro". I did that and get your graph! It is this line in the original data: {"Kale", "Texas", {"Paul", "Calro"}} $\endgroup$ – Mark R Jun 20 at 22:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.