Message Boards Message Boards

Graph from CountryData

I take these informations in CountryData base, and I want to construct the bipartite graph. What is the solution?
 risk = CountryData["SouthAmerica", "NaturalHazards"]
 nom = CountryData["SouthAmerica", "Name"]
 mat = Transpose[{nom, risk}]
 {{"Earthquake", "Flood"}, {"Flood"}, {"Drought",
   "Flood"}, {"Earthquake", "Tsunami", "Volcano"}, {"Drought",
   "Earthquake", "Volcano"}, {"Drought", "Earthquake", "Flood",
   "Landslide", "Volcano"}, {}, {"Flood",
   "Thunderstorm"}, {"Flood"}, {"Flood"}, {"Earthquake", "Flood",
   "Landslide", "Tsunami", "Volcano"}, {}, {"Drought",
  "Flood"}, {"Drought", "Flood", "Landslide"}}

{"Argentina", "Bolivia", "Brazil", "Chile", "Colombia", "Ecuador", \
"Falkland Islands", "French Guiana", "Guyana", "Paraguay", "Peru", \
"Suriname", "Uruguay", "Venezuela"}

{{"Argentina", {"Earthquake",
   "Flood"}}, {"Bolivia", {"Flood"}}, {"Brazil", {"Drought",
   "Flood"}}, {"Chile", {"Earthquake", "Tsunami",
   "Volcano"}}, {"Colombia", {"Drought", "Earthquake",
   "Volcano"}}, {"Ecuador", {"Drought", "Earthquake", "Flood",
   "Volcano"}}, {"Falkland Islands", {}}, {"French Guiana", {"Flood",
   "Thunderstorm"}}, {"Guyana", {"Flood"}}, {"Paraguay", {"Flood"}}, \
{"Peru", {"Earthquake", "Flood", "Landslide", "Tsunami",
   "Volcano"}}, {"Suriname", {}}, {"Uruguay", {"Drought",
   "Flood"}}, {"Venezuela", {"Drought", "Flood", "Landslide"}}}
POSTED BY: André Dauphiné
2 Replies
The first step is to get the data into the form of a list of rules. This of type Country -> Disaster.

There are many ways to program this depending on your coding style. Here is one way that shows many useful ways of manipulating data:
mat /. {country_, disaster_} :> (Rule[country, #] & /@ disaster)
graphData = Flatten[%, 2]
graphData is now of the correct form to be used with the Graph function. So we give it the data with the correct arguments:
Graph[graphData, VertexLabels -> "Name", GraphLayout -> "BipartiteEmbedding",ImagePadding -> 60]

Choosing the right optional arguments often involves a little trial and error to see whether what you get is what you want. I had to look up in the documentation for information about the different layouts. The ImagePadding is there to make sure the VertexLabels are all visible in the output.
POSTED BY: Sean Clarke
Thanks very much, It's OKA. Dauphiné
POSTED BY: André Dauphiné
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract