I would like to construct a map with rnaturalearth.
On top of that map I want to have three dots representing three cities, which are connected with lines. Line color corresponds to the value of F (indicates how close those countries are), with the scale color for the map (as it is on the figure)
I have the code for map:
target <- c("Armenia", "Azerbaijan", "Turkey", "Iran", "Georgia", "Russia", "Syria", "Iraq")
countries<- ne_countries(scale = 10,returnclass = 'sf') %>%
filter(name %in% target)
coordinates <- data.frame(
region = c("A", "B", "C"),
longitude = c(39.80949367, 39.65485163, 38.47802923),
latitude = c(46.7793274, 36.99614676, 43.3863868)
)
map <- ggplot(data=countries)+geom_sf(fill = "lightgoldenrodyellow") +
xlim(36,49) +
ylim(36.5,42) + theme_void() +
theme(panel.background = element_rect(fill = "lightblue2"))
print(map)
How to plot dots for cities and colored lines?
Thank you!

Your problem shows a bit of a mix of solutions. Thus, I offer 2 Options below. But read up on what you use. For example, how ggplot works, how data frame manipulation are implemented, or what sf does/offers.
For a start:
{ggplot} works with
layersthat you add on top of each other. This is symbolised by the+operator.You have produced already the underlying map with your code. For this you worked with the mapdata from {rnaturalearth} and used {sf} for handling the geospatial aspects. For example, this helps a bit with the projection of the map (or you can tap into other functions of the {sf} package.
This delivers your base map:
Option 1: combining {ggplot} and {sf} ... takes care of geospatial placement
There is one side-aspect of combining {ggplot2} and {sf}. The latter ensures that "ordinary" data frame columns are handled as coordinates (without being a sf-object).
Thus, you could "add" a point layer on too of your base-map.
Note: I also add a text label to show another problem.
So we now have (one) point super-imposed on the base-map. Unfortunately, you picked an area that only shows B.
We can now use the same side-effect to plot links between the cities. {ggplot} allows you to plot
line-segments defined by a start and end position. Let's create alinksdata frame:If you plot this .... unfortunately no lines are shown. {ggplot} gives you a warning about missing (points, text, and segments). We have seen this already before ... but it is clear that the area you picked does not allow to show all elements (as they are outside your viewing area from a geo-spatial perspective)
To fix the "missing" points/links issue, check your
limits. You have picked latitude values that eliminate A and C.For this change
ylimaccordingly:To fix the scale, convert your F values into "discrete" variables. {ggplot} will interpret them as continuous. For example, you can turn them into a factor or character when creating the data frame (or inside your plot call, (i) mutate( ... F = as.character(c(3,6,9) ...) or (ii) geom_segment( ... color = as.character(F) ...).
Option 2: using {sf}
As you picked {sf} for the base map, you can also address the problem in {sf}.
The key here is that you have to "cast" your data frames of points and links to so-called sf-objects.
Working with {sf} gives you access to powerful geospatial tools. However, it is not straight forward as you need to read up on other (geospatial) concepts.
For a start:
geometryPOINTgeometryLINESTRINGI leave the other geometries out here.
Coercing from a data frame of POINT positions to sf-object is relatively straight forward.
For a LINESTRING we need to reshape the data, as a linestring can conceptually comprise multiple points (and not just the start and end point).
For our case we create a
long tableof the start and end positions, label then with thenameandFvalue (which are "constant" along the link) by using group_by() and then coerce the linestrings of start- and end-positions.Work through the code line-by-line if you want to get a better grip on the operations.
We now have a sf-object for our links (city pair connections).
With this we can go back to {ggplot) and use
geom_sf()-layers:This yields:
You can now beautify the visualisation by renaming the scale, changing colors, etc.
Good luck.