【问题标题】:Border line around a group of certain countries in a map地图中一组特定国家的边界​​线
【发布时间】:2021-01-23 11:51:49
【问题描述】:

我正在绘制如下欧洲地图:

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)

这给出了以下情节:

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

【问题讨论】:

    标签: r ggplot2 maps plotly


    【解决方案1】:

    也可以在一组国家周围绘制红色边框。这可以通过使用 sf::st_union() 轻松实现。

    在某些国家/地区周围创建带有红色边框的绘图:

      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()
    

    或者删除 sf-object 中的内部边界:

      ggplot() +
        geom_sf(data = spatialrisk::europe_countries) +
        geom_sf(data = sf::st_union(red_borders), color = "red", alpha = 0) +
        theme_void()
    

    reprex package (v0.3.0) 于 2020 年 10 月 11 日创建

    【讨论】:

    • 如果没有sf 包,这是否也可以实现,就像上面的代码一样?
    • st_union 来自 sf 包,这在 base R 中是做不到的。你不想使用 sf 包的原因是什么?
    • 我这个包有问题/错误,没有这个包我已经做了这么多(我自己的情节已经很发达了,这里的问题我只提到了一个基本情节。)。所以我更喜欢基本的方式。但我会尝试将st.union 与我已经开发的情节混合在一起。
    • 不幸的是,sf.union不适合我的情节!
    • 您可以使用填充参数:geom_sf(data = red_borders, fill = "red")。这能解决问题吗?
    【解决方案2】:

    也许最简单的方法是在您的数据框中创建一个新列,作为应将哪些国家/地区标记为红色的标志:

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

    现在您将此变量映射到颜色美学,在red 列中为FALSE 的国家指定轮廓颜色NA,为具有TRUE 值的国家指定"red"

    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
    

    【讨论】:

    • 是否可以用您的版本擦除内部边界?这样边界线仅围绕一组国家/地区绘制,例如您的答案下方的答案。不应使用包 sf
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-30
    相关资源
    最近更新 更多