You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言巴西地图数据可视化:调整颜色范围与图例配置

调整巴西地图数据可视化的颜色范围与配色方案

没问题,我帮你调整代码来实现你想要的效果!核心是要手动固定颜色映射的范围为-1到1,而不是依赖数据本身的极值,同时确保负值显示红色、正值显示蓝色的逻辑正确。以下是修改后的完整代码,我会在后面解释关键改动:

# Packages
library(maptools)
library(descr)
library(RColorBrewer)
library(plotrix)

# Reading shape files
estados <- readShapePoly(fn = 'estados_2010.shp')
estados2 <- readShapePoly(fn = 'estados_2010.shp')

# Adding an order vector in the data frame
estados@data$order <- 1:nrow(estados@data)

# Data to fit in the map
teste <- c(0.567072774466371, -0.144934811865636, 0.595967865326944, 
           0.479904421295722, 0.251495518688696, 0.195815073242536, 
           0.548223329774688, 0.463845521774005, 0.272145251828454, 
           0.580334048486482, 0.649672015127772, 0.584054575484519, 
           0.671373957229477, 0.436391947145788, 0.522605768431274, 
           0.641067907529059, 0.570339843565797, 0.381297082036819, 
           0.474587396032187, 0.462810571821925, 0.558684078070584, 
           -0.197881941278306, 0.720551728476599, 0.489189630257170, 
           -0.538560893510226, 0.732280520987057, 0.555364159479124)

# Adding the data vector in the data frame
estados@data$dados <- teste

# -------------------------- 关键修改部分 --------------------------
# 定义红-蓝配色板(RdBu正好是红到蓝,符合负值红、正值蓝的需求)
myPalette <- brewer.pal(9, 'RdBu')
# 创建颜色渐变映射函数,将[-1,1]的数值转换为对应的颜色
colorMapper <- colorRamp(myPalette)

# 设置颜色分档数(保持和原代码一致的9档)
numberColors <- 9
# 手动设置breaks为从-1到1的等距序列,而不是基于数据分位数
cuts <- seq(-1, 1, length.out = numberColors + 1)

# 将数据dados映射到[0,1]区间(对应颜色映射的输入范围)
scaled_dados <- (estados@data$dados - (-1)) / (1 - (-1))
# 确保数值不超出0-1范围(防止数据极值超出-1/1时出错)
scaled_dados <- pmax(pmin(scaled_dados, 1), 0)
# 生成每个数据点对应的颜色
myColors <- rgb(colorMapper(scaled_dados), maxColorValue = 255)

# 将颜色与数据绑定(简化原代码的merge逻辑)
estados@data$colors <- myColors
# -----------------------------------------------------------------

# Ordering the data frame back to the first order
estados@data <- estados@data[order(estados@data$order),]

# Plot
plot(estados, col=as.character(estados@data$colors), lty=0)
plot(estados2, add=T) # 绘制州边界线

# Legend - 更新为-1到1的范围
legend_labels <- c('-1.0', '-0.5', '0', '0.5', '1.0')
# 生成图例对应的颜色
legend_colors <- rgb(colorMapper(seq(0,1,length.out=length(legend_labels))), maxColorValue=255)
color.legend(xl=-70, xr=-60, yb=-25, yt=-26, 
             legend=legend_labels, rect.col=legend_colors, 
             gradient='x', cex=.8, pos=c(1,1,1))

关键改动说明:

  1. 固定颜色映射范围:不再用数据的分位数生成cuts,而是手动设置seq(-1, 1, length.out = numberColors + 1),确保颜色的极值对应-1和1,你的数据极值(-0.53/0.73)会落在中间偏深的位置,但不会达到最深色。
  2. 颜色映射逻辑:用colorRamp创建从[-1,1]到RdBu调色板的映射,先把你的数据缩放到0-1区间(对应-1到1的相对位置),再生成对应颜色,确保负值偏红、正值偏蓝的过渡自然。
  3. 简化颜色绑定:直接给每个数据点分配对应的颜色,省去了原代码中cut和merge的复杂逻辑,更直观。
  4. 更新图例:图例标签改为-1到1的刻度,颜色也对应新的映射范围,让读者能准确理解颜色对应的数值。

这样修改后,你的地图就会按照预期显示:现有数据的极值颜色更浅,-1和1对应最深的红/蓝,负值红、正值蓝。

内容的提问来源于stack exchange,提问作者user9846514

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:51:17