R语言 移动位于AK和HI的河流以匹配plot_usmap

6vl6ewon  于 2023-03-27  发布在  其他
关注(0)|答案(1)|浏览(115)

我用usmap包中的plot_usmap做了一个可爱的chloropleth:

plot_usmap(data = state_difference, values = "my_variable") +
    theme(legend.position = "bottom") +
    scale_fill_stepsn(breaks = as.vector(quantile(state_difference$median_premium, probs = seq(0, 1, 1/5))), colors = heat.colors(5)) +
    labs(title = "Graph title")

然后有人说,“你知道吗?我们应该把地形特征放在这张Map上。”所以我找到了一个很好的定义河流的shapefile:

rivers <- st_read("sources/river_files")

## Match projection to that of 'plot_usmap' (ESRI:102003):
rivers2 <- sf::st_transform(rivers, "ESRI:102003")

这是 * 如此接近 *:

plot_usmap(data = state_difference, values = "my_variable") +
    theme(legend.position = "bottom") +
    scale_fill_stepsn(breaks = as.vector(quantile(state_difference$median_premium, probs = seq(0, 1, 1/5))), colors = heat.colors(5)) +
    labs(title="Graph title") +
    geom_sf(rivers2)

问题是plot_usmap将阿拉斯加和夏威夷置于美国大陆南部,而shapefile将它们置于其实际位置。因此,我需要将阿拉斯加和夏威夷的河流移至美国大陆南部以匹配plot_usmap
通常的方法(参见here)涉及到标记为“Alaska”和“夏威夷”的几何图元可以进行变换,但shapefile只有河流。我尝试将shift_geometry应用于rivers2,但结果是垃圾。
样本状态数据:

