I'm plotting a Europe map as follows:
library(ggplot2)
library(grid)
library(rworldmap)
## Get the world map: ##
worldMap <- getMap()
## Define vector with all European countries: ##
v.europe <- c("Norway", "Sweden", "Finland", "Denmark", "United Kingdom","Ireland", "Greece",
"Belgium", "Netherlands", "France", "Spain", "Portugal", "Luxembourg", "Croatia",
"Germany", "Switzerland", "Austria", "Slovenia", "Italy", "Bulgaria", "Romania",
"Czech Rep.", "Slovakia", "Hungary", "Poland", "Bosnia Hercegovina", "Serbia",
"Turkey", "Ukraine", "Moldova", "Belarus", "Estonia", "Latvia", "Lithuania",
"Montenegro", "Albania", "Macedonia")
## Select only the index of countries of Europe: ##
indEU <- which(worldMap$NAME%in%v.europe)
## Extract longitude and latitude border's coordinates of countries: ##
df.europeCoords <- lapply(indEU, function(i){
df <- data.frame(worldMap@polygons[[i]]@Polygons[[1]]@coords)
df$region = as.character(worldMap$NAME[i])
colnames(df) <- list("long", "lat", "region")
return(df)
})
df.europeCoords <- do.call("rbind", df.europeCoords)
names(df.europeCoords) <- c("longitude", "latitude", "country")
## Deletes/Removes borders of PLOT: ##
ax <- list(
title = "",
zeroline = FALSE,
showline = FALSE,
showticklabels = FALSE,
showgrid = FALSE
)
## Plot the map: ##
p <- ggplot(data = df.europeCoords, aes(x = longitude, y = latitude, group = country,
text = paste("<b>", country, '</b>\n')),
color = "grey50", size = 0.1) +
geom_polygon() +
coord_map(xlim = c(-13, 35), ylim = c(32, 71)) +
theme_classic() +
theme(axis.text.x = element_blank(), axis.text.y = element_blank(), axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(), axis.title = element_blank(), legend.position = "none",
plot.margin = unit(0 * c(-1.5, -1.5, -1.5, -1.5), "lines"))
## Create plot_ly() object: ##
EuropePlot <- plotly::ggplotly(p, tooltip = "text") %>%
layout(xaxis = ax, yaxis = ax)
This gives the following plot:

I would like to draw a red border around a group of countries. This group should include the following countries: Austria, Germany, Denmark, Belgium, France and Switzerland. How can I do this?
Perhaps the simplest way to do this is to create a new column in your data frame which acts as a flag for which countries should be colored red:
df.europeCoords$red <- df.europeCoords$country %in%
c("Austria", "Germany", "Denmark", "Belgium", "France", "Switzerland")
Now you map this variable to the color aesthetic, specifying NA outline color for the countries that are FALSE in column red, and specifying "red" for the countries that have the value TRUE:
p <- ggplot(data = df.europeCoords,
aes(x = longitude, y = latitude, group = country,
text = paste("<b>", country, '</b>\n')),
color = "grey50", size = 0.1) +
geom_polygon(aes(color = red), size = 1) +
scale_color_manual(values = c(NA, "red")) +
coord_map(xlim = c(-13, 35), ylim = c(32, 71)) +
theme_classic() +
theme(axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
axis.title = element_blank(),
legend.position = "none",
plot.margin = unit(0 * c(-1.5, -1.5, -1.5, -1.5), "lines"))
p

It is also possible to draw a red border around a group of countries. This can be easily achieved by using sf::st_union().
Create plot with red borders around certain countries:
library(tidyverse)
library(spatialrisk)
library(sf)
red_borders <- spatialrisk::europe_countries %>%
filter(admin %in% c("Austria", "Germany", "Denmark",
"Belgium", "France", "Switzerland"))
ggplot() +
geom_sf(data = spatialrisk::europe_countries) +
geom_sf(data = red_borders, color = "red") +
theme_void()

Or remove internal boundaries in sf-object:
ggplot() +
geom_sf(data = spatialrisk::europe_countries) +
geom_sf(data = sf::st_union(red_borders), color = "red", alpha = 0) +
theme_void()

Created on 2020-10-11 by the reprex package (v0.3.0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With