如何使用 coord_map 获取 ggplot 以使用 ESPG 投影 ID?

How do I get ggplot with coord_map to use an ESPG projection id?

我正在绘制几个图层,一个是多边形图层,它在lat/lon中。另一个是 sf 对象。如果我将我的 sf 对象放入 WGS84 (ESPG 4326),那么多边形和 sf 对象会很好地叠加在一起。然而,我在阿拉斯加工作,所以 WGS84 确实扭曲了事情。

我的 sf 对象最初是在阿拉斯加阿尔伯斯投影中 (espg:2964)。我希望将多边形对象投影到该坐标系中。我认为你应该使用 coord_map() 来处理它,但我不确定如何让 coord_map 进行阿拉斯加阿尔伯斯投影,因为 coord_map() 似乎没有espg 代码或 proj4 字符串。

在此先感谢您的帮助。

此代码有效,但将两者都放入 wgs84:

results.transform = results %>% st_transform(4326)
ggplot(ak, aes(long, lat, group=group))+
  geom_polygon(fill="grey", color="black")+
  geom_sf(data=results.transform, aes(color=doy),inherit.aes = FALSE, size=2)

此代码不会引发错误,但多边形和 sf 图层不会相互叠加:

#results is the original sf object in espg 2964
ggplot(ak, aes(long, lat, group=group))+
  geom_polygon(fill="grey", color="black")+
  coord_map("albers", lat0 =50, lat1=55)+
  geom_sf(data=results, aes(color=doy),inherit.aes = FALSE, size=2)

输入样本(使用 dput):

结果:

