如何使用R在图表中将相同的色阶应用于2个地图

时间:2017-01-19 00:28:54

标签: r leaflet plotly choropleth

我有一个数据帧,我希望从中生成两个美国州地图,理想情况是在情节上,但具有相同的色阶。例如,值1.6将在两个地图上具有相同的颜色

library(plotly)
library(tidyverse)

df <- structure(list(stateID = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 
20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 
33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 
46L, 47L, 48L, 49L, 50L, 51L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 
22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 
35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 
48L, 49L, 50L, 51L), .Label = c("AK", "AL", "AR", "AZ", "CA", 
"CO", "CT", "DC", "DE", "FL", "GA", "HI", "IA", "ID", "IL", "IN", 
"KS", "KY", "LA", "MA", "MD", "ME", "MI", "MN", "MO", "MS", "MT", 
"NC", "ND", "NE", "NH", "NJ", "NM", "NV", "NY", "OH", "OK", "OR", 
"PA", "RI", "SC", "SD", "TN", "TX", "UT", "VA", "VT", "WA", "WI", 
"WV", "WY"), class = "factor"), category = structure(c(1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L), .Label = c("small", "large"), class = "factor"), 
values = c(1.49852796690539, 1.149561395403, 1.24566150736064, 
1.00189715065062, 0.987562097609043, 0.231859257910401, 1.64033870631829, 
0.12613168777898, 1.91596280829981, 1.35835290746763, 0.792609711177647, 
1.97339694341645, 0.376533678267151, 1.63516522897407, 0.225585919804871, 
0.505839260760695, 0.0116439824923873, 1.51879569422454, 
1.35506999026984, 0.907982874661684, 1.08905055327341, 1.43553646048531, 
1.88463490083814, 0.908853257540613, 0.902149567846209, 1.5428032441996, 
1.62871437706053, 0.623223986010998, 1.38058050535619, 1.57016781903803, 
1.8731079204008, 0.297486190218478, 1.27625703299418, 0.519723014440387, 
0.848097313195467, 0.471342021599412, 1.98357644258067, 1.82963251601905, 
0.626917572226375, 0.140670910011977, 1.1393640646711, 1.99097026558593, 
1.70624398859218, 0.0956417261622846, 1.53923089429736, 1.25705669261515, 
1.89643088867888, 1.88176721381024, 1.44151636445895, 0.435520166531205, 
1.18845809064806, 2.95685278996825, 1.37898187059909, 2.95418810285628, 
1.94776474731043, 1.98949646670371, 1.37768995529041, 2.0124197602272, 
1.33321205340326, 2.07528439210728, 2.49668326042593, 2.69146995106712, 
1.52930808160454, 1.90695044351742, 1.74849874572828, 2.3098370959051, 
1.59070889744908, 1.52278333622962, 2.13915946660563, 1.52873482694849, 
1.39334590546787, 1.82181124016643, 2.44108150294051, 2.21637984598055, 
1.99761381838471, 2.33465715777129, 2.53672145865858, 2.98815744556487, 
1.09659902472049, 1.13088199263439, 1.91391275310889, 1.07779059326276, 
1.03311925474554, 1.25638853525743, 2.84107198892161, 2.64419505093247, 
1.85066655138507, 2.65426727430895, 2.37351916264743, 1.45171522675082, 
2.54493401898071, 1.40593391284347, 1.82211668742821, 1.36818132549524, 
2.88858095230535, 2.54271147632971, 2.90091867512092, 1.5358378039673, 
2.86527143837884, 2.71315307915211, 2.09380666306242, 2.02881665108725
)), .Names = c("stateID", "category", "values"), row.names = c(NA, 
102L), class = "data.frame")

small <-df %>% 
filter(category=="small") %>% 
plot_geo(locationmode = 'USA-states') %>% 
add_trace(
z = ~values, color = ~values, colors = "Blues",
 locations = ~stateID) %>% 
 layout(geo=list(scope='usa'))
small

large <-df %>% 
filter(category=="large") %>% 
plot_geo(locationmode = 'USA-states') %>% 
add_trace(
z = ~values, color = ~values, colors = "Blues",
locations = ~stateID) %>% 
layout(geo=list(scope='usa'))
large

small values

enter image description here

如您所见,相同的色标涵盖了每个地图中的不同值。我想要一个0-3范围的单一色标。我不是挑剔的颜色虽然想要一个顺序选项

TIA

1 个答案:

答案 0 :(得分:1)

通过将zminzmax设置为每个图中的相同值,您可以为每个地图设置相同的比例。它是documented in the help,但它有点神秘:&#34; zmax(数字):设置颜色域的上限&#34; (同样适用于zmin)。

small <-df %>% 
  filter(category=="small") %>% 
  plot_geo(locationmode = 'USA-states') %>% 
  add_trace(
    z = ~values, color = ~values, colors = "Blues",
    zmin=0, zmax=3, 
    locations = ~stateID) %>% 
  layout(geo=list(scope='usa'), title="Small")

large <-df %>% 
  filter(category=="large") %>% 
  plot_geo(locationmode = 'USA-states') %>% 
  add_trace(
    z = ~values, color = ~values, colors = "Blues",
    zmin=0, zmax=3,
    locations = ~stateID) %>% 
  layout(geo=list(scope='usa'), title="Large")

enter image description here

enter image description here