I'm working on a ham radio project and I've run into a bit of a wall. I'm trying to take a map and make a link between the transmitter and the receiver.
Data
rx_sign <- c("2E0ILY/KIWI","ND7M","KP4MD","N2HQI","WA2N","WA2ZKD","OE9TAV","2E0PYB","OE9TAV","2E0ILY/KIWI")
rx_lat <- c(52.896,36.229,38.688,43.021,35.813,43.146,47.438,51.771,47.438,52.896)
rx_lon <- c(-2.625,-116.042,-121.292,-76.458,-82.542,-77.625,9.625,0.708,9.625,-2.625)
tx_sign <- c("2E0JCC","AA5GI","AC9YY","AC9YY","AC9YY","AC9YY","DC6EB","DG8GAD","DG8GAD","DK9MS")
tx_lat <- c(51.479,35.479,41.813,41.813,41.813,41.813,49.479,48.438,48.438,50.479)
tx_lon <- c(-2.708,-95.042,-88.042,-88.042,-88.042,-88.042,6.958,9.875,9.875,8.958)
map_data <- data.frame(rx_sign,rx_lat,rx_lon,tx_sign,tx_lat,tx_lon)
My goal is to make a map that looks like this. I've connected cities on maps before but for whatever reason having multiple connections has stumped me.
In the past I've make pairs for each city with it's lat/lon then connected them like this. I know that this isn't the right way to do this with the data that I have but it's kind of stumped me and I can't quite get my head around how to fix this.
base_world <- map_data("world")
ggplot()
geom_polygon(data=base_world, aes(x=long, y=lat, group=group))
geom_line(data=map_data, aes(x=rx_lon, y=rx_lat), color="red", size=1)
geom_line(data=map_data, aes(x=tx_lon, y=tx_lat), color="green", size=1)
geom_point(data=map_data, aes(x=rx_lon, y=rx_lat), colour = "yellow", size=1.5, alpha=I(0.7))
geom_point(data=map_data, aes(x=tx_lon, y=tx_lat), colour = "blue", size=1.5, alpha=I(0.7))
theme(text = element_text(size=20), legend.position="none")
CodePudding user response:
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
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))