如何使用更多的色盲安全合成而不是rgb(红色,绿色,蓝色)来可视化Map?

63lcw9qa  于 2023-11-14  发布在  其他
关注(0)|答案(1)|浏览(133)

这个问题是在将answer升级到question之后出现的。
基本上,我有一个dataframe(请参阅下面的可重复数据),其中的列我想用rgb颜色合成来可视化(下面的代码是从answer复制粘贴的):

library(terra)

# convert df to a rast
r <-  rast(df, crs = "WGS84")

# Just use geom_spatraster_rgb

library(tidyterra)
library(ggplot2)

ggplot() +
  geom_spatraster_rgb(data = r, 
                      # Specify the channels and the max value
                      r =1, g = 2, b = 3, max_col_value = 1)

字符串
x1c 0d1x的数据
我怎样才能得到一个比红色、绿色和蓝色更安全的色盲调色板?我还想得到一个图例,其中显示哪个参数对应更多的红色/绿色/蓝色(例如使用三元图)?

可重现数据:

df <- structure(list(x = c(-74.5, -74.5, -74.5, -73.5, -73.5, -73.5, 
-72.5, -72.5, -71.5, -71.5, -71.5, -71.5, -71.5, -71.5, -71.5, 
-70.5, -70.5, -70.5, -70.5, -70.5, -70.5, -70.5, -70.5, -70.5, 
-70.5, -70.5, -70.5, -70.5, -69.5, -69.5, -69.5, -69.5, -69.5, 
-69.5, -69.5, -69.5, -69.5, -69.5, -69.5, -69.5, -69.5, -69.5, 
-69.5, -68.5, -68.5, -68.5, -68.5, -68.5, -68.5, -68.5, -68.5, 
-68.5, -68.5, -68.5, -67.5, -67.5, -67.5, -67.5, -67.5, -67.5, 
-67.5, -67.5, -67.5, -67.5, -66.5, -66.5, -66.5, -66.5, -66.5, 
-66.5, -65.5, -65.5, -65.5, -65.5, -64.5, -63.5, 145.5, 146.5, 
146.5, 147.5, 147.5, 169.5, 170.5, 170.5, 172.5, 172.5, 173.5, 
173.5, 175.5), y = c(-50.5, -49.5, -46.5, -45.5, -41.5, -40.5, 
-51.5, -40.5, -52.5, -51.5, -50.5, -49.5, -48.5, -45.5, -44.5, 
-52.5, -51.5, -50.5, -49.5, -48.5, -47.5, -46.5, -45.5, -44.5, 
-43.5, -42.5, -41.5, -40.5, -55.5, -53.5, -52.5, -51.5, -50.5, 
-49.5, -48.5, -47.5, -46.5, -45.5, -44.5, -43.5, -42.5, -41.5, 
-40.5, -53.5, -49.5, -48.5, -47.5, -46.5, -45.5, -44.5, -43.5, 
-42.5, -41.5, -40.5, -54.5, -48.5, -47.5, -46.5, -45.5, -44.5, 
-43.5, -42.5, -41.5, -40.5, -47.5, -44.5, -43.5, -42.5, -41.5, 
-40.5, -43.5, -42.5, -41.5, -40.5, -40.5, -40.5, -41.5, -42.5, 
-41.5, -42.5, -41.5, -45.5, -45.5, -44.5, -43.5, -41.5, -42.5, 
-41.5, -40.5), a = c(0.00960646095846934, 0.0107211537770858, 
0.0021450412691773, 0.0045505840420013, 0, 0, 0.0178641663901207, 
0.000114664688300056, 0.00307793920859317, 0.0397662643009339, 
0.0604302099149969, 0.0489996355067338, 0.0761637028942619, 0.0721391994373738, 
0.0286955884419135, 0.0132820025262969, 0.0239635116965841, 0.0321472332160114, 
0.0297445886688907, 0.0286537513835504, 0.0350160311966702, 0.0266880069207789, 
0.0246363370053615, 0.0196110963394959, 0.0192277974257839, 0.00443219128797979, 
0.00905912420084446, 0.00108861789789237, 0.00545657352102614, 
0.00277087980897986, 0.0048581585653791, 0.0111483654900654, 
0.0173443447511208, 0.0191704395787758, 0.0287344700495196, 0.035562350382349, 
0.0162432464057994, 0.0152193319873854, 0.0233689119154181, 0.015818007952541, 
0.0138323743513157, 0.00995716868729754, 0.0060245918334208, 
0.00126566408878553, 0.0121859145176333, 0.0160686675462497, 
0.0199868442198467, 0.00571838395930305, 0.00899896782857034, 
0.0122839878433772, 0.00812199685920614, 0.0236680200661936, 
0.0164528502963111, 0.00401833465388731, 0.0252041777182223, 
0.00474040271660494, 0.00529145334170783, 0.00143754732859506, 
0.00306248020407983, 0.0047192270320121, 0.00284510865796966, 
0.00654780874247308, 0.00200161475298119, 0.000983630507466302, 
0.00414849014194222, 0.00228390751366616, 0.00101576169552506, 
0.00054963121693693, 0.00063555535523403, 0, 0.00042539225277756, 
6.45333309886086e-05, 9.4029799969462e-05, 0, 0, 7.14516962134147e-06, 
0, 0, 0, 0, 0, 0.00957735912739627, 0.000458637950700158, 0.00184617106824235, 
0, 0.000642432395196541, 0.00039151279290547, 0.000341094431495925, 
0), b = c(0.726214797184461, 0.71164224353343, 0.451275516898288, 
0.394425878075392, 0.739653175101045, 0.789679333377203, 0.54513343050909, 
0.748096411499116, 0.637758148362954, 0.804483227899012, 0.815882186402205, 
0.845419576781181, 0.785035127263261, 0.650754524321527, 0.565620243042535, 
0.747242358064224, 0.901570593192835, 0.876864014581682, 0.875748761130915, 
0.875441727867402, 0.850544308509126, 0.875610142568506, 0.778353470331944, 
0.66290601970565, 0.643516154686927, 0.652461173409377, 0.641005740998037, 
0.673939925204614, 0.579626516309421, 0.745095570147862, 0.875498117933161, 
0.90645794278283, 0.900246890214777, 0.917139905866649, 0.88258894208252, 
0.875682104646359, 0.904559344914523, 0.869277878116666, 0.82815980916799, 
0.789286961613949, 0.793714463229649, 0.798340899287069, 0.795265039070927, 
0.809122812256599, 0.936535149104964, 0.913385909113815, 0.913860352861773, 
0.942470665576281, 0.914731915186546, 0.910895562157346, 0.908557068434931, 
0.890082095057589, 0.898121177345363, 0.90117499876107, 0.743740425652289, 
0.947013840601633, 0.951526480785598, 0.925388777233251, 0.939990726262484, 
0.925945462742375, 0.915508745124089, 0.916192056973718, 0.926008092761408, 
0.912451960219619, 0.954478263060087, 0.949854335448342, 0.923279988118492, 
0.922573791351208, 0.935874036776427, 0.926298552448805, 0.948494363610902, 
0.937484856878812, 0.929022357932769, 0.939701487351031, 0.923342876011735, 
0.96264144140606, 0.827523195314497, 0.762759732100396, 0.872746242783302, 
0.919113271004337, 0.943498218180097, 0.672071125498697, 0.853102387466382, 
0.595971968865642, 0.805944307217466, 0.73953470776106, 0.680611436206279, 
0.72312882725833, 0.777090811680429), c = c(0.26417874185707, 
0.277636602689484, 0.546579441832535, 0.601023537882607, 0.260346824898955, 
0.210320666622797, 0.437002403100789, 0.251788923812584, 0.359163912428452, 
0.155750507800054, 0.123687603682798, 0.105580787712085, 0.138801169842477, 
0.277106276241099, 0.405684168515551, 0.23947563940948, 0.0744658951105806, 
0.090988752202307, 0.094506650200194, 0.0959045207490474, 0.114439660294203, 
0.0977018505107156, 0.197010192662695, 0.317482883954854, 0.337256047887289, 
0.343106635302644, 0.349935134801119, 0.324971456897494, 0.414916910169553, 
0.252133550043159, 0.11964372350146, 0.0823936917271048, 0.0824087650341023, 
0.0636896545545757, 0.0886765878679606, 0.0887555449712918, 0.0791974086796774, 
0.115502789895948, 0.148471278916592, 0.19489503043351, 0.192453162419035, 
0.191701932025634, 0.198710369095652, 0.189611523654616, 0.0512789363774025, 
0.0705454233399353, 0.0661528029183804, 0.0518109504644155, 0.0762691169848838, 
0.0768204499992763, 0.0833209347058624, 0.0862498848762179, 0.0854259723583258, 
0.0948066665850425, 0.231055396629489, 0.0482457566817616, 0.0431820658726939, 
0.073173675438154, 0.0569467935334359, 0.0693353102256132, 0.0816461462179411, 
0.0772601342838093, 0.0719902924856103, 0.0865644092729144, 0.0413732467979708, 
0.0478617570379917, 0.0757042501859831, 0.0768765774318549, 0.0634904078683389, 
0.073701447551195, 0.0510802441363202, 0.0624506097901988, 0.0708836122672612, 
0.0602985126489692, 0.0766571239882651, 0.0373514134243189, 0.172476804685503, 
0.237240267899604, 0.127253757216698, 0.080886728995663, 0.0565017818199034, 
0.318351515373907, 0.146438974582918, 0.402181860066115, 0.194055692782534, 
0.259822859843743, 0.318997051000816, 0.276530078310174, 0.222909188319571
)), row.names = c(NA, -89L), class = "data.frame")

