R: Connecting Dots on a Map

Connecting 2 points in a map using ggplot

No need for the final ggplot(p) (that's probably throwing an error on your end) and I'd use geom_path:

p <- ggmap(Bangalore)
p <- p + geom_point(data=d, aes(x=lon, y=lat),color="red",size=3)
p + geom_path(data=d, aes(x=lon, y=lat), color="black", size=1)
## or....
p + geom_line(data=d, aes(x=lon, y=lat), color="black", size=1)

Sample Image

Connecting two specific points in a map

You can get a subset based on the points you want to be connected;

link1 <- countries[countries$country %in% c("USA", "Spain","China"), ]
link2 <- countries[countries$country %in% c("Australia", "Poland"), ]

base_world <- map_data("world")
p <- ggplot() +
geom_polygon(data=base_world, aes(x=long, y=lat, group=group)) +
geom_line(data=link1, aes(x=Longitude, y=Latitude), color="red", size=1) +
geom_line(data=link2, aes(x=Longitude, y=Latitude), color="green", size=1) +
geom_point(data=countries, aes(x=Longitude, y=Latitude), colour = "cyan", size=5, alpha=I(0.7)) + #set the color outside of `aes`
theme(text = element_text(size=20), legend.position="none") #remove the legend

and your plot would look like:

> p

MAP-ggplot2-worldmap-connections-p

You may need to play around a little bit for formatting issues.

Connecting points in ggmap based on an ID

Are you looking for something like this?

aux %>%
inner_join(aux, by = c("id_conn" = "id")) %>%
select(-id_conn.y) -> aux2

ggmap(Gmap) +
geom_segment(data = aux2, aes(x = lon_coord.x, y = lat_coord.x,
xend = lon_coord.y, yend = lat_coord.y),
color = "yellow", arrow = arrow(length = unit(0.2,"cm"))) +
geom_point(aes(x=lon_coord.x, y=lat_coord.x),data=aux2) +
geom_text(aes(x=lon_coord.x, y=lat_coord.x, label = id), data=aux2, hjust = -0.5)

The secret ingredient is geom_segment() which allows you to add line segments. You can adjust the appearance of the arrows as you like.

Sample Image

Connecting two specific points on a map for linking between HF radio site

You’ll want to pivot your data longer, with an id linking each tx/rx pair. You can then pass this id to the group aesthetic in geom_line(). (This also gives you a tx vs. rx variable you can map to color in geom_point().)

library(tidyr)
library(dplyr)
library(ggplot2)

map_data <- map_data %>%
mutate(link = row_number()) %>%
pivot_longer(
!link,
names_to = c("txrx", ".value"),
names_sep = "_"
)

p <- ggplot(map_data) +
geom_polygon(data=base_world, aes(x=long, y=lat, group=group)) +
geom_line(aes(x=lon, y=lat, group = link), color="red", size=1) +
geom_point(data=map_data, aes(x=lon, y=lat, colour = txrx), alpha=0.7) +
theme(text = element_text(size=20), legend.position="none") +
coord_quickmap()

p

Sample Image

If you want to crop to just certain regions, you can set limits inside coord_quickmap().

p + coord_quickmap(xlim = c(-140, 20), ylim = c(20, NA))

Sample Image

Adding coordinates as points to a map in R

You need to reproject these points, then you can use standard geom_point and geom_text. Your points are far too close together to see them all separately on a world map though:

df <-  sf::sf_project("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0", 
"+proj=moll", df) %>%
as.data.frame() %>%
setNames(c("long", "lat")) %>%
cbind(object = object)

world %>%
st_transform(crs = "+proj=moll") %>%
ggplot() +
geom_sf() +
theme_minimal() +
geom_point(data = df, aes(long, lat)) +
geom_text(data = df, aes(long, lat, label = object),
vjust = c(1, 0, -1), hjust = 1)

Sample Image

If you zoom it, it is much clearer:

world %>% 
st_transform(crs = "+proj=moll") %>%
ggplot() +
geom_sf() +
theme_minimal() +
geom_point(data = df, aes(long, lat)) +
geom_text(data = df, aes(long, lat, label = object),
vjust = c(0.5, 1, -1), hjust = 1.2) +
coord_sf(ylim = c(4200000, 4700000), xlim = c(-10.75, -10.25) * 10^6)

Sample Image

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

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.



Related Topics



Leave a reply



Submit