我想為我的地圖下方的國家名稱添加一個圖例。
我有這個不同地區事件發生頻率的資料框:
trend_country_freq <- structure(list(country = c("US", "CN", "KR", "IN", "AU", "GB",
"JP"), n = c(25L, 20L, 12L, 5L, 2L, 1L, 1L), country_name = c("USA",
"China", "South Korea", "India", "Australia", "UK", "Japan")), row.names = c(1L,
2L, 3L, 4L, 5L, 7L, 8L), class = "data.frame")
現在我使用maps和ggplot2包來創建顯示事件發生頻率的世界地圖:
library(maps)
library(ggplot2)
world_map <- map_data("world")
world_map <- subset(world_map, region != "Antarctica")
ggplot(trend_country_freq)
geom_map(
dat = world_map, map = world_map, aes(map_id = region),
fill = "white", color = "#7f7f7f", size = 0.25
)
geom_map(map = world_map, aes(map_id = country_name, fill = n), size = 0.25)
scale_fill_gradient(low = "#fff7bc", high = "#cc4c02", name = "Total Cases")
expand_limits(x = world_map$long, y = world_map$lat)
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank())
theme(axis.title = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank())
結果如下所示:

但我實際上想要這樣的東西:

你有想法如何生成這樣的地圖嗎?非常感謝!
uj5u.com熱心網友回復:
我們可以簡單地將文本寫成 geom 層:
ggplot(trend_country_freq)
geom_map(
dat = world_map, map = world_map, aes(map_id = region),
fill = "white", color = "#7f7f7f", size = 0.25
)
geom_map(map = world_map, aes(map_id = country_name, fill = n), size = 0.25)
scale_fill_gradient(low = "#fff7bc", high = "#cc4c02", name = "Total Cases")
expand_limits(x = world_map$long, y = world_map$lat)
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank())
theme(axis.title = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank())
geom_text(aes(label = paste0(country_name, ' (', n, ')'),
x = rep(c(-50, 50), each = 4)[-8],
y = rep(seq(-90, -120, -10), 2)[-8]),
hjust = 0)

uj5u.com熱心網友回復:
這是一個 hack,不知道它有多棒:
transform(trend_country_freq, # NEW
txt = sprintf("%s (%i)", country_name, n), #
vj = -1.2 * seq(nrow(trend_country_freq)) #
) |> #
ggplot() # CHANGED
geom_map(
dat = world_map, map = world_map, aes(map_id = region),
fill = "white", color = "#7f7f7f", size = 0.25
)
geom_map(map = world_map, aes(map_id = country_name, fill = n), size = 0.25)
scale_fill_gradient(low = "#fff7bc", high = "#cc4c02", name = "Total Cases")
expand_limits(x = world_map$long, y = world_map$lat)
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank())
theme(axis.title = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank())
geom_text(aes(label = txt, vjust = vj), x = -Inf, y = -Inf, hjust = 0) # NEW

uj5u.com熱心網友回復:
另外的選擇:
library(tidyverse)
library(maps)
p1 <- trend_country_freq |>
mutate(lab = paste0(country_name, " (", n, ")")) |>
ggplot()
geom_map(
dat = world_map, map = world_map, aes(map_id = region),
fill = "white", color = "#7f7f7f", size = 0.25
)
geom_map(map = world_map, aes(map_id = country_name, fill = n, color = lab), size = 0.25)
scale_fill_gradient(low = "#fff7bc", high = "#cc4c02", name = "Total Cases")
scale_color_manual(values = rep(NA, 7), name = "")
expand_limits(x = world_map$long, y = world_map$lat)
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank(),
legend.key = element_blank())
guides(color = guide_legend(direction='horizontal',label.position = "left",
override.aes = list(fill = NA, color = NA)))
guide_color <- cowplot::get_legend(p1 guides(color = "none"))
cowplot::plot_grid(p1
guides(fill = "none")
theme(legend.position = "bottom"),
guide_color,
ncol = 2, rel_widths = c(.85, .15))

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/527536.html
標籤:rggplot2几何图
