简体   繁体   中英

Plot coordinates to a map in R and connect them by lines (= graph on a map)

I would like to create a map of Germany with R and place some points (with geo coordinates) on it. Then I want to connect some of the points with lines of different color and width. So I could also say, I want to build a graph with fixed position of the nodes over the picture of a map. The map is not really important (can be very coarse) and should not be google maps.

For example with the following code I get a coarse map of Germany:

    library(maps)
    map("world", regions="Germany")

How can I add points to this map and connect them?

EDIT: My data looks like this:

I have a data frame for the points to place on the map:

    Name  |  Latitude    |   Longitude
    ------+--------------+------------
    Point1|  50.110556   |   8.682222
    Point2|  52.516667   |   13.383333
    Point3|  48.137222   |   11.575556

And I have a data frame with the lines to draw between the points:

    Source | Target | color | width
    -------+--------+-------+-------
    Point1 | Point2 |  red  |   2   
    Point1 | Point3 | green |   5   

Thank you! :)

You can use functions like points and lines.

library(maps)
map("world", regions="Germany")
set.seed(10)
dfPoints <- data.frame(
  long = 10 + runif(10, -2, 2),
  lat = 50 + runif(10, -2, 2)
  )
points(x = dfPoints$long, y = dfPoints$lat, col = "red")
lines(x = dfPoints$long, y = dfPoints$lat, col = "blue")

You can also take a look at the ggmap package, based on ggplot2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM