最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - How to assign pre-determined RGB values to polygons - Stack Overflow

programmeradmin1浏览0评论

I have a map defined by polygons in a shape file that I have read into R. Each polygon has a character value assigned to it in a column called "UNIT_250K", and each of those character values has it's own RGB values provided in 3 columns called "RED", "GREEN", and "BLUE". I need to color the polygons using the RGB values but my attempts to do so aren't working.

This is what I tried:

tm_shape(YGS_GLGY) +
  tm_polygons(fill = rgb(red = "RED", green = "GREEN", blue = "BLUE")) +
  tm_borders()

I have a map defined by polygons in a shape file that I have read into R. Each polygon has a character value assigned to it in a column called "UNIT_250K", and each of those character values has it's own RGB values provided in 3 columns called "RED", "GREEN", and "BLUE". I need to color the polygons using the RGB values but my attempts to do so aren't working.

This is what I tried:

tm_shape(YGS_GLGY) +
  tm_polygons(fill = rgb(red = "RED", green = "GREEN", blue = "BLUE")) +
  tm_borders()
Share Improve this question edited Mar 6 at 20:06 Kirsten Rasmussen asked Mar 6 at 18:48 Kirsten RasmussenKirsten Rasmussen 33 bronze badges 1
  • Please edit the question to include language tag (possibly "R") and don't add greetings. – Alexei Levenkov Commented Mar 6 at 18:52
Add a comment  | 

2 Answers 2

Reset to default 0

You can create a new column with your RGB values converted to their corresponding hex values and use that column to fill your polygons. As you did not provide sample data, this reprex uses the built-in nc dataset from the sf package, and assumes your RGB values are scaled from 0-1. If not, you need to scale them first by dividing by 255.

library(tmap)
library(sf)
library(dplyr)

# Sample data with scaled (0 - 1) RGB values
set.seed(42)
YGS_GLGY <- st_read(system.file("shape/nc.shp", package = "sf")) |>
  mutate(RED = sample(0:255, n()) / 255,
         GREEN = sample(0:255, n()) / 255,
         BLUE = sample(0:255, n()) / 255)

# Generate hex values in a new column
YGS_GLGY <- YGS_GLGY |>
  mutate(fill = rgb(RED, GREEN, BLUE))

# Plot
tm_shape(YGS_GLGY) +
  tm_polygons(fill = "fill") +
  tm_borders()

Thank you L Tyrone, that was very helpful! I modified your example to something that was easier for me, a total beginner, to understand and was successful in making my map.

This is what I used:

#load libraries
library(sf)
library(tmap)
library(dplyr)

#read in shape file
YGS_GLGY <- st_read("Yukon_Bedrock_Geology_Complete.shp/Bedrock_Geology.shp/Bedrock_Geology.shp")

#Colour the polygons using the RGB values provided under the columns RED, GREEN, BLUE.

#the RGB colours need to be scaled from (0 - 255) to (0 - 1)
YGS_GLGY <- YGS_GLGY |>
  mutate(R = RED / 255,
         G = GREEN / 255,
         B = BLUE / 255)

#then, they need to be converted to one column of hex values
YGS_GLGY <- YGS_GLGY |>
  mutate(HEX = rgb(R, G, B))

#then the coloured polygons can be plotted as a map
tm_shape(YGS_GLGY) +
  tm_polygons(fill = "HEX") +
  tm_borders() +
  tm_scalebar(position = c(0.01, 0.035)) +
  tm_compass(size = 1.5, position = c(0.05, 0.95))
发布评论

评论列表(0)

  1. 暂无评论