使用ggplot与geom_sf绘制地图时,颜色梯度图例无法显示求助
问题描述
用ggplot和geom_sf绘制图卢兹地图,已保留主干道、河流、铁路等元素,现在要将n个自行车站点根据向量F0(取值0到1)着色,颜色从白色到深红渐变,但无法在图例中显示该颜色梯度。
原代码
library(osmdata) # wrapper for Overpass API from Open Street Maps library(sf) # library for manipulating Simple features objects library(dplyr) library(ggplot2) toulouse <- opq("Toulouse") head(toulouse) #routes toulouse_street1 <- toulouse %>% add_osm_feature(key = "highway", value = c("motorway","motorway_link")) %>% osmdata_sf() #### water toulouse_water1<- toulouse %>% add_osm_feature(key = "natural", value = c("water")) %>% osmdata_sf() toulouse_water1$osm_polygons <- toulouse_water1$osm_polygons %>% filter(natural == "water") all_toulouse_water <- st_union(toulouse_water1$osm_polygons,toulouse_water1$osm_multipolygons) #### train et métro toulouse_railway1 <- toulouse %>% add_osm_feature(key = "public_transport", value=c("station")) %>% osmdata_sf() toulouse_railway2 <- toulouse %>% add_osm_feature(key = "railway",value=c("rail")) %>% osmdata_sf() ##### # loadings ###### library(sf) library(autoimage) shp <- st_read(dsn = '/Users', layer = 'factor_loadings_with_geometry_T2') coord<-shp$geometry coord_new<-st_transform(coord,crs=4326) F0<-shp$F5 #F0 is between 0 and 1 mycol<-colorRampPalette(c("white","bisque","indianred1","red3","red4","grey20")) coul=mycol(11) F0_ok=numeric() F0_ok=coul[F0+1] ########### ### print the plot street_plot <- ggplot() + geom_sf(data = toulouse_railway2$osm_lines, inherit.aes = FALSE, size = 0.2, colour = "navajowhite3")+ geom_sf(data = toulouse_railway2$osm_lines, inherit.aes = FALSE, size = 0.2, colour = "grey35", linetype="dotted")+ geom_sf(data = toulouse_street1$osm_lines, inherit.aes = FALSE, color = "grey40", size = 1) + geom_sf(data = toulouse_water1$osm_multipolygons, inherit.aes = FALSE, fill="skyblue1", alpha = .8, lwd=0) + geom_sf(data = coord_new, color = F0_ok,#rgb(1-F0, F0*F0, F0*F0, maxColorValue = 1), fill = F0_ok,#rgb(1-F0, F0*F0, F0*F0, maxColorValue = 1), shape = 21, size = 1.4, inherit.aes = FALSE )+ annotation_north_arrow(which_north = "true", height = unit(1, "cm"), width = unit(1, "cm"),location="bl",pad_x = unit(0.25, "cm"), pad_y = unit(0.5, "cm")) + annotation_scale(height = unit(0.15, "cm"))+ #coord Toulouse coord_sf(xlim = c(1.39, 1.495758), ylim = c(43.55, 43.65)) + theme(axis.text.x = element_blank(), axis.text.y = element_blank(), axis.ticks = element_blank(), axis.line = element_blank(), panel.grid=element_blank(), panel.background = element_rect(fill = "floralwhite"), legend.position = "right") # Print the plot street_plot
解决方案
问题出在直接给geom_sf的color和fill参数赋值手动生成的颜色向量,且设置了inherit.aes = FALSE,导致ggplot无法识别颜色与F0变量的映射关系,无法生成梯度图例。
修改步骤:
- 不再手动生成颜色向量,而是在
geom_sf的aes中将fill和color映射到F0变量 - 使用
scale_fill_gradientn和scale_color_gradientn指定颜色渐变范围 - 移除
inherit.aes = FALSE,保留映射关系
完整修改后的代码:
library(osmdata) library(sf) library(dplyr) library(ggplot2) toulouse <- opq("Toulouse") # routes toulouse_street1 <- toulouse %>% add_osm_feature(key = "highway", value = c("motorway","motorway_link")) %>% osmdata_sf() # water toulouse_water1<- toulouse %>% add_osm_feature(key = "natural", value = c("water")) %>% osmdata_sf() toulouse_water1$osm_polygons <- toulouse_water1$osm_polygons %>% filter(natural == "water") all_toulouse_water <- st_union(toulouse_water1$osm_polygons,toulouse_water1$osm_multipolygons) # train et métro toulouse_railway1 <- toulouse %>% add_osm_feature(key = "public_transport", value=c("station")) %>% osmdata_sf() toulouse_railway2 <- toulouse %>% add_osm_feature(key = "railway",value=c("rail")) %>% osmdata_sf() # loadings library(autoimage) shp <- st_read(dsn = '/Users', layer = 'factor_loadings_with_geometry_T2') coord_new <- st_transform(shp$geometry, crs=4326) F0 <- shp$F5 # F0取值0到1 mycol <- colorRampPalette(c("white","bisque","indianred1","red3","red4","grey20")) # print the plot street_plot <- ggplot() + geom_sf(data = toulouse_railway2$osm_lines, inherit.aes = FALSE, size = 0.2, colour = "navajowhite3")+ geom_sf(data = toulouse_railway2$osm_lines, inherit.aes = FALSE, size = 0.2, colour = "grey35", linetype="dotted")+ geom_sf(data = toulouse_street1$osm_lines, inherit.aes = FALSE, color = "grey40", size = 1) + geom_sf(data = toulouse_water1$osm_multipolygons, inherit.aes = FALSE, fill="skyblue1", alpha = .8, lwd=0) + # 修改自行车站点的颜色映射 geom_sf(data = coord_new, aes(fill = F0, color = F0), shape = 21, size = 1.4 ) + # 添加颜色渐变图例 scale_fill_gradientn(colors = mycol(11), name = "F0值") + scale_color_gradientn(colors = mycol(11), name = "F0值") + annotation_north_arrow(which_north = "true", height = unit(1, "cm"), width = unit(1, "cm"),location="bl",pad_x = unit(0.25, "cm"), pad_y = unit(0.5, "cm")) + annotation_scale(height = unit(0.15, "cm"))+ coord_sf(xlim = c(1.39, 1.495758), ylim = c(43.55, 43.65)) + theme(axis.text.x = element_blank(), axis.text.y = element_blank(), axis.ticks = element_blank(), axis.line = element_blank(), panel.grid=element_blank(), panel.background = element_rect(fill = "floralwhite"), legend.position = "right") street_plot
说明:
- 通过
aes(fill = F0, color = F0)将颜色与F0变量绑定,让ggplot识别映射关系 scale_fill_gradientn和scale_color_gradientn使用自定义颜色梯度生成图例,name参数可设置图例标题- 移除冗余的手动颜色生成代码,简化逻辑
内容的提问来源于stack exchange,提问作者Bertrand Jouve
相关产品推荐
相关产品推荐

