ggplot2地图Home Country图例填充颜色异常的解决方法
问题描述
我有一份包含工作地点(纬度、经度)、研究系统(淡水、海水/海洋、两者兼具)以及研究者所属国家(Home)的数据集。尝试在世界地图上展示信息:用点的形状表示研究系统(System),用形状的填充颜色表示所属国家(Home)。当前地图上的点形状与颜色显示正常,但Home Country图例显示黑色点而非对应的填充色,如何让Home Country图例显示正确的填充色?
原代码如下:
library("ggplot2") library("sf") library("rnaturalearth") library("rnaturalearthdata") rm(list = ls()) dev.off() # Example dataset people2 <- as.data.frame(matrix(ncol = 4, nrow = 3)) colnames(people2)[1:4] <- c('Lat','Lon','System','Home') people2$Lat <- c(46.86197, 45.83034, -27.96287) people2$Lon <- c(-113.98460, 15.98697, 153.38142) people2$System <- c('Both', 'Freshwater', 'Marine') people2$Home <- c('China', 'Croatia', 'Australia') people2 <- st_as_sf(people2, coords = c("Lon", "Lat"), crs = 4326) # Geographic representation of Earth's countries world <- ne_countries(scale = "medium", returnclass = "sf") # Geographic representation of Earth's large lakes lakes <- rnaturalearth::ne_download(scale = 110, type = 'lakes', category = 'physical') %>% sf::st_as_sf(lakes110, crs = epsg:4326) # Geographic representation of Earth's oceans ocean <- st_polygon(list(cbind(c(seq(-180, 179, len = 100), rep(180, 100), seq(179, -180, len = 100), rep(-180, 100)), c(rep(-90, 100), seq(-89, 89, len = 100), rep(90, 100), seq(89, -90, len = 100))))) |> st_sfc(crs = "WGS84") |> st_as_sf() # Global basemap in Robinson projection p0 <- ggplot(data = world) + geom_sf(data = ocean, fill = "aliceblue") + geom_sf(color = "gray60", fill = "white") + geom_sf(data = lakes, mapping = aes(geometry = geometry), color = "gray60", fill = "aliceblue") + coord_sf(crs = "+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs", expand = F) + theme_bw() + theme(panel.border = element_blank(), panel.background = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), axis.title = element_blank()) p0 # Colors, shapes, and shape sizes for map plotting cols <- c("Australia" = "#a50026", "China" = "#fee090", "Croatia" = "#4575b4") shapes <- c("Freshwater" = 21, "Marine" = 22, "Both" = 24) shape_size = c("Freshwater" = 2.5, "Marine" = 2.2, "Both" = 2.2) # Plotting the geographic data p0 + geom_sf(data = people2, aes(fill = Home, shape = System, size = System)) + scale_fill_manual(values = cols) + scale_shape_manual(values = shapes) + scale_size_manual(values = shape_size) + coord_sf(crs = "+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs", expand = F) + guides(fill = guide_legend(ncol = 1, title.position = 'top', title.hjust = 0.5), shape = guide_legend(ncol = 1, title.position = 'top', title.hjust = 0.5)) + labs(fill = "Home Country") + theme(axis.text.x = element_blank(), axis.text.y = element_blank(), legend.position = 'bottom', legend.title = element_text(size = 14, color = 'black'), legend.text = element_text(size = 14), legend.background = element_blank())
解决方案
问题根源是fill图例默认使用无填充属性的简单点形状(比如pch=1),这类形状无法展示填充色,所以呈现黑色。只需在guides()的fill参数中添加override.aes,强制图例使用带填充属性的形状即可。
修改后的核心代码如下:
# Plotting the geographic data p0 + geom_sf(data = people2, aes(fill = Home, shape = System, size = System)) + scale_fill_manual(values = cols) + scale_shape_manual(values = shapes) + scale_size_manual(values = shape_size) + coord_sf(crs = "+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs", expand = F) + guides(fill = guide_legend(ncol = 1, title.position = 'top', title.hjust = 0.5, override.aes = list(shape = 21, size = 2.5)), # 新增override.aes设置 shape = guide_legend(ncol = 1, title.position = 'top', title.hjust = 0.5)) + labs(fill = "Home Country") + theme(axis.text.x = element_blank(), axis.text.y = element_blank(), legend.position = 'bottom', legend.title = element_text(size = 14, color = 'black'), legend.text = element_text(size = 14), legend.background = element_blank())
关键修改说明
override.aes = list(shape = 21, size = 2.5):强制Home Country图例使用带填充的圆形(shape=21),并设置合适大小,确保填充色正常显示。- 你也可以选择其他带填充属性的形状(比如22方形、23菱形),只要形状编号在21-25之间,都支持fill颜色设置。
内容的提问来源于stack exchange,提问作者tassones
相关产品推荐
相关产品推荐