structure(list(state = c("HI", "WV", "MS", "AL", "LA", "NJ"), 
    value = c(10951.893, 11088.963, 58661.391, 32610.476, 543746.029, 
    105367.232)), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

河流数据样本:

structure(list(NAMEEN = c("Kigalik River", "Maybe Creek", "Canning River", 
"Hulahula River", "Jago River"), geometry = structure(list(structure(c(-2899252.91587767, 
-2899312.64601041, -2903046.31714341, -2909538.23596611, -2911302.61092802, 
-2914750.27748601, -2918789.31605733, -2922075.39177455, -2924200.79458146, 
-2926396.01465823, -2929253.81477254, -2931621.18065852, -2933357.06098142, 
-2937854.95625042, -2938794.60865356, -2942085.2347789, -2943084.7635867, 
-2943154.3625061, -2944788.90979832, -2946543.19849677, -2948238.24538405, 
-2949678.70773815, -2955522.29616087, -2956126.13419868, -2958998.8998913, 
-2960102.11139554, -2961572.49799357, -2963256.36542367, -2964385.6207291, 
-2968334.16733643, -2970077.83291074, -2971493.12021008, -2973130.40390839, 
-2977842.69472845, -2980109.87122154, -2981192.73175109, -2982395.52402644, 
-2985001.06435783, -2987804.6996, -2990072.09038422, 4239476.95011435, 
4237821.92721547, 4237140.47059142, 4238882.29842369, 4239875.86641957, 
4242540.02891328, 4244900.97267643, 4247208.85389156, 4247048.23873586, 
4249769.38355097, 4252727.02820637, 4254621.48491297, 4255611.66104693, 
4259709.13053415, 4261319.54587873, 4263635.1126088, 4264566.25816588, 
4266268.42646858, 4267418.87482698, 4268425.14310217, 4270077.800334, 
4271567.06528524, 4278514.07466109, 4279414.13570809, 4282372.70376572, 
4283150.69760176, 4283957.52851983, 4284441.74348548, 4284534.82538005, 
4287074.78088274, 4291138.16264392, 4291432.37237433, 4292588.16580516, 
4296390.90840629, 4298450.61737576, 4299215.80365335, 4299837.67860639, 
4300729.39398182, 4301301.24570256, 4301496.30889971), dim = c(40L, 
2L), class = c("XY", "LINESTRING", "sfg")), structure(c(-2856799.26426802, 
-2856311.3774156, -2855609.51715275, -2856077.3203322, -2857126.77877474, 
-2858948.24273194, -2872393.15940038, -2874313.77029371, -2875834.36391061, 
-2877276.34536589, -2878955.96798146, -2881164.65323986, -2881924.09427179, 
-2889003.39897877, -2892897.33899493, -2896405.57923701, -2897490.38244475, 
-2898383.6108713, -2899363.17790892, -2899322.5355603, -2898044.52526552, 
-2897820.96246425, -2899252.91587767, 4210892.05791716, 4209850.57679172, 
4206768.02920257, 4202640.72257962, 4201724.19664976, 4202048.71970684, 
4210266.71218086, 4211609.76577034, 4212919.86773861, 4214384.5525767, 
4217211.37538879, 4218756.84466467, 4221166.17811783, 4228017.58153775, 
4230026.26265049, 4232028.60673767, 4232789.05489458, 4233871.09393742, 
4235979.46541856, 4236654.30348682, 4236705.67853229, 4237981.59548233, 
4239476.95011435), dim = c(23L, 2L), class = c("XY", "LINESTRING", 
"sfg")), structure(c(-2514215.55544332, -2514108.60295901, -2516474.73663247, 
-2518453.22967354, -2518908.49380877, -2517737.73005421, -2514790.73574887, 
-2515401.76286617, -2516656.81238836, -2517508.2364059, -2518533.2392692, 
-2518414.60386001, -2519493.57285708, -2521308.60450706, -2522859.54465132, 
-2522782.19438828, -2519230.80640189, -2519067.17337389, -2517564.67479945, 
-2516916.9539517, -2512102.05979041, -2510829.87788662, -2511752.66501718, 
-2514557.60667386, -2515920.84566496, -2514983.12625848, -2514066.12140984, 
-2512428.26286561, -2511040.1031902, -2507615.61588024, -2504527.64229299, 
-2497948.21335612, -2490438.90679884, -2489073.30059575, -2487101.60591081, 
-2485048.02119662, -2479179.66167563, -2478234.89283411, -2475638.73235202, 
-2472077.42625223, -2470917.73700336, -2467210.7719459, -2465146.80587687, 
-2464756.69068305, 3979240.91750093, 3979869.21125782, 3982644.50267001, 
3985854.68251267, 3987334.77896143, 3993083.57016668, 3993893.46660858, 
3996642.38103645, 3997717.92190821, 3998764.63617605, 4001094.53234293, 
4001710.30999891, 4005466.48050885, 4007408.00327857, 4008685.16070701, 
4010733.93808285, 4013893.53011966, 4015883.42334632, 4019040.48911394, 
4019761.24850631, 4021136.44266445, 4026524.4311791, 4028994.82021167, 
4031357.65934572, 4037345.31065658, 4040407.62428636, 4040939.29886049, 
4040742.91253541, 4040268.24547748, 4042092.34070452, 4043007.46779132, 
4041772.22555455, 4041533.19291715, 4041059.24079698, 4039730.02831388, 
4039490.95098141, 4039436.6081968, 4039944.23676835, 4039802.80034074, 
4036721.32471232, 4035018.40468451, 4031619.42696723, 4031376.16720174, 
4032661.24307734), dim = c(44L, 2L), class = c("XY", "LINESTRING", 
"sfg")), structure(c(-2455802.34972538, -2453391.28517002, -2451117.68782068, 
-2450222.14719026, -2451215.76855211, -2449752.46680174, -2444621.43262733, 
-2440904.34564473, -2437502.4275941, -2434979.22741248, -2434870.06439399, 
-2434314.70965303, -2429762.91818513, -2425666.797462, -2423226.98639026, 
-2417776.68884648, -2417968.12255325, -2417331.4894273, -2413444.50430002, 
-2411233.41758996, -2410265.12703715, -2409295.50378024, -2408307.88367522, 
-2407051.25040841, -2403963.12093879, -2399321.78235239, -2397528.97342155, 
-2396716.10233376, -2396620.75446153, -2397682.10864439, 3944200.72880045, 
3945490.44731529, 3947949.34275907, 3950434.19115405, 3951676.29139213, 
3954037.43418137, 3956904.81201506, 3957923.75787283, 3958278.09229342, 
3958897.96171749, 3960183.01602756, 3961283.87424064, 3963988.73352533, 
3964811.08035148, 3965072.69119469, 3968963.83065237, 3970066.35833318, 
3970931.15683084, 3973704.79428498, 3974769.45399505, 3975965.88168327, 
3978409.45939458, 3979858.37552362, 3980875.28452987, 3981978.04354102, 
3987999.98150925, 3989616.56833822, 3991162.64095591, 3991858.59803784, 
3993651.94569022), dim = c(30L, 2L), class = c("XY", "LINESTRING", 
"sfg")), structure(c(-2415373.75111227, -2414648.14835331, -2412390.74585963, 
-2410056.6386044, -2405120.36599989, -2402861.10810753, -2402986.24635509, 
-2406418.46686885, -2406982.27650905, -2405007.7006716, -2402118.76144927, 
-2398386.07718651, -2396958.87590622, -2394897.1222264, -2393341.57970908, 
-2389037.11456408, -2388414.48535736, -2384350.37346166, -2383080.34788004, 
-2381034.58159959, -2379091.73450143, -2377843.05765348, -2377693.25288159, 
-2376955.21571413, -2370738.31788899, -2368157.17696267, -2366350.53207778, 
-2365504.09842216, -2364203.71185746, -2361941.81933682, -2362724.85045673, 
-2362640.35403509, -2360130.16923897, -2356745.08525755, 3917793.08252573, 
3918212.12755188, 3918467.4571113, 3919265.73758435, 3925540.65361894, 
3929226.40695579, 3934178.55867348, 3940737.70011046, 3943334.05038866, 
3946991.37945152, 3948513.08487937, 3952165.26827236, 3952282.46236242, 
3952013.12261047, 3952634.66620483, 3956910.69385851, 3958186.40719334, 
3957769.94115641, 3957220.33234559, 3956915.42765594, 3957922.21259561, 
3959127.65713169, 3960775.84966022, 3963760.03373303, 3966038.71645059, 
3967760.91808983, 3969463.43424552, 3969571.34062276, 3969492.27068219, 
3970560.1252503, 3972321.63833911, 3974664.33394043, 3975945.15814048, 
3975988.18740328), dim = c(34L, 2L), class = c("XY", "LINESTRING", 
"sfg"))), class = c("sfc_LINESTRING", "sfc"), precision = 0, bbox = structure(c(xmin = -2990072.09038422, 
ymin = 3917793.08252573, xmax = -2356745.08525755, ymax = 4301496.30889971
), class = "bbox"), crs = structure(list(input = "ESRI:102003", 
    wkt = "PROJCRS[\"USA_Contiguous_Albers_Equal_Area_Conic\",\n    BASEGEOGCRS[\"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    CONVERSION[\"USA_Contiguous_Albers_Equal_Area_Conic\",\n        METHOD[\"Albers Equal Area\",\n            ID[\"EPSG\",9822]],\n        PARAMETER[\"Latitude of false origin\",37.5,\n            ANGLEUNIT[\"Degree\",0.0174532925199433],\n            ID[\"EPSG\",8821]],\n        PARAMETER[\"Longitude of false origin\",-96,\n            ANGLEUNIT[\"Degree\",0.0174532925199433],\n            ID[\"EPSG\",8822]],\n        PARAMETER[\"Latitude of 1st standard parallel\",29.5,\n            ANGLEUNIT[\"Degree\",0.0174532925199433],\n            ID[\"EPSG\",8823]],\n        PARAMETER[\"Latitude of 2nd standard parallel\",45.5,\n            ANGLEUNIT[\"Degree\",0.0174532925199433],\n            ID[\"EPSG\",8824]],\n        PARAMETER[\"Easting at false origin\",0,\n            LENGTHUNIT[\"metre\",1],\n            ID[\"EPSG\",8826]],\n        PARAMETER[\"Northing at false origin\",0,\n            LENGTHUNIT[\"metre\",1],\n            ID[\"EPSG\",8827]]],\n    CS[Cartesian,2],\n        AXIS[\"(E)\",east,\n            ORDER[1],\n            LENGTHUNIT[\"metre\",1]],\n        AXIS[\"(N)\",north,\n            ORDER[2],\n            LENGTHUNIT[\"metre\",1]],\n    USAGE[\n        SCOPE[\"unknown\"],\n        AREA[\"USA - CONUS - onshore\"],\n        BBOX[24.41,-124.79,49.38,-66.91]],\n    ID[\"ESRI\",102003]]"), class = "crs"), n_empty = 0L)), sf_column = "geometry", agr = structure(c(NAMEEN = NA_integer_), levels = c("constant", 
"aggregate", "identity"), class = "factor"), row.names = c(NA, 
5L), class = c("sf", "data.frame"))
pprl5pva

pprl5pva1#

好了,我已经回答了我自己的问题!下面是我必须做的。出于某种原因,如果我在geom_sf中使用shift_geometry,它可以工作-但如果我在绘图之前对 Dataframe 进行此操作......它就不能工作。

#Get a state map from tigris (=Census)
state_geo <- tigris::states(class = "sf", cb=TRUE, progress_bar=FALSE, resolution="20m")

#Marry some variables to each state
state_geo <- left_join(state_geo, state_difference,by=c("STUSPS"="state"))

#Get the rivers data
rivers <- st_read("sources/NA_Lakes_and_Rivers/data")

#We only need rivers (TYPE 17) in the US.
rivers2 <- rivers %>% filter(COUNTRY=="USA", TYPE=="17") %>% dplyr::select(c("geometry","NAMEEN"))

#Need to match the projections
state_geo <- sf::st_transform(state_geo,"ESRI:102003")
rivers2 <- sf::st_transform(rivers2,"ESRI:102003")

#This works!
ggplot()+geom_sf(data=shift_geometry(state_geo),aes(fill=median_premium))+
  geom_sf(data=shift_geometry(rivers2))+coord_sf(datum = NA)

相关问题