当使用气泡饼图在地图上的不同站点中映射 3 个类别的计数时,我找不到准确指示图例中每个(气泡)饼图大小的解决方案。
pacman::p_load(ggplot2, dplyr, tidyr, sf, rnaturalearth, rnaturalearthdata, scatterpie)
#my summarised data
coropango_summary <- data.frame(site = c("KOU","LMB","MMG","MND","MRG","MSS","NDO","OYN","PTK","TRQ"),
lat = c(1.80,0.41,0.34,1.34,1.53,0.47,0.11,1.36,-0.59,-3.03),
long = c(12.29,10.13,12.52,13.12,11.56,11.34,10.45,11.34,12.49,11.29),
Bos = c(2,45,1,1,1,0,1,2,0,1),
Gnu = c(2,1,0,1,0,1,0,0,0,1),
undet = c(0,0,0,0,0,0,1,0,1,0),
host_sum = c(4,46,1,2,1,1,2,2,1,2)
)
#get map of Gabon and its neighbours
central_africa_map <- ne_countries(scale = "medium", continent = "Africa", returnclass = "sf")
gabon_map <- subset(central_africa_map, admin == "Gabon")
neighboring_countries <- subset(central_africa_map, admin != "Gabon" & sovereignt %in% c("Equatorial Guinea", "Cameroon", "Republic of the Congo"))
gabon_regions <- ne_states(country = "Gabon", returnclass = "sf")
ocean_color <- "steelblue1"
host_counts <- unique(sort(coropango_summary$host_sum))
gabon <- ggplot(data = gabon_map) +
geom_sf(fill = "white", color = "black") +
geom_sf(data = neighboring_countries, fill = "grey95", color = "black", linetype = "solid") +
geom_sf(data = gabon_map, fill = "white", color = "black", linewidth = .5) +
geom_sf(data = gabon_regions, fill = NA, color = "darkgrey", linetype = "dotted", linewidth = .4) +
geom_scatterpie(data = coropango_summary, aes(x = long, y = lat, group = site,
r = sqrt(host_sum)/10, size = host_sum),
cols = c("Bos","Gnu", "undet"),
color = NA, alpha = 0.6) +
geom_scatterpie_legend(radius = sqrt(coropango_summary$host_sum)/10,
x = 8, y = -2.95, n = 4, labeller = function(value) host_counts) +
scale_fill_manual(values = c("Bos" = "goldenrod2","Gnu" = "darkgreen","undet" = "firebrick4")) +
geom_hline(yintercept = 0, color = "black", linewidth = 0.2, linetype = "solid") +
annotate("text", x = 12.2, y = 2.7, label = "Cameroon", size = 4, fontface = "bold") +
annotate("text", x = 10.6, y = 1.62, label = "Equatorial\nGuinea", size = 4, fontface = "bold") +
annotate("text", x = 13.5, y = -3.1, label = "Republic of the\nCongo", size = 4, fontface = "bold") +
labs(fill = "Host") +
theme_minimal() +
coord_sf(xlim = c(7, 15), ylim = c(-4, 3), expand = FALSE) +
theme(
panel.background = element_rect(fill = ocean_color, color = NA),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_text(),
panel.grid = element_blank()
) +
scale_y_continuous(breaks = c(0))
结果是一张地图,其中最小和最大气泡的尺寸似乎合适,但中间气泡(尺寸 2 和 4)则不然,它们比相应的散点大得多。将这些气泡与地图上的散点相匹配是主要问题。这是我到目前为止所管理的[结果](https://i.sstatic.net/cwFPJWfg.png)
其次,“host_sum”的映射出现在图例中,我不知道如何删除它。 我认为圆形区域应该在某个地方匹配,但我不知道如何以及在哪里匹配。非常感谢任何帮助。 我尝试将每个大小(1,2,4,46)映射为单独的“geom_scatterpie”,但无法控制未转换值的大小。
另外,我也尝试过[单独创建图例](为ggplot气泡图创建同心圆图例),但结果并不好,可能是由于我对R的理解有限。
要将散点图例中的半径与散点图相匹配,请使用
breaks=
参数而不是 radius=
。 “问题”是,在幕后 geom_scatterpie_legend
将根据传递给 scales::breaks_extended
的值向量使用 radius=
选择良好的中断。对于第二期,请从 size = host_sum
中删除 geom_scatterpie
:
library(ggplot2)
library(scatterpie)
ggplot(data = gabon_map) +
geom_sf(fill = "white", color = "black") +
geom_sf(data = neighboring_countries, fill = "grey95", color = "black", linetype = "solid") +
geom_sf(data = gabon_map, fill = "white", color = "black", linewidth = .5) +
geom_sf(data = gabon_regions, fill = NA, color = "darkgrey", linetype = "dotted", linewidth = .4) +
geom_scatterpie(
data = coropango_summary, aes(
x = long, y = lat, group = site,
r = sqrt(host_sum) / 10
),
cols = c("Bos", "Gnu", "undet"),
color = NA, alpha = 0.6
) +
geom_scatterpie_legend(
breaks = sqrt(host_counts) / 10,
x = 8, y = -2.95, n = 4,
labeller = function(value) host_counts
) +
scale_fill_manual(values = c("Bos" = "goldenrod2", "Gnu" = "darkgreen", "undet" = "firebrick4")) +
geom_hline(yintercept = 0, color = "black", linewidth = 0.2, linetype = "solid") +
annotate("text", x = 12.2, y = 2.7, label = "Cameroon", size = 4, fontface = "bold") +
annotate("text", x = 10.6, y = 1.62, label = "Equatorial\nGuinea", size = 4, fontface = "bold") +
annotate("text", x = 13.5, y = -3.1, label = "Republic of the\nCongo", size = 4, fontface = "bold") +
labs(fill = "Host") +
theme_minimal() +
coord_sf(xlim = c(7, 15), ylim = c(-4, 3), expand = FALSE) +
theme(
panel.background = element_rect(fill = ocean_color, color = NA),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_text(),
panel.grid = element_blank()
) +
scale_y_continuous(breaks = c(0))