如何使用 R 地图包中的 maps.text 标记国家而非地区
How to label countries, not regions, using maps.text in the R maps package
在地图包的文档或堆栈溢出中似乎没有任何示例试图绘制文本或其他信息来代替国家名称,而不是国家内的区域。
library(maps)
test <- structure(list(countries = structure(c(3L, 4L, 2L, 1L), .Label = c("France",
"Germany", "Italy", "UK"), class = "factor"), value = c(20, 13,
42, 6)), .Names = c("countries", "value"), row.names = c(NA,
-4L), class = "data.frame")
map.text("world", c("France", "Germany", "Italy", "UK"))
我计划用这些值替换国家名称。解决方案可能类似于
map.text("world",test$countries, labels=test$values)
但我不知道如何让它去掉所有不必要的区域标签。
map.text
试图用正则表达式匹配区域,导致了这个问题。您可以使用 exact = TRUE
来禁用它,或者通过在每个之后放置 $
(行尾)来利用它。不过,严格来说,英国并不是一个国家,因此您需要 select 组成部分,或以其他方式解决它。
总而言之,
map.text("world", region = c("France$", "Germany$", "Italy$",
"UK:Great Britain", "UK:Northern Ireland"))
returns
如果不是完全完美的话,哪个更好。设置标签让我们让它看起来像我们想要的:
map.text("world", region = c("France$", "Germany$", "Italy$",
"UK:Great Britain", "UK:Northern Ireland"),
labels = c("France", "Germany", '', "UK", "Italy"))
小心,标签参数中的顺序很挑剔。
如果您这样重构 test
:
test <- data.frame(countries = c("Germany$", "France$", "UK:Northern Ireland",
"UK:Great Britain", "Italy$"),
value = c(42, 6, '', 13, 20), stringsAsFactors = FALSE)
看起来像
> test
countries value
1 Germany$ 42
2 France$ 6
3 UK:Northern Ireland
4 UK:Great Britain 13
5 Italy$ 20
您可以绘制 value
列作为标签:
map.text("world", region = test$countries, labels = test$value)
在地图包的文档或堆栈溢出中似乎没有任何示例试图绘制文本或其他信息来代替国家名称,而不是国家内的区域。
library(maps)
test <- structure(list(countries = structure(c(3L, 4L, 2L, 1L), .Label = c("France",
"Germany", "Italy", "UK"), class = "factor"), value = c(20, 13,
42, 6)), .Names = c("countries", "value"), row.names = c(NA,
-4L), class = "data.frame")
map.text("world", c("France", "Germany", "Italy", "UK"))
我计划用这些值替换国家名称。解决方案可能类似于
map.text("world",test$countries, labels=test$values)
但我不知道如何让它去掉所有不必要的区域标签。
map.text
试图用正则表达式匹配区域,导致了这个问题。您可以使用 exact = TRUE
来禁用它,或者通过在每个之后放置 $
(行尾)来利用它。不过,严格来说,英国并不是一个国家,因此您需要 select 组成部分,或以其他方式解决它。
总而言之,
map.text("world", region = c("France$", "Germany$", "Italy$",
"UK:Great Britain", "UK:Northern Ireland"))
returns
map.text("world", region = c("France$", "Germany$", "Italy$",
"UK:Great Britain", "UK:Northern Ireland"),
labels = c("France", "Germany", '', "UK", "Italy"))
如果您这样重构 test
:
test <- data.frame(countries = c("Germany$", "France$", "UK:Northern Ireland",
"UK:Great Britain", "Italy$"),
value = c(42, 6, '', 13, 20), stringsAsFactors = FALSE)
看起来像
> test
countries value
1 Germany$ 42
2 France$ 6
3 UK:Northern Ireland
4 UK:Great Britain 13
5 Italy$ 20
您可以绘制 value
列作为标签:
map.text("world", region = test$countries, labels = test$value)