muk1a3rh

muk1a3rh1#

如果我理解得很好,你需要用新的颜色替换数据集中已经定义的颜色,并将这些值显示为图例(例如某些栅格的例子Corine Land Cover,即RGB,但每种颜色都是一个类别)。
这里的问题是,首先我们需要对颜色进行一些额外的分类工作,因为您的df包括83个不同的值。

# Data import ommited for brevity
# df <- structure(list(x = c(
#   -74.5, -74.5, -74.5, -73.5, -73.5, -73.5, ...
# )

# Create the raster
library(terra)
#> terra 1.7.55
library(tidyterra)
r <- rast(df, crs = "WGS84")

字符串
首先,我们需要识别栅格中的颜色

r_as_df <- as_tibble(r)

# Mark NAs as 0, (black) for proper handling
r_as_df[is.na(r_as_df)] <- 0

cols <- rgb(r_as_df)
length(unique(cols))
#> [1] 84


这就给出了84种不同的颜色(包括我强制使用的black),其中大部分颜色非常相似。

# remotes::install_github("jmw86069/colorjam")
namedcols <- colorjam::closestRcolor(cols)
unique(namedcols)
#> [1] "black"          "limegreen"      "green3"         "mediumseagreen"
#> [5] "green2"         "seagreen"       "steelblue4"     "skyblue4"      
#> [9] "aquamarine4"

