gpt4 book ai didi

r - map 中某些国家/地区周围的边界线

转载 作者:行者123 更新时间:2023-12-03 21:45:55 25 4
gpt4 key购买 nike

我正在绘制欧洲 map ,如下所示:

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)

这给出了以下 plotly :

enter image description here

我想在一组国家周围画一个红色边界。该组应包括以下国家:奥地利、德国、丹麦、比利时、法国和瑞士。我该怎么做?

最佳答案

也许最简单的方法是在您的数据框中创建一个新列,作为一个标志,哪些国家应该被涂成红色:

df.europeCoords$red <- df.europeCoords$country %in% 
c("Austria", "Germany", "Denmark", "Belgium", "France", "Switzerland")

现在您将此变量映射到颜色美学,为 red 列中 FALSE 的国家/地区指定 NA 轮廓颜色,并指定"red" 对于值为 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

enter image description here

关于r - map 中某些国家/地区周围的边界线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64262412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com