如何使用色彩空间包控制色阶的限制?

问题描述 投票:0回答:1

拿一个像这样的

sf
物体。

df <- structure(list(margin = c(26.2569832402235, 26.775956284153, 
                                20.7317073170732, -4.21052631578947, 10.9876543209877, 13.6094674556213, 
                                23.4567901234568, -13.3333333333333), geometry = structure(list(
                                  structure(list(structure(c(-88.121832, -88.157869, -88.176234, 
                                                             -88.177947, -88.177989, -88.059246, -88.0582574, -88.094754, 
                                                             -88.105617, -88.136212, -88.121832, 45.831761, 45.844797, 
                                                             45.844652, 45.804926, 45.71809, 45.713063, 45.7806288, 45.785755, 
                                                             45.800389, 45.818735, 45.831761), dim = c(11L, 2L))), 
                                            class = c("XY", "POLYGON", "sfg")), structure(list(structure(c(-88.176234, -88.179036, -88.303528, -88.303565, -88.303894, -88.177947, -88.176234, 45.844652, 45.905805, 45.908553, 45.894106, 45.807592, 45.804926, 45.844652), dim = c(7L, 2L))), 
                                                                                          class = c("XY", "POLYGON", "sfg")), structure(list(structure(c(-88.365253, -88.36627, -88.425482, -88.551359, -88.675785, -88.67598, -88.365253, 45.722149, 45.808698, 45.809419, 45.809857, 45.808946, 45.722835, 45.722149),
                                                                                                                                                       dim = c(7L, 2L))), class = c("XY", "POLYGON", "sfg")), 
                                  structure(list(structure(c(-88.303894,-88.303565, -88.425221, -88.425482, -88.36627, -88.303894, 45.807592, 45.894106, 45.895528, 45.809419, 45.808698, 45.807592),
                                                           dim = c(6L, 2L))), class = c("XY", "POLYGON", "sfg")), 
                                  structure(list(structure(c(-88.121832, -88.105196, -88.070483, 
                                                             -88.101757, -88.102461, -88.126654, -88.148128, -88.180498, 
                                                             -88.230193, -88.25498, -88.268944, -88.301378, -88.328041, 
                                                             -88.380891, -88.388957, -88.423493, -88.464731, -88.48732, 
                                                             -88.497978, -88.50748, -88.534173, -88.55937, -88.551383, 
                                                             -88.550958, -88.553837, -88.551359, -88.425482, -88.425221, 
                                                             -88.303565, -88.303528, -88.179036, -88.176234, -88.157869, 
                                                             -88.121832, 45.831761, 45.842413, 45.874329, 45.883192, 45.921546, 
                                                             45.921791, 45.937546, 45.948287, 45.947251, 45.963512, 45.956413, 
                                                             45.956459, 45.963539, 45.991813, 45.982515, 45.982072, 46.000738, 
                                                             45.991024, 45.995794, 46.018516, 46.020269, 46.014102, 45.982055, 
                                                             45.896127, 45.883736, 45.809857, 45.809419, 45.895528, 45.894106, 
                                                             45.908553, 45.905805, 45.844652, 45.844797, 45.831761), dim = c(34L, 2L))),
                                            class = c("XY", "POLYGON", "sfg")), structure(list(structure(c(-88.177989, -88.177947, -88.303894, -88.36627, -88.365253, -88.177989, 45.71809, 45.804926, 45.807592, 45.808698, 45.722149, 45.71809), dim = c(6L, 2L))),
                                                                                          class = c("XY", "POLYGON", "sfg")), structure(list(structure(c(-88.551359, -88.553837, -88.550958, -88.675489, -88.675785, -88.551359, 45.809857, 45.883736, 45.896127, 45.895958, 45.808946, 45.809857), 
                                                                                                                                                       dim = c(6L, 2L))), class = c("XY", "POLYGON", "sfg")), 
                                  structure(list(structure(c(-88.55937, -88.58827, -88.616471, 
                                                             -88.658654, -88.67445, -88.675489, -88.550958, -88.551383, 
                                                             -88.55937, 46.014102, 46.005098, 45.987732, 45.98929, 45.980868, 
                                                             45.895958, 45.896127, 45.982055, 46.014102), dim = c(9L, 2L))),
                                            class = c("XY", "POLYGON", "sfg"))), n_empty = 0L, crs = structure(list(input = "NAD83", wkt = "GEOGCRS[\"NAD83\",\n    DATUM[\"North American Datum 1983\",\n        ELLIPSOID[\"GRS 1980\",6378137,298.257222101,\n            LENGTHUNIT[\"metre\",1]]],\n    PRIMEM[\"Greenwich\",0,\n        ANGLEUNIT[\"degree\",0.0174532925199433]],\n    CS[ellipsoidal,2],\n        AXIS[\"geodetic latitude (Lat)\",north,\n            ORDER[1],\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n        AXIS[\"geodetic longitude (Lon)\",east,\n            ORDER[2],\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n    ID[\"EPSG\",4269]]"), class = "crs"), class = c("sfc_POLYGON", "sfc"), 
                                  precision = 0, bbox = structure(c(xmin = -88.67598, ymin = 45.713063, xmax = -88.0582574, ymax = 46.020269), class = "bbox"))), row.names = c(NA, 8L),
                sf_column = "geometry", agr = structure(c(margin = NA_integer_), levels = c("constant", "aggregate", "identity"), class = "factor"), class = c("sf", "tbl_df", "tbl", "data.frame"))