scales::show_col(unique(namedcols))


x1c 0d1x的数据
好了,现在我们有8加黑色,这是一个更容易接受的数字来创建类别。下一步是使用因子将每个不同的颜色分类为一个类别(我在这里使用的只是字母,但可以是任何东西,只要它是一个字符串):

# Replace black placeholder
cols[namedcols == "black"] <- NA
namedcols[namedcols == "black"] <- NA

# Relabel each reduced color
categ <- factor(namedcols, labels = LETTERS[seq_len(length(unique(namedcols)) - 1)])

# And now back to the raster as new layers
r$cols <- as.factor(cols)
r$namedcols <- as.factor(namedcols)
r$categ <- categ

r

#> class       : SpatRaster 
#> dimensions  : 16, 251, 6  (nrow, ncol, nlyr)
#> resolution  : 1, 1  (x, y)
#> extent      : -75, 176, -56, -40  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326) 
#> source(s)   : memory
#> names       :         a,         b,          c,    cols,   namedcols, categ 
#> min values  : 0.0000000, 0.3944259, 0.03735141, #009867, aquamarine4,     A 
#> max values  : 0.0761637, 0.9626414, 0.60102354, #13C823,  steelblue4,     H

现在是情节!

首先,我想通过使用默认颜色和减少的调色板来检查操作是否正确:

library(tidyterra)
library(ggplot2)

# Original colors, you can use also this approach instead of geom_spatraster_rgb
ggplot() +
  geom_spatraster(data = r, aes(fill = cols)) +
  scale_fill_identity()

# Modified colors
ggplot() +
  geom_spatraster(data = r, aes(fill = cols)) +
  scale_fill_identity()

最后的答案

只需使用geom_spatraster(data = r, aes(fill = categ)),其中categ是分配给每种颜色的类别。首先,没有安全盲调色板(这将回答图例部分):

# Classified colors with legend
ggplot() +
  geom_spatraster(data = r, aes(fill = categ))

对于色盲(我自己,我是轻微色盲),我会推荐colorspace包或rcartocolor包。我在这里使用rcartocolor的例子:

# And use a color-blind friendly palette
library(rcartocolor)
#> Warning: package 'rcartocolor' was built under R version 4.3.2
display_carto_all(colorblind_friendly = TRUE)

ggplot() +
  geom_spatraster(data = r, aes(fill = categ), na.rm = TRUE) +
  scale_fill_carto_d(palette = "Safe", na.translate = FALSE, name = "Category")

创建于2023-11-08带有reprex v2.0.2

相关问题