避免与 rworldmap 重叠文本标签

Avoid overlapping text labels with rworldmap

我正在使用 rworldmap 创建世界地图并使用 text 函数添加国家名称。但是,文本标签重叠。我尝试了 adjpos 参数,但到目前为止没有成功。有什么建议吗?

library(rworldmap)

DF = data.frame(q = c("VAT","ESP","CAN","AND","MCO","VCT",
                      "NLD", "LUX", "GBR", "LIE", "BEL", "DNK",
                      "SWE","NOR","ATG","AUS", "BHS", "BHR","BRB",
                      "BLZ","BTN","BRN","KHM","CAN","SWZ","GRD",
                      "JAM","JPN","KWT","JOR","LSO","MYS","MAR",
                      "NZL","OMN","PNG","QAT","KNA","LCA","SAU",
                      "SLB","THA","TON","TUV","ARE" ), 
                Assignment = ("Monarchies Worldwide"))

country = c("Vatican","Spain","Monaco","Canada","Andorra","Saint Vincent and the Grenadines",
            "Netherlands", "Luxembourg","United Kingdom", "Liechtenstein","Belgium","Denmark",
            "Sweden","Norway","Anitgua and Barbuda","Australia", "Bahamas", "Bahrain","Brunei Darussalam",
            "Belize","Bhutan","Cambodia","Swaziland","Grenada","Jamaica","Japan","Kuwait","Jordan",
            "Lesotho","Malaysia","Morocco","New Zealand","Oman","Papua New Guinea","Qatar","Saint Kitts and Nevis",
            "Saint Lucia","Saudi Arabia","Solomon Islands","Thailand","Tonga","Tuvalu","United Arab Emirates")

Map = joinCountryData2Map(DF, 
                          joinCode = "ISO3", 
                          nameJoinColumn ="q", 
                          mapResolution = "coarse") 

mapParams = mapCountryData(Map, 
                           nameColumnToPlot = "Assignment", 
                           catMethod = "categorical",
                           missingCountryCol = gray(.4))

country_coord <- data.frame(coordinates(Map))[country, ]

text(x = country_coord$X1, 
     y = country_coord$X2, 
     labels = row.names(country_coord), 
     adj = NULL, pos = 3,  offset = 0, vfont = NULL,
     cex = 0.3, col = "blue", font = 5)

回答

基本 text 函数没有此功能。您可能不得不依赖其他包来实现您想要的:

  • 查找适用于基本图形的包,例如 basicPlotteR
  • 切换到使用 ggplot2::ggplot 绘图并使用 ggrepel::geom_text_repelggrepel::geom_label_repel

1. basicPlotteR::addTextLabels

根据一些设置,它会替换文本,并使用线条来指示文本属于哪个国家/地区。

# remotes::install_github("JosephCrispell/basicPlotteR")
library(basicPlotteR)

# replace text() with this
addTextLabels(x = country_coord$X1, y = country_coord$X2, labels = row.names(country_coord), cex.label = 0.3, col.label = "blue")


2。 ggrepel::geom_text_repel

这将做同样的事情,但有更多的自定义选项。请注意,如果无法根据您的要求绘制标签,geom_text_repel(和 geom_label_repel)将删除标签。

library(ggplot2)
library(ggrepel)

mymap <- fortify(Map)
mymap$monarch <- mymap$id %in% country

ggplot(data = mymap, aes(x = long, y = lat)) + 
  theme_void() +
  geom_map(map = mymap,
           aes(group = group, map_id = id,
               fill = monarch),
           color = "#ffffff") +
  scale_fill_manual(values = c("#666666", "#FF0000")) +
  ggrepel::geom_text_repel(data = country_coord, 
                           aes(x = X1, y = X2), 
                           label = row.names(country_coord),
                           size = 2,
                           col = "blue")


3。 ggrepel::geom_label_repel

这将像 geom_text_repel 一样起作用(也可以删除不符合要求的标签),但文本周围会有一个漂亮的框。

ggplot(data = mymap, aes(x = long, y = lat)) + 
  theme_void() +
  geom_map(map = mymap,
           aes(group = group, map_id = id,
               fill = monarch),
           color = "#ffffff") +
  scale_fill_manual(values = c("#666666", "#FF0000")) +
  ggrepel::geom_label_repel(data = country_coord, 
                           aes(x = X1, y = X2), 
                           label = row.names(country_coord),
                           size = 2,
                           col = "blue")