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

R语言rayshader:单一高度矩阵多色彩纹理绘制问题求助

解决方案:R rayshader按人口收入分区域设置3D地图纹理

核心问题原因

你之前生成的texture_all格式错误——height_shade输出的是三维RGB数组(行×列×3,对应每个像素的红绿蓝通道),而非简单的颜色向量拼接。传入不符合要求的纹理对象会导致颜色无法正确映射。

正确实现步骤

假设你已拥有与mat_height同维度的分类矩阵income_mat(1=高收入,2=中等收入),按以下步骤操作:

  1. 定义各收入组颜色系并生成对应高度纹理

    # 自定义高收入蓝色系、中等收入绿色系
    high_income_colors <- c("#002266", "#0055aa", "#3388dd")
    mid_income_colors <- c("#004422", "#008844", "#33bb66")
    
    # 分别生成两个群体的高度着色纹理(三维RGB数组)
    texture_high <- height_shade(mat_height, 
                                 texture = grDevices::colorRampPalette(high_income_colors, bias = 3.5)(256))
    texture_mid <- height_shade(mat_height, 
                                texture = grDevices::colorRampPalette(mid_income_colors, bias = 3.5)(256))
    
  2. 按分类矩阵合并纹理

    # 初始化最终纹理为高收入纹理,再替换中等收入区域的像素
    texture_all <- texture_high
    texture_all[,,][income_mat == 2] <- texture_mid[,,][income_mat == 2]
    
  3. 绘制带分类纹理的3D地图

    mat_height |> 
      plot_3d(heightmap = mat_height,
              texture = texture_all,
              zscale = 0.5,
              solid = FALSE,
              shadowdepth = 0)
    

额外提示

  • 确保income_mat的行列数与mat_height完全一致,否则替换时会出现维度不匹配错误。
  • 若需添加阴影效果,可在plot_3d前叠加ray_shade或ambient_shade,示例:
    texture_all |> 
      add_shadow(ray_shade(mat_height, zscale=0.5), 0.3) |> 
      plot_3d(heightmap = mat_height, zscale=0.5, solid=FALSE, shadowdepth=0)
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 05:12:46