> df
Simple feature collection with 8 features and 1 field
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: -88.67598 ymin: 45.71306 xmax: -88.05826 ymax: 46.02027
Geodetic CRS:  NAD83
# A tibble: 8 × 2
  margin                                                                                     geometry
*  <dbl>                                                                                <POLYGON [°]>
1  26.3  ((-88.12183 45.83176, -88.15787 45.8448, -88.17623 45.84465, -88.17795 45.80493, -88.1779...
2  26.8  ((-88.17623 45.84465, -88.17904 45.90581, -88.30353 45.90855, -88.30357 45.89411, -88.303...
3  20.7  ((-88.36525 45.72215, -88.36627 45.8087, -88.42548 45.80942, -88.55136 45.80986, -88.6757...
4  -4.21 ((-88.30389 45.80759, -88.30357 45.89411, -88.42522 45.89553, -88.42548 45.80942, -88.366...
5  11.0  ((-88.12183 45.83176, -88.1052 45.84241, -88.07048 45.87433, -88.10176 45.88319, -88.1024...
6  13.6  ((-88.17799 45.71809, -88.17795 45.80493, -88.30389 45.80759, -88.36627 45.8087, -88.3652...
7  23.5  ((-88.55136 45.80986, -88.55384 45.88374, -88.55096 45.89613, -88.67549 45.89596, -88.675...
8 -13.3  ((-88.55937 46.0141, -88.58827 46.0051, -88.61647 45.98773, -88.65865 45.98929, -88.67445...

我想将

margin
列映射到红蓝发散调色板,中点设置为 0。我正在使用 {colorspace} 包。

library(ggplot2)
library(colorspace)

ggplot(df) +
  geom_sf(aes(fill = margin)) +
  scale_fill_continuous_divergingx(palette = "RdBu", mid = 0)

enter image description here

现在,调色板的限制是由提供的数据的限制设置的。因此,最大正值 26.8 被赋予最深的蓝色阴影。

我想更改调色板,以便将 最深的蓝色阴影指定为 100 的值(最深的红色阴影指定为 -100 的值)。在此版本的地图中,所有颜色均为浅红色和浅蓝色。

理想情况下,ggplot 图例仍将仅显示该图中包含的数据范围,就像目前一样。

r ggplot2 color-space
1个回答
0
投票

我想你想要

limits=
参数:

ggplot(df) +
  geom_sf(aes(fill = margin)) +
  scale_fill_continuous_divergingx(palette = "RdBu", mid = 0, limits = c(-100, 100))
© www.soinside.com 2019 - 2024. All rights reserved.