results =structure(list(id = c("1", "2", "3"), doy = c(152, 158, 142), 
    doy_meanthresh = c(156, 160, 140), threshold = c(0.173556868958583, 
    0.109912373062543, 0.074946580423674), smooth.series = list(
        c(0.356716711457841, 0.370050893258325, 0.383236999766461, 
        0.396376974233949, 0.40957275991249, 0.422784291482468, 
        0.435895856103075, 0.448895925744217, 0.461772972375802, 
        0.474515467967738, 0.48722268616777, 0.499933470515835, 
        0.512545647820125, 0.524957044888832, 0.537065488530148, 
        0.549189274496968, 0.561532939869938, 0.573823673448877, 
        0.5857886640336, 0.597155100423927, 0.608751798005646, 
        0.621116663488914, 0.633540522660091, 0.645314201305544, 
        0.655728525211634, 0.665571086939856, 0.675708836320647, 
        0.685551635043781, 0.694509344799033, 0.701991827276177, 
        0.70938842013153, 0.717660871422796, 0.725577658441836, 
        0.731907258480512, 0.735418148830686, 0.737609381488737, 
        0.740068708326791, 0.741697656450321, 0.741397752964802, 
        0.738070524975708, 0.730787113459408, 0.720275348839784, 
        0.707921792393576, 0.695113005397529, 0.683235549128384, 
        0.66854065601544, 0.648565682783239, 0.626626377151392, 
        0.606038486839507, 0.590117759567193, 0.575248354822936, 
        0.557183338977548, 0.538291820074129, 0.520942906155777, 
        0.507505705265592, 0.497170423227522, 0.487542218972326, 
        0.478612630203321, 0.470373194623822, 0.462815449937146, 
        0.458831683827816, 0.459466542404155, 0.461940101005184, 
        0.463472434969922, 0.461283619637389, 0.458826926942516, 
        0.459760482491641, 0.461611642130895, 0.461907761706409, 
        0.458176197064313, 0.45041527548862, 0.440794234319326, 
        0.430096794486539, 0.419106676920368, 0.408607602550923, 
        0.396242242656226, 0.380503346606902, 0.363449752471964, 
        0.347140298320423, 0.333633822221291, 0.321253095838767, 
        0.307606088569194, 0.293679435079791, 0.28045977003778, 
        0.268933728110384, 0.258699817638372, 0.248739536036072, 
        0.239114001581039, 0.22988433255083, 0.221111647222998, 
        0.213576575535607, 0.207511807976447, 0.202156553647666, 
        0.196750021651411, 0.19053142108983, 0.183900129705502, 
        0.177683584689661, 0.17176308431744, 0.166019926863971, 
        0.160335410604389, 0.153743267353215, 0.146014563103421, 
        0.138136597397814, 0.131096669779199, 0.125882079790385, 
        0.121448622919517, 0.116554575980903, 0.111890960506595, 
        0.10814879802864, 0.106019110079089, 0.105661169696536, 
        0.106498694582266, 0.108119373262358, 0.110110894262892, 
        0.11206094610995, 0.11540233539241, 0.120997725074791, 
        0.127579588246629, 0.133880397997461, 0.138632627416822, 
        0.143475963087052, 0.150098990228934, 0.157307529889666, 
        0.163907403116449, 0.168704430956481, 0.172368187413333, 
        0.176173416587563, 0.179833694671856, 0.183062597858895, 
        0.185573702341366, 0.187052218702838, 0.187638597324987, 
        0.187729274097655, 0.187720684910687, 0.188009265653923, 
        0.188094043051569, 0.187473296399738, 0.186542340446139, 
        0.185696489938482, 0.185331059624476, 0.18519846898834, 
        0.184852749545972, 0.184391634092598, 0.183912855423446, 
        0.183514146333744, 0.183117312589153, 0.182606699408324, 
        0.182023848765966, 0.181410302636787, 0.180807602995498, 
        0.180212334083628, 0.179591966640683, 0.178944372388341, 
        0.178267423048277, 0.177558990342169, 0.176816945991692, 
        0.176039161718523, 0.175223509244339, 0.174367860290815
        ), c(0.774610362619149, 0.746412269781788, 0.719913789191898, 
        0.695420287796062, 0.673237132540861, 0.653273968452586, 
        0.635200894750251, 0.618963959669522, 0.604509211446066, 
        0.59178269831555, 0.581143108860635, 0.572741206185169, 
        0.566211150306591, 0.561187101242345, 0.557303219009872, 
        0.555232501533965, 0.555534753213423, 0.557674343776691, 
        0.561115642952215, 0.565323020468442, 0.573372729704498, 
        0.586895414376992, 0.603187029720595, 0.619543530969977, 
        0.63326087335981, 0.649100787927623, 0.670935206211495, 
        0.694725384196925, 0.716432577869408, 0.732018043214443, 
        0.745048344614319, 0.760240534670835, 0.775281601698759, 
        0.787858534012854, 0.795658319927888, 0.799986340749408, 
        0.803308852568436, 0.805054155877942, 0.804650551170897, 
        0.801526338940272, 0.794278541385548, 0.783000246619136, 
        0.769363854003397, 0.755041762900692, 0.741706372673384, 
        0.725475019829043, 0.703589930868272, 0.679410840142977, 
        0.656297482005064, 0.637609590806439, 0.619522671663228, 
        0.59768917697572, 0.574684262022063, 0.553083082080407, 
        0.535460792428901, 0.520724989583801, 0.506208924554847, 
        0.492126234360422, 0.478690556018906, 0.466115526548682, 
        0.456163205377972, 0.449307080301381, 0.443827930886861, 
        0.438006536702364, 0.430123677315843, 0.422161025148951, 
        0.416341337462143, 0.411307021081574, 0.405700482833399, 
        0.398164129543771, 0.388509330252805, 0.377833000710694, 
        0.366705867808713, 0.355698658438137, 0.34538209949024, 
        0.334476225402477, 0.322019919293152, 0.309062181074817, 
        0.296652010660021, 0.285838407961315, 0.276447235011443, 
        0.267512949813937, 0.258897446237528, 0.250462618150946, 
        0.242070359422922, 0.234129641095434, 0.226902750204945, 
        0.220031554611916, 0.21315792217681, 0.205923720760086, 
        0.19852372751621, 0.191343478746417, 0.184310981031329, 
        0.177354240951568, 0.170401265087756, 0.163192040497278, 
        0.155702671847874, 0.148215342135701, 0.141012234356916, 
        0.134375531507676, 0.127758694427545, 0.120727232121655, 
        0.113731399834516, 0.107221452810637, 0.101647646294526, 
        0.0965005694542742, 0.091209569652388, 0.0861330394250022, 
        0.0816293713082512, 0.0780569578382695, 0.075371315752509, 
        0.0732346851768886, 0.0715655437302434, 0.0702823690314086, 
        0.0693036386992192, 0.0691863250953481, 0.0701883842483067, 
        0.0717797692771877, 0.073430433301084, 0.0746103294390886, 
        0.0761784204310707, 0.0788956524181342, 0.0820849536212236, 
        0.0850692522612836, 0.0871714765592586, 0.0890972731947091, 
        0.0916871981841642, 0.094466543754022, 0.0969606021306803, 
        0.0986946655405372, 0.0998521594454551, 0.100914181761071, 
        0.101852533394445, 0.102639015252636, 0.103245428242704, 
        0.103462264899288, 0.103250578499722, 0.102838011065113, 
        0.102452204616565, 0.102320801175184, 0.102287042959656, 
        0.102088178523127, 0.101792859388387, 0.10146973707823, 
        0.101187463115447, 0.100889974984667, 0.100495657145597, 
        0.100034831583652, 0.0995378202842451, 0.0990349452327908, 
        0.0985243275768939, 0.097982837008049, 0.0974089183275044, 
        0.0968010163365085, 0.0961575758363094, 0.0954770416281557, 
        0.0947578585132956, 0.0939984712929773, 0.0931973247684494
        ), c(0.754994105046569, 0.759262980856892, 0.763248462599852, 
        0.767062652758686, 0.77081765381663, 0.774472838830307, 
        0.777902459086919, 0.781090934415562, 0.784022684645335, 
        0.786682129605334, 0.789179136777192, 0.791558707699299, 
        0.793707963285894, 0.795514024451214, 0.796864012109498, 
        0.798059444416928, 0.799307737538445, 0.800354859401417, 
        0.800946777933214, 0.800829461061205, 0.800535183022801, 
        0.800489794522664, 0.800279628189041, 0.799491016650182, 
        0.797710292534332, 0.793727807721641, 0.787531875152631, 
        0.780505769046113, 0.774032763620899, 0.7694961330958, 
        0.766814565429895, 0.764644322900798, 0.76247504120512, 
        0.759796356039472, 0.756097903100465, 0.753105409945246, 
        0.751812834849642, 0.750612159588286, 0.747895365935809, 
        0.742054435666847, 0.734349211270437, 0.726724708968583, 
        0.718600671135361, 0.709396840144846, 0.698532958371117, 
        0.685958066059214, 0.672217339551, 0.657624725365782, 
        0.642494170022865, 0.627139620041553, 0.608952937377657, 
        0.586693889874402, 0.562728282882226, 0.539421921751564, 
        0.519140611832852, 0.498827185487661, 0.475313450129851, 
        0.450798673688293, 0.427482124091856, 0.40756306926941, 
        0.391594783774196, 0.377888811456193, 0.365281824189383, 
        0.352610493847746, 0.338711492305265, 0.324742577633052, 
        0.312133619351959, 0.300129456510569, 0.287974928157469, 
        0.274914873341241, 0.260271155053797, 0.244483391182105, 
        0.228473053117332, 0.21316161225065, 0.199470539973226, 
        0.18615371420193, 0.171962954821233, 0.157816646173702, 
        0.144633172601908, 0.13333091844842, 0.12330701757161, 
        0.113524600030087, 0.104251044466771, 0.0957537295245833, 
        0.0883000338464436, 0.0824702507214148, 0.0782344478172879, 
        0.0749446653450757, 0.0719529435157916, 0.0686113225404485, 
        0.0661995394292459, 0.0657367472597934, 0.0661841103442199, 
        0.0665027929946539, 0.0656539595232242, 0.0644681965998428, 
        0.0641406846621584, 0.0641794786739045, 0.0640926335988149, 
        0.0633882044006231, 0.0619833956742033, 0.0602788637336122, 
        0.058507841121357, 0.0569035603799449, 0.0556992540518834, 
        0.0546528395507013, 0.0534742168963767, 0.0523245925410911, 
        0.0513651729370255, 0.0507571645363614, 0.0504376622860656, 
        0.050241048436331, 0.0501744603941783, 0.0502450355666279, 
        0.0504599113607003, 0.0510283221178971, 0.052012828131863, 
        0.0532005557378333, 0.0543786312710432, 0.055334181066728, 
        0.0564245098836434, 0.057962991953209, 0.0596708578196554, 
        0.0612693380272137, 0.0624796631201147, 0.0635479377810315, 
        0.0648035693895804, 0.0660927425354069, 0.0672616418081562, 
        0.0681564517974736, 0.0687850088878122, 0.0692835297177245, 
        0.0696907692580061, 0.0700454824794523, 0.0703864243528584, 
        0.0706277104270338, 0.0707098566767007, 0.0707112187115236, 
        0.0707101521411668, 0.0707850125752948, 0.0708833573425856, 
        0.0709155836751044, 0.0709043070375961, 0.0708721428948058, 
        0.0708417067114787, 0.070795102344694, 0.0707055967085618, 
        0.0705825498533734, 0.0704353218294197, 0.0702732726869918, 
        0.0700959115439309, 0.0698961835839182, 0.069673602956005, 
        0.0694276838092427, 0.0691579402926828, 0.0688638865553766, 
        0.0685450367463756, 0.0682009050147312, 0.0678310055094947
        )), doy.series = list(c(55, 56, 57, 58, 59, 60, 61, 62, 
    63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 
    78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 
    93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 
    106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 
    118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 
    130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 
    142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 
    154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 
    166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 
    178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 
    190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 
    202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213
    ), c(55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 
    68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 
    83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 
    98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 
    110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 
    122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 
    134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 
    146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 
    158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 
    170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 
    182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 
    194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 
    206, 207, 208, 209, 210, 211, 212, 213), c(55, 56, 57, 58, 
    59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 
    74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 
    89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 
    103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 
    115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 
    127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 
    139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 
    151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 
    163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 
    175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 
    187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 
    199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 
    211, 212, 213)), year = c("2000", "2000", "2000"), geometry = structure(list(
        structure(c(-1661436.84461341, -1661115.05202279, -1661228.76098043, 
        -1660425.2592279, -1659056.23818505, -1658307.31829931, 
        -1656634.51866674, -1656620.45332118, -1656912.32303615, 
        -1658398.86388024, -1659675.83584702, 4861078.89101535, 
        4860325.10237897, 4859645.11439113, 4857608.78556975, 
        4855675.65967803, 4850643.61215932, 4846958.71327386, 
        4845855.03478051, 4845305.91718129, 4844752.81259044, 
        4843366.85928571), .Dim = c(11L, 2L), class = c("XY", 
        "LINESTRING", "sfg")), structure(c(-1658241.19039753, 
        -1654712.75990038, -1651942.38784669, -1650404.98329246, 
        -1648988.60261601, -1645978.84503211, -1643222.00421422, 
        -1639781.08711522, -1638602.99625793, -1637902.54559266, 
        -1639311.78216295, -1639248.9245045, -1638107.13443693, 
        -1637058.59090575, 4774391.6371314, 4774653.20892703, 
        4775738.21754787, 4774227.59717794, 4771389.21271209, 
        4770934.19458225, 4769568.55276305, 4771094.02869766, 
        4770996.1462525, 4769827.51755788, 4767060.08312911, 
        4765949.41565321, 4763854.76024583, 4763305.58844374), .Dim = c(14L, 
        2L), class = c("XY", "LINESTRING", "sfg")), structure(c(-1637058.59090575, 
        -1634446.33873168, -1631719.63870714, -1629361.15811325, 
        -1628116.65633233, -1627013.82581023, -1626189.71570171, 
        -1625551.50211327, -1624075.99821849, -1622805.13871411, 
        -1622273.7980277, -1620254.94375451, -1618394.36936387, 
        -1616289.48293551, -1615299.3539038, -1613130.11112742, 
        4763305.58844374, 4761938.07374864, 4761581.34917333, 
        4761984.29842838, 4762878.85905119, 4765596.31389399, 
        4766660.74696295, 4766903.7237966, 4766207.39483074, 
        4763687.87083808, 4763298.06222046, 4763460.09645817, 
        4765651.28116598, 4765148.87541194, 4763932.07676722, 
        4759749.97915703), .Dim = c(16L, 2L), class = c("XY", 
        "LINESTRING", "sfg"))), class = c("sfc_LINESTRING", "sfc"
    ), precision = 0, bbox = structure(c(-1661436.84461341, 4759749.97915703, 
    -1613130.11112742, 4861078.89101535), .Names = c("xmin", 
    "ymin", "xmax", "ymax"), class = "bbox"), crs = structure(list(
        epsg = 2964L, proj4string = "+proj=aea +lat_1=55 +lat_2=65 +lat_0=50 +lon_0=-154 +x_0=0 +y_0=0 +datum=NAD27 +units=us-ft +no_defs"), .Names = c("epsg", 
    "proj4string"), class = "crs"), n_empty = 0L)), .Names = c("id", 
"doy", "doy_meanthresh", "threshold", "smooth.series", "doy.series", 
"year", "geometry"), row.names = c(NA, 3L), class = c("sf", "data.frame"
), sf_column = "geometry", agr = structure(c(NA_integer_, NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_
), .Names = c("id", "doy", "doy_meanthresh", "threshold", "smooth.series", 
"doy.series", "year"), .Label = c("constant", "aggregate", "identity"
), class = "factor"))

