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)
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
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.
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
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))
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)
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)
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
Removing/Replacing Brackets from R String Using Gsub
R - Error When Using Geturl from Curl After Site Was Changed
R Eps Export and Import into Word 2010
How to Get The R Shiny Downloadhandler Filename to Work
R Plotly: Cannot Re-Arrange X-Axis When Axis Type Is Category
Modifying Plot in Ggplot2 Using As.Yearmon from Zoo
Visualizing Distance Between Nodes According to Weights - with R
How to Create a Continuous Legend (Color Bar Style) for Scale_Alpha
Make a Boxplot Without Whiskers
Existing Function to Combine Standard Deviations in R
How to Install/Locate R.H and Rmath.H Header Files
Flag First By-Group in R Data Frame
Get Tick Break Positions in Ggplot
Standard Error of Variance Component from The Output of Lmer
Using Mutate Rowwise Over a Subset of Columns
Ggplot2 Log Transformation for Data and Scales