# How to specify VertexCoordinates as geographic coordinates of cities?

I have been very busy these days with giving the VertexCoordinates of the cities the geographic coordinates from CityData["CityName","Coordinates"] of the cities. Could somebody please help me on writing algorithm?

Graph[{"Uppsala" -> "Marsta", "Marsta" -> "Uppsala",
"UpplandsVasby" -> "Sollentuna", "Sollentuna" -> "UpplandsVasby",
"UpplandsVasby" -> "Marsta", "Marsta" -> "UpplandsVasby",
"Stockholm" -> "Boo", "Boo" -> "Stockholm",
"Stockholm" -> "Lidingo", "Lidingo" -> "Stockholm",
"Stockholm" -> "Sollentuna", "Sollentuna" -> "Stockholm",
"Stockholm" -> "Taby", "Taby" -> "Stockholm"},
VertexLabels -> "Name"]

• Which Graph[]? Can you maybe show what you've already done in Mathematica? Commented Apr 21, 2013 at 11:37
– Alex
Commented Apr 21, 2013 at 12:32
• Are you saying that you would like to plot the graph laid out in such a way that the vertices are placed according to their geographic location? Commented Apr 21, 2013 at 12:56
• Exactly!!My graph is just simple here but yes my problem is how to locate vertices on their geographical coordinates and second step to show it in the map.Not just a Plot but the graph itself!!!When I want to show the flow on the vertices.
– Alex
Commented Apr 21, 2013 at 13:09
• Try ref/VertexCoordinates and ref/CityData in the help? Commented Apr 21, 2013 at 14:14

You could extract coordinates using CityData and set it by SetProperty.

g = Graph[{"Uppsala" -> "Marsta", "Marsta" -> "Uppsala",
"UpplandsVasby" -> "Sollentuna", "Sollentuna" -> "UpplandsVasby",
"UpplandsVasby" -> "Marsta", "Marsta" -> "UpplandsVasby",
"Stockholm" -> "Boo", "Boo" -> "Stockholm",
"Stockholm" -> "Lidingo", "Lidingo" -> "Stockholm",
"Stockholm" -> "Sollentuna", "Sollentuna" -> "Stockholm",
"Stockholm" -> "Taby", "Taby" -> "Stockholm"},
VertexLabels -> "Name", ImagePadding -> 40];

coords = CityData[#, "Coordinates"] & /@ VertexList[g];

SetProperty[g, {VertexCoordinates -> Reverse[coords, 2],
Prolog -> {Gray, CountryData["Sweden", "Polygon"]}}]


If SetProperty doesn't work (v9.0.0)

cities = {"Uppsala", "Marsta", "UpplandsVasby", "Sollentuna",
"Stockholm", "Boo", "Lidingo", "Taby"};

coords = CityData[#, "Coordinates"] & /@ cities;

g = Graph[
cities, {"Uppsala" -> "Marsta", "Marsta" -> "Uppsala",
"UpplandsVasby" -> "Sollentuna", "Sollentuna" -> "UpplandsVasby",
"UpplandsVasby" -> "Marsta", "Marsta" -> "UpplandsVasby",
"Stockholm" -> "Boo", "Boo" -> "Stockholm",
"Stockholm" -> "Lidingo", "Lidingo" -> "Stockholm",
"Stockholm" -> "Sollentuna", "Sollentuna" -> "Stockholm",
"Stockholm" -> "Taby", "Taby" -> "Stockholm"},
VertexCoordinates -> Reverse[coords, 2],
Prolog -> {Gray, CountryData["Sweden", "Polygon"]},
VertexLabels -> "Name", ImagePadding -> 40]

• thats great !!But when i am evaluating your proposed codes, it just gives me the simple Graph that I had!!Without Sweden Map and coordinates!!What could be wrong?Do you have output in the Graph?
– Alex
Commented Apr 21, 2013 at 15:27
• Which version of Mathematica are you using? In v 9.0.1 it works fine. If it doesn't work, you can just add option when you construct graph or reconstruct graph like: Graph[VertexList[g], EdgeList[g], VertexCoordinates->.., Prolog->...] Commented Apr 21, 2013 at 16:06
• black means "it's defined", so nothing wrong with it. did you try to build graph with coords? I added that version.. Commented Apr 21, 2013 at 18:24

I tend to use GraphPlot[] instead if I need to depict the graph with other primitives, so here's my take:

cityGraph = {"Uppsala" -> "Marsta", "Marsta" -> "Uppsala", "UpplandsVasby" -> "Sollentuna",
"Sollentuna" -> "UpplandsVasby", "UpplandsVasby" -> "Marsta",
"Marsta" -> "UpplandsVasby", "Stockholm" -> "Boo", "Boo" -> "Stockholm",
"Stockholm" -> "Lidingo", "Lidingo" -> "Stockholm",
"Stockholm" -> "Sollentuna", "Sollentuna" -> "Stockholm",
"Stockholm" -> "Taby", "Taby" -> "Stockholm"}

GraphPlot[cityGraph, Background -> ColorData["Legacy", "PowderBlue"],
EdgeRenderingFunction -> ({Blue, Line[#]} &), MultiedgeStyle -> 1/20,
PlotRange -> {{17, 19}, {58, 60}},
Prolog -> {Gray, CountryData["Sweden", {"FullPolygon", "Equirectangular"}]},
VertexCoordinateRules ->
Map[# -> Reverse[CityData[{#, "Sweden"}, "Coordinates"]] &,
VertexList[Graph[cityGraph]]], VertexLabeling -> Tooltip,
VertexRenderingFunction -> ({Directive[AbsolutePointSize[4], Red],
Tooltip[Point[#1], #2]} &)]


(If executed in Mathematica, there should be tooltips associated with each vertex.)

• I suppose it ought to be explicitly noted that the coordinates in the polygon returned by CountryData[] are in (long, lat) format, while CityData[] coordinates return in (lat, long) format. Commented Apr 21, 2013 at 15:07
• Your coordinates comment about CountryData and CityData and their differences is really amazing !!!Many Thanks!!!Thats very helpful to know that!!But about your suggestion to use GraphPlot the point is I want to show the optimized flow in the my Graph then better to use new version 9 availabilities.I wonder by using GraphPlot I can use "FlowGraph" capabilities?!
– Alex
Commented Apr 21, 2013 at 15:54
• Can you maybe edit your question to show the fancy Graph[] with flows that you want to be embedded into a map? Commented Apr 21, 2013 at 16:06
• Many thanks for the suggestion .I will definitely do that But on that way I have to explain many other things not related to issue.After developing whole GraphNetwork I will upload it as sample later.
– Alex
Commented Apr 21, 2013 at 16:45
• It doesn't have to be the entire network. You've already supplied a minimal example; now, how would you put the flows into this small graph you've presented? Commented Apr 21, 2013 at 16:47