阿克:

ak=structure(list(long = c(-130.013061523438, -130.01530456543, 
-130.015808105469, -130.015014648438, -130.015808105469, -130.014724731445, 
-130.014434814453, -130.014434814453, -130.01416015625, -130.014724731445, 
-130.027786254883, -130.041656494141, -130.053924560547, -130.066925048828, 
-130.080841064453, -130.088623046875, -130.117797851562, -130.146667480469, 
-130.175262451172, -130.204147338867, -130.229187011719, -130.235260009766, 
-130.242767333984, -130.249176025391, -130.25749206543, -130.264984130859, 
-130.275527954102, -130.285278320312, -130.296676635742, -130.307220458984, 
-130.318328857422, -130.329742431641, -130.341934204102, -130.354141235352, 
-130.365249633789, -130.375564575195, -130.387496948242, -130.397811889648, 
-130.409713745117, -130.414184570312), lat = c(55.9116859436035, 
55.9241752624512, 55.9383277893066, 55.952766418457, 55.9669189453125, 
55.9814147949219, 55.9952812194824, 56.0100021362305, 56.0238723754883, 
56.0258178710938, 56.0422058105469, 56.05859375, 56.075553894043, 
56.0919380187988, 56.1086120605469, 56.1180648803711, 56.1122207641602, 
56.1066627502441, 56.1008186340332, 56.0949745178223, 56.090274810791, 
56.0925102233887, 56.0961227416992, 56.0994453430176, 56.1021919250488, 
56.1058616638184, 56.1083221435547, 56.1111297607422, 56.11279296875, 
56.1150283813477, 56.1166915893555, 56.118350982666, 56.1203002929688, 
56.1222457885742, 56.123908996582, 56.1252822875977, 56.1263732910156, 
56.1278038024902, 56.1288948059082, 56.1288948059082), group = c(1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), order = 1:40, 
    region = c("USA", "USA", "USA", "USA", "USA", "USA", "USA", 
    "USA", "USA", "USA", "USA", "USA", "USA", "USA", "USA", "USA", 
    "USA", "USA", "USA", "USA", "USA", "USA", "USA", "USA", "USA", 
    "USA", "USA", "USA", "USA", "USA", "USA", "USA", "USA", "USA", 
    "USA", "USA", "USA", "USA", "USA", "USA"), subregion = c("Alaska", 
    "Alaska", "Alaska", "Alaska", "Alaska", "Alaska", "Alaska", 
    "Alaska", "Alaska", "Alaska", "Alaska", "Alaska", "Alaska", 
    "Alaska", "Alaska", "Alaska", "Alaska", "Alaska", "Alaska", 
    "Alaska", "Alaska", "Alaska", "Alaska", "Alaska", "Alaska", 
    "Alaska", "Alaska", "Alaska", "Alaska", "Alaska", "Alaska", 
    "Alaska", "Alaska", "Alaska", "Alaska", "Alaska", "Alaska", 
    "Alaska", "Alaska", "Alaska")), .Names = c("long", "lat", 
"group", "order", "region", "subregion"), row.names = c(NA, 40L
), class = "data.frame")

ak 转换为 EPSG:2964,然后在 ggplot 调用

中使用 coord_sf(datum = st_crs(2964))
library(sp)
library(sf)
library(tidyverse)

# Convert ak to EPSG:2964
ak_sf <- sf::st_as_sf(ak, coords = c("long", "lat"), crs = 4326)
ak_sf_tp <- st_transform(ak_sf, crs = 2964)
str(ak_sf_tp)

# Plot everything under EPSG:2964 
ggplot() +
  geom_sf(data = ak_sf_tp) +
  geom_sf(data = results, aes(color = doy), inherit.aes = FALSE, size = 2) +
  coord_sf(datum = st_crs(2964))

编辑:如果我们想将ak绘制为多边形

# Get lat/long from sf object
ak_poly <- as(ak_sf_tp, 'Spatial') %>% 
  as.data.frame() %>% 
  rename(long = "coords.x1", lat = "coords.x2")

ggplot(ak_poly, aes(long, lat, group = group)) +
  geom_polygon(fill = "grey", color = "black") +
  geom_sf(data = results, aes(color = doy), inherit.aes = FALSE, size = 2) +
  coord_sf(datum = st_crs(2964))