如何用GEOM_MAP用各自的国旗填充国家?

发布于 2025-02-13 16:36:31 字数 1792 浏览 1 评论 0原文

我正在使用软件包ggplot2,并使用函数geom_map to 填充具有不同颜色的国家。

我想知道是否可以用标志而不是颜色填充?

所需结果:

“在此处输入图像描述”

我正在做:

ALL_EU <-c("Russia", "Germany", "UK", "France", "Italy", "Spain", "Ukraine", "Poland", "Romania", "Netherlands", "Belgium", "Czech Republic", "Greece", "Portugal", "Sweden", "Hungary", "Belarus", "Austria", "Serbia", "Switzerland", "Bulgaria", "Denmark", "Finland","Slovakia", "Norway", "Ireland", "Croatia", "Moldova", "Bosnia and Herzegovina","Albania", "Lithuania", "North Macedonia", "Slovenia", "Latvia", "Estonia","Montenegro", "Luxembourg"  , "Malta", "Iceland", "Andorra", "Monaco", "Liechtenstein","San Marino", "Vatican", "Kosovo")

#Filter some regions out just for plot
world_map <- map_data("world",region = ALL_EU[-which(ALL_EU%in%c("Russia","Iceland"))])
world_map <- world_map%>%filter(subregion!="Svalbard"&subregion!="Jan Mayen"|is.na(subregion))

然后绘制它,请注意,在这种情况下,我使用fill = “标志”因此,我可以用scale_fill_manual更改颜色和传奇,也可以使所有国家/地区蓝色仅用于一个可复制的示例。

ggplot() +
    theme_void()+
    geom_map(dat = world_map,map = world_map, 
             aes(map_id = region,fill = "Flags"),color = "gray",size = 0.25)+
    scale_fill_manual(name = "Countries", values = c("Flags" = "blue"))+
    expand_limits(x = world_map$long, y = world_map$lat)

也,即使可能,也可以将几个国家填满,例如欧盟的旗帜。但这可能是另一个问题。谢谢你!

I'm working with package ggplot2 and using the function geom_map to fill the countries with different colors.

I was wondering if it is possible to fill with flags instead of colors?

Desired result:

enter image description here

I'm doing:

ALL_EU <-c("Russia", "Germany", "UK", "France", "Italy", "Spain", "Ukraine", "Poland", "Romania", "Netherlands", "Belgium", "Czech Republic", "Greece", "Portugal", "Sweden", "Hungary", "Belarus", "Austria", "Serbia", "Switzerland", "Bulgaria", "Denmark", "Finland","Slovakia", "Norway", "Ireland", "Croatia", "Moldova", "Bosnia and Herzegovina","Albania", "Lithuania", "North Macedonia", "Slovenia", "Latvia", "Estonia","Montenegro", "Luxembourg"  , "Malta", "Iceland", "Andorra", "Monaco", "Liechtenstein","San Marino", "Vatican", "Kosovo")

#Filter some regions out just for plot
world_map <- map_data("world",region = ALL_EU[-which(ALL_EU%in%c("Russia","Iceland"))])
world_map <- world_map%>%filter(subregion!="Svalbard"&subregion!="Jan Mayen"|is.na(subregion))

and then plot it, note that in this case I use fill = "Flags" so I can then change the color and legend with scale_fill_manual, also I make all countries blue just for a reproducible example.

ggplot() +
    theme_void()+
    geom_map(dat = world_map,map = world_map, 
             aes(map_id = region,fill = "Flags"),color = "gray",size = 0.25)+
    scale_fill_manual(name = "Countries", values = c("Flags" = "blue"))+
    expand_limits(x = world_map$long, y = world_map$lat)

enter image description here

Also, and even if its possible, fill several countries with, for example, the flag of the European Union. But this is probably for another question. Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

匿名的好友 2025-02-20 16:36:31

这是一个选项,使用优点 ggpattern flagon 软件包。

# Get mapping of country codes to countries; need this to select the right
# flags by country codes as provided by `flagon`
# Must also clean up country names to match those from `world_map`
library(tidyverse)
library(flagon)
data_flags <- flagon::country_codes %>%
    select(country, ccode) %>%
    mutate(flag = flagon::flags(ccode)) %>%
    mutate(country = case_when(
        str_detect(country, "Macedonia") ~ "North Macedonia",
        str_detect(country, "Moldova") ~ "Moldova",
        str_detect(country, "Vatican") ~ "Vatican",
        str_detect(country, "United Kingdom") ~ "UK",
        TRUE ~ country)) %>%
    filter(country %in% ALL_EU)
    

library(ggpattern)
world_map %>%
    left_join(data_flags, by = c("region" = "country")) %>%
    filter(!is.na(flag)) %>%
    ggplot() +
    theme_void() +
    geom_map_pattern(
        map = world_map,
        aes(map_id = region, pattern_type = region, pattern_filename = I(flag)), 
        pattern = "image",
        pattern_type = "expand",
        color = "gray", 
        size = 0.25) +
    expand_limits(x = world_map$long, y = world_map$lat)

Here is an option using the excellent ggpattern and flagon packages.

# Get mapping of country codes to countries; need this to select the right
# flags by country codes as provided by `flagon`
# Must also clean up country names to match those from `world_map`
library(tidyverse)
library(flagon)
data_flags <- flagon::country_codes %>%
    select(country, ccode) %>%
    mutate(flag = flagon::flags(ccode)) %>%
    mutate(country = case_when(
        str_detect(country, "Macedonia") ~ "North Macedonia",
        str_detect(country, "Moldova") ~ "Moldova",
        str_detect(country, "Vatican") ~ "Vatican",
        str_detect(country, "United Kingdom") ~ "UK",
        TRUE ~ country)) %>%
    filter(country %in% ALL_EU)
    

library(ggpattern)
world_map %>%
    left_join(data_flags, by = c("region" = "country")) %>%
    filter(!is.na(flag)) %>%
    ggplot() +
    theme_void() +
    geom_map_pattern(
        map = world_map,
        aes(map_id = region, pattern_type = region, pattern_filename = I(flag)), 
        pattern = "image",
        pattern_type = "expand",
        color = "gray", 
        size = 0.25) +
    expand_limits(x = world_map$long, y = world_map$lat)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文