如何通过点之间的插值绘制不规则空间数据?

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

我有一个包含 3 列的数据框:x、y 和 z。在此数据集中,x 和 y 表示坐标位置,z 表示感兴趣的响应变量。然而,数据是不规则的;它代表了一块三面被水包围的陆地。我们只对陆地上的数据感兴趣,因此当我制作图像图时,我希望将水表示为空白。

我目前正在使用 ggplot2 中的以下代码。这会生成如下图块图。

ggplot(data=df, aes(x, y, height=0.005, width=0.005)) +
  geom_tile(aes(fill = z)) +
  scale_fill_gradientn(colours = tim.colors(7)) + 
  theme_classic()

我想用点之间的插值来绘制它。然而,我不希望插值超出陆地范围。有什么方法可以做到这一点吗?

数据子集的代码:

structure(list(x = c(-90.68602269474, -90.66545269474, -90.66061269474, 
-90.64609269474, -90.67634269474, -90.68360269474, -90.65093269474, 
-90.64004269474, -90.64246269474, -90.65698269474, -90.64367269474, 
-90.69933269474, -90.66424269474, -90.68602269474, -90.68360269474, 
-90.65819269474, -90.67876269474, -90.70175269474, -90.64609269474, 
-90.65214269474, -90.66424269474, -90.67755269474, -90.68481269474, 
-90.64851269474, -90.64125269474, -90.67271269474, -90.67997269474, 
-90.66424269474, -90.64367269474, -90.65698269474, -90.67755269474, 
-90.67634269474, -90.68239269474, -90.66061269474, -90.65819269474, 
-90.69086269474, -90.64004269474, -90.66908269474, -90.69207269474, 
-90.71506269474, -90.68481269474, -90.64730269474, -90.69207269474, 
-90.68965269474, -90.64730269474, -90.64125269474, -90.64004269474, 
-90.68602269474, -90.65214269474, -90.67271269474, -90.68723269474, 
-90.67634269474, -90.69691269474, -90.68602269474, -90.64246269474, 
-90.69328269474, -90.68965269474, -90.64730269474, -90.66303269474, 
-90.67876269474, -90.67271269474, -90.66424269474, -90.67150269474, 
-90.66787269474, -90.67029269474, -90.64367269474, -90.71143269474, 
-90.64004269474, -90.70417269474, -90.64367269474, -90.65577269474, 
-90.65940269474, -90.65093269474, -90.65698269474, -90.66545269474, 
-90.66061269474, -90.66182269474, -90.70054269474, -90.65819269474, 
-90.69812269474, -90.68723269474, -90.67755269474, -90.68481269474, 
-90.70296269474, -90.66908269474, -90.69570269474, -90.64851269474, 
-90.65093269474, -90.65214269474, -90.68844269474, -90.70054269474, 
-90.66303269474, -90.66303269474, -90.68602269474, -90.65577269474, 
-90.64851269474, -90.64730269474, -90.64730269474, -90.64367269474, 
-90.69449269474, -90.68481269474, -90.70296269474, -90.71385269474, 
-90.69086269474, -90.67755269474, -90.65819269474, -90.65335269474, 
-90.66908269474, -90.67271269474, -90.66303269474, -90.64609269474, 
-90.67392269474, -90.69207269474, -90.64367269474, -90.68481269474, 
-90.64004269474, -90.65698269474, -90.64004269474, -90.64972269474, 
-90.69570269474, -90.68844269474, -90.67755269474, -90.64246269474, 
-90.66303269474, -90.64851269474, -90.69207269474, -90.65698269474, 
-90.68360269474, -90.64367269474, -90.65940269474, -90.68239269474, 
-90.64488269474, -90.64488269474, -90.67876269474, -90.64004269474, 
-90.65940269474, -90.64004269474, -90.65819269474, -90.64730269474, 
-90.67513269474, -90.64367269474, -90.71022269474, -90.64367269474, 
-90.65456269474, -90.64125269474, -90.64367269474, -90.65456269474, 
-90.67392269474, -90.70417269474, -90.71022269474, -90.64488269474, 
-90.64730269474, -90.64972269474, -90.65819269474, -90.64972269474, 
-90.67029269474, -90.64730269474, -90.66908269474, -90.68239269474, 
-90.64851269474, -90.64246269474, -90.68481269474, -90.66666269474, 
-90.65940269474, -90.65335269474, -90.67150269474, -90.67876269474, 
-90.68118269474, -90.64488269474, -90.66182269474, -90.68360269474, 
-90.64488269474, -90.67513269474, -90.70538269474, -90.70780269474, 
-90.67271269474, -90.66787269474, -90.67029269474, -90.69086269474, 
-90.66908269474, -90.65819269474, -90.67513269474, -90.71506269474, 
-90.66061269474, -90.68239269474, -90.68118269474, -90.68602269474, 
-90.70538269474, -90.66303269474, -90.65214269474, -90.67392269474, 
-90.66545269474, -90.69328269474, -90.69570269474, -90.66303269474, 
-90.68118269474, -90.68965269474, -90.66061269474, -90.66303269474, 
-90.64609269474, -90.64488269474, -90.66424269474, -90.67392269474, 
-90.65214269474, -90.67271269474, -90.66908269474, -90.67755269474, 
-90.65940269474, -90.66182269474, -90.70538269474, -90.71627269474, 
-90.70780269474, -90.67876269474, -90.69328269474, -90.67029269474, 
-90.69691269474, -90.66061269474, -90.66424269474, -90.67150269474, 
-90.67392269474, -90.67150269474, -90.64488269474, -90.67150269474, 
-90.68481269474, -90.68481269474, -90.71143269474, -90.68965269474, 
-90.65577269474, -90.67876269474, -90.67513269474, -90.65698269474, 
-90.68723269474, -90.66182269474, -90.66908269474, -90.70659269474, 
-90.64367269474, -90.68844269474, -90.67755269474, -90.70296269474, 
-90.68360269474, -90.66061269474, -90.68118269474, -90.66666269474, 
-90.64609269474, -90.68965269474, -90.71264269474, -90.69933269474, 
-90.66061269474, -90.65698269474, -90.64851269474, -90.66303269474, 
-90.64488269474, -90.69570269474, -90.64851269474, -90.66061269474, 
-90.67876269474, -90.71022269474, -90.65577269474, -90.67634269474, 
-90.67392269474, -90.64609269474, -90.64730269474, -90.65214269474, 
-90.67271269474, -90.64367269474, -90.65335269474, -90.66424269474, 
-90.68481269474, -90.66666269474, -90.65940269474, -90.68602269474, 
-90.70054269474, -90.68239269474, -90.67029269474, -90.64367269474, 
-90.66908269474, -90.64609269474, -90.67271269474, -90.67513269474, 
-90.65456269474, -90.68723269474, -90.64125269474, -90.68723269474, 
-90.64004269474, -90.67755269474, -90.64246269474, -90.69691269474, 
-90.70538269474, -90.68239269474, -90.67029269474, -90.68481269474, 
-90.67029269474, -90.65335269474, -90.66787269474, -90.64488269474, 
-90.68844269474, -90.69812269474, -90.66061269474, -90.64125269474, 
-90.65456269474, -90.65214269474, -90.64246269474, -90.65819269474, 
-90.65335269474, -90.64609269474, -90.64609269474, -90.64004269474, 
-90.66666269474, -90.66666269474, -90.70417269474, -90.65456269474, 
-90.64851269474, -90.64488269474, -90.68602269474, -90.67755269474, 
-90.66545269474, -90.65335269474, -90.64972269474, -90.66666269474, 
-90.64246269474, -90.66061269474, -90.66908269474, -90.69570269474, 
-90.66303269474, -90.65698269474), y = c(31.55491278936, 31.62862278936, 
31.60918278936, 31.64563278936, 31.57273278936, 31.58164278936, 
31.53709278936, 31.65778278936, 31.62133278936, 31.60432278936, 
31.55977278936, 31.57030278936, 31.56625278936, 31.59622278936, 
31.58731278936, 31.62457278936, 31.54357278936, 31.54600278936, 
31.53952278936, 31.60513278936, 31.62052278936, 31.56463278936, 
31.58164278936, 31.53628278936, 31.64158278936, 31.56139278936, 
31.58974278936, 31.56868278936, 31.59055278936, 31.57516278936, 
31.60351278936, 31.55410278936, 31.59622278936, 31.60594278936, 
31.62781278936, 31.57840278936, 31.64806278936, 31.60756278936, 
31.59622278936, 31.54519278936, 31.55977278936, 31.56787278936, 
31.54033278936, 31.60432278936, 31.61485278936, 31.58569278936, 
31.56544278936, 31.58002278936, 31.54276278936, 31.59217278936, 
31.56706278936, 31.55491278936, 31.58893278936, 31.56463278936, 
31.64725278936, 31.53628278936, 31.53547278936, 31.56058278936, 
31.56868278936, 31.61485278936, 31.60837278936, 31.60594278936, 
31.53628278936, 31.54114278936, 31.53952278936, 31.55815278936, 
31.54438278936, 31.61080278936, 31.54438278936, 31.55491278936, 
31.55248278936, 31.54438278936, 31.57192278936, 31.62214278936, 
31.57435278936, 31.62457278936, 31.58083278936, 31.55329278936, 
31.60837278936, 31.57030278936, 31.55653278936, 31.53466278936, 
31.59460278936, 31.54195278936, 31.58326278936, 31.57030278936, 
31.62052278936, 31.62457278936, 31.57759278936, 31.57921278936, 
31.56058278936, 31.62538278936, 31.63024278936, 31.55653278936, 
31.58407278936, 31.62538278936, 31.60918278936, 31.55572278936, 
31.63267278936, 31.57597278936, 31.59703278936, 31.55896278936, 
31.54033278936, 31.60027278936, 31.55572278936, 31.63348278936, 
31.62214278936, 31.54438278936, 31.57273278936, 31.55167278936, 
31.64320278936, 31.58893278936, 31.57759278936, 31.59379278936, 
31.58650278936, 31.62862278936, 31.63267278936, 31.61485278936, 
31.58326278936, 31.55167278936, 31.60027278936, 31.56139278936, 
31.55977278936, 31.56706278936, 31.55491278936, 31.56058278936, 
31.62376278936, 31.57192278936, 31.60351278936, 31.62781278936, 
31.58326278936, 31.59460278936, 31.60270278936, 31.58326278936, 
31.55005278936, 31.54600278936, 31.53790278936, 31.60513278936, 
31.54114278936, 31.59703278936, 31.65373278936, 31.54033278936, 
31.60432278936, 31.60513278936, 31.61485278936, 31.58569278936, 
31.59055278936, 31.55167278936, 31.55815278936, 31.55086278936, 
31.53952278936, 31.63753278936, 31.55977278936, 31.60918278936, 
31.62052278936, 31.61647278936, 31.63591278936, 31.54195278936, 
31.60513278936, 31.56544278936, 31.56625278936, 31.54762278936, 
31.54843278936, 31.53790278936, 31.53385278936, 31.58407278936, 
31.55653278936, 31.53790278936, 31.59946278936, 31.60675278936, 
31.60108278936, 31.59379278936, 31.55977278936, 31.55086278936, 
31.54033278936, 31.60189278936, 31.58812278936, 31.57273278936, 
31.58488278936, 31.53466278936, 31.54843278936, 31.57678278936, 
31.53952278936, 31.62619278936, 31.60027278936, 31.58731278936, 
31.55005278936, 31.54114278936, 31.57030278936, 31.57840278936, 
31.59622278936, 31.58164278936, 31.58812278936, 31.55734278936, 
31.59622278936, 31.55734278936, 31.58488278936, 31.61485278936, 
31.60513278936, 31.61647278936, 31.58245278936, 31.58164278936, 
31.62214278936, 31.53871278936, 31.60270278936, 31.58650278936, 
31.53709278936, 31.61080278936, 31.58650278936, 31.55572278936, 
31.54195278936, 31.55329278936, 31.55977278936, 31.55572278936, 
31.55167278936, 31.54843278936, 31.56301278936, 31.58407278936, 
31.55977278936, 31.54195278936, 31.56382278936, 31.57030278936, 
31.61890278936, 31.56463278936, 31.57597278936, 31.55734278936, 
31.59784278936, 31.60189278936, 31.60837278936, 31.60756278936, 
31.57759278936, 31.57759278936, 31.59055278936, 31.53790278936, 
31.56463278936, 31.63105278936, 31.58245278936, 31.54519278936, 
31.54357278936, 31.58974278936, 31.59379278936, 31.56139278936, 
31.61242278936, 31.58002278936, 31.53466278936, 31.54195278936, 
31.53385278936, 31.53871278936, 31.56301278936, 31.59703278936, 
31.61647278936, 31.64725278936, 31.56463278936, 31.56220278936, 
31.53790278936, 31.57597278936, 31.54276278936, 31.62214278936, 
31.61728278936, 31.55653278936, 31.53871278936, 31.56382278936, 
31.55815278936, 31.54033278936, 31.59298278936, 31.62619278936, 
31.62781278936, 31.55896278936, 31.53628278936, 31.56220278936, 
31.55896278936, 31.54033278936, 31.60432278936, 31.54843278936, 
31.61485278936, 31.56706278936, 31.58893278936, 31.56301278936, 
31.57516278936, 31.61242278936, 31.55167278936, 31.58650278936, 
31.57030278936, 31.58893278936, 31.56625278936, 31.63429278936, 
31.59217278936, 31.56382278936, 31.59784278936, 31.62295278936, 
31.54600278936, 31.58245278936, 31.59217278936, 31.56058278936, 
31.54276278936, 31.55086278936, 31.57516278936, 31.57516278936, 
31.61161278936, 31.59379278936, 31.64077278936, 31.59379278936, 
31.56868278936, 31.55734278936, 31.55410278936, 31.63105278936, 
31.61404278936, 31.60756278936, 31.62133278936, 31.54762278936, 
31.57678278936, 31.54519278936, 31.57273278936, 31.59541278936, 
31.57030278936, 31.57273278936, 31.55005278936, 31.57516278936, 
31.60270278936, 31.59136278936, 31.58488278936, 31.55248278936, 
31.57111278936, 31.62781278936, 31.62538278936), z = c(2.21617364883423, 
5.18160009384155, 1.72322416305542, 4.58639198541641, 0, 3.6407219171524, 
2.07821702957153, 5.27303981781006, 3.40689146518707, 3.61956095695496, 
1.3246488571167, 3.69806087017059, 0, 1.53354144096375, 4.09159445762634, 
3.09474396705627, 3.6792277097702, 4.61969965696335, 3.24926733970642, 
3.04723405838013, 4.25243532657623, 1.68729043006897, 3.19221138954163, 
4.24685549736023, 4.26611590385437, 1.4171085357666, 3.67361676692963, 
1.61618304252625, 0, 0, 0.39594841003418, 1.69955015182495, 1.13015174865723, 
2.48155045509338, 3.60377383232117, 2.55310797691345, 3.3507684469223, 
0.380399703979492, 4.48928076028824, 5.48640012741089, 2.70782089233398, 
1.42980194091797, 2.7500901222229, 5.24256038665771, 4.5738468170166, 
0, 5.06571972370148, 2.1562488079071, 4.90820306539536, 2.2172794342041, 
2.57104802131653, 3.94599449634552, 5.30351972579956, 2.82142996788025, 
4.6315136551857, 4.88588309288025, 4.79950547218323, 0.798227787017822, 
0.216001033782959, 5.18160009384155, 2.26520204544067, 1.82085561752319, 
5.51106875389814, 3.44128680229187, 3.96330404281616, 3.09808754920959, 
2.87631058692932, 4.37582141160965, 1.47647762298584, 5.69976043701172, 
2.24710178375244, 3.4322202205658, 1.27843618392944, 4.60561090707779, 
0.705840110778809, 3.59524774551392, 0.443630695343018, 4.59360927343369, 
2.95776700973511, 3.00426506996155, 2.04206323623657, 3.09783983230591, 
1.61977005004883, 2.88991045951843, 1.26495265960693, 1.7446813583374, 
4.52431952953339, 3.20415484905243, 0.795879364013672, 2.96334505081177, 
0, 4.3867279291153, 5.21208000183105, 2.50863671302795, 0, 2.95625257492065, 
4.40247213840485, 0, 3.14974105358124, 0.953481674194336, 1.22647619247437, 
3.70125699043274, 1.96855306625366, 5.24256038665771, 3.22269725799561, 
4.22496837377548, 4.66877281665802, 1.69085741043091, 0, 0.943667411804199, 
4.3773655295372, 3.0216372013092, 2.7596333026886, 0, 3.92121362686157, 
3.21059989929199, 4.25104296207428, 4.56235527992249, 0, 4.48859536647797, 
1.9795138835907, 3.07512831687927, 1.95486283302307, 0.984044551849365, 
0, 3.05180215835571, 4.17794954776764, 0, 3.45956254005432, 3.49401152133942, 
3.79418385028839, 0.419891834259033, 3.38326096534729, 3.19350409507751, 
4.00007009506226, 2.87329316139221, 3.12628054618835, 3.77789235115051, 
2.26466965675354, 0.184243679046631, 5.21208000183105, 3.31840085983276, 
3.85629594326019, 3.4429646730423, 4.3976263999939, 0, 0, 3.33968782424927, 
0, 5.48640012741089, 3.17885899543762, 4.51800864934921, 0.416900157928467, 
3.99537467956543, 4.66246691346169, 2.828040599823, 4.35544657707214, 
2.38837504386902, 2.54606795310974, 3.76547050476074, 2.82735633850098, 
2.57662773132324, 1.96747541427612, 4.33749377727509, 3.60288429260254, 
0.744102954864502, 2.72504496574402, 5.06682556867599, 1.44944334030151, 
1.76421189308167, 1.97295427322388, 0.0745348930358887, 1.19782543182373, 
4.68425649404526, 3.58992123603821, 1.32766270637512, 1.39545011520386, 
0.135560512542725, 4.63663226366043, 4.35367631912231, 2.25015568733215, 
0.861161231994629, 5.51688003540039, 4.48733985424042, 0.318652153015137, 
4.20196950435638, 4.66947513818741, 4.89535307884216, 0.0985107421875, 
0.647350311279297, 0.933235645294189, 1.14137983322144, 0, 2.46584820747375, 
1.48323774337769, 2.02576160430908, 4.46684151887894, 2.33353757858276, 
2.65197277069092, 4.55186665058136, 0, 0, 5.21208000183105, 1.57779216766357, 
0.913077831268311, 1.51798391342163, 5.18213644623756, 1.69445753097534, 
0, 3.29378437995911, 5.48640012741089, 4.36081314086914, 2.44793081283569, 
2.60301470756531, 1.15275621414185, 4.72035443782806, 0.259468078613281, 
0, 0.553012847900391, 3.40464115142822, 1.88040280342102, 4.53549182415009, 
3.46175444126129, 2.76776933670044, 1.30834484100342, 5.42543983459473, 
2.00919556617737, 4.36328858137131, 3.28247952461243, 2.00969743728638, 
0, 2.32147479057312, 0, 4.86347550153732, 5.45591974258423, 4.03749012947083, 
4.12622857093811, 3.21701598167419, 3.08766222000122, 2.62924551963806, 
1.1977174282074, 4.39161467552185, 1.80306601524353, 0.242744922637939, 
3.47819352149963, 3.71696412563324, 3.0727813243866, 3.32302927970886, 
0.314995288848877, 1.27295970916748, 2.97435164451599, 4.60056006908417, 
0, 0.432526588439941, 4.94206875562668, 0.86236047744751, 3.14719390869141, 
4.64221012592316, 5.24256038665771, 2.37999844551086, 3.82510161399841, 
1.60189628601074, 3.05499053001404, 1.88647675514221, 0, 2.89338779449463, 
4.45307648181915, 2.79794096946716, 4.02468931674957, 0.683592319488525, 
3.34516978263855, 3.78860950469971, 2.94512486457825, 1.5796947479248, 
4.40989404916763, 2.81607890129089, 0, 4.8873964548111, 0.937100410461426, 
3.67776644229889, 3.75944709777832, 0, 0.581685543060303, 0, 
2.2755343914032, 3.30813264846802, 5.30351972579956, 2.33314681053162, 
0.435218334197998, 5.21208000183105, 4.24737870693207, 2.63135242462158, 
0, 0.388642311096191, 3.30683088302612, 2.84371876716614, 0.193492889404297, 
0.352246284484863, 4.42685329914093, 0.526696681976318, 4.72076925635338, 
0, 0, 1.32304096221924, 3.11385536193848, 4.26705467700958, 4.36811399459839, 
1.86139512062073, 4.69438397884369, 4.74122244119644, 0.348623752593994, 
5.06831973791122, 1.83810353279114, 2.11357808113098, 0.655951499938965, 
0, 3.3361828327179, 2.06640291213989, 1.38602519035339, 0, 0, 
0.953574180603027, 2.04014682769775, 3.85358107089996, 3.47133827209473
)), row.names = c(47002L, 32300L, 36516L, 28535L, 43985L, 42269L, 
49407L, 25774L, 33911L, 37546L, 46261L, 44408L, 45153L, 39210L, 
41109L, 33192L, 48679L, 48320L, 49181L, 37380L, 34069L, 45421L, 
42268L, 49478L, 29448L, 45970L, 40598L, 44727L, 40455L, 43548L, 
37699L, 47136L, 39213L, 37202L, 32484L, 42901L, 27992L, 36853L, 
39205L, 48424L, 46227L, 44884L, 49059L, 37519L, 35310L, 41481L, 
45313L, 42588L, 48812L, 40082L, 44993L, 47010L, 40757L, 45414L, 
28173L, 49441L, 49510L, 46125L, 44728L, 35284L, 36678L, 37199L, 
49459L, 48992L, 49161L, 46524L, 48540L, 36187L, 48546L, 47037L, 
47402L, 48583L, 44155L, 33723L, 43693L, 33190L, 42448L, 47241L, 
36690L, 44409L, 46747L, 49583L, 39553L, 48870L, 41955L, 44411L, 
34082L, 33198L, 43089L, 42745L, 46081L, 33011L, 31946L, 46748L, 
41802L, 33023L, 36527L, 46907L, 31426L, 43364L, 39041L, 46344L, 
49041L, 38362L, 46882L, 31235L, 33726L, 48575L, 43988L, 47519L, 
29081L, 40776L, 43056L, 39759L, 41277L, 32321L, 31415L, 35316L, 
41971L, 47492L, 38364L, 45966L, 46262L, 45013L, 47033L, 46088L, 
33370L, 44128L, 37727L, 32483L, 41944L, 39586L, 37895L, 41947L, 
47781L, 48355L, 49343L, 37375L, 49009L, 39049L, 26700L, 49044L, 
37557L, 37378L, 35315L, 41479L, 40446L, 47510L, 46474L, 47602L, 
49182L, 30346L, 46256L, 36518L, 34081L, 34941L, 30706L, 48898L, 
37355L, 45306L, 45171L, 48101L, 47998L, 49327L, 49663L, 41789L, 
46754L, 49309L, 38568L, 37030L, 38200L, 39758L, 46235L, 47606L, 
49046L, 38041L, 40952L, 43990L, 41607L, 49590L, 48005L, 43225L, 
49124L, 32836L, 38369L, 41111L, 47743L, 48961L, 44438L, 42933L, 
39220L, 42284L, 40931L, 46611L, 39229L, 46623L, 41608L, 35299L, 
37371L, 34961L, 42139L, 42285L, 33709L, 49256L, 37872L, 41290L, 
49385L, 36171L, 41296L, 46859L, 48859L, 47235L, 46232L, 46869L, 
47513L, 47973L, 45710L, 41795L, 46238L, 48894L, 45564L, 44453L, 
34413L, 45415L, 43372L, 46598L, 38867L, 38055L, 36673L, 36848L, 
43085L, 43060L, 40440L, 49319L, 45397L, 31783L, 42103L, 48455L, 
48659L, 40595L, 39745L, 45963L, 35817L, 42621L, 49573L, 48862L, 
49625L, 49249L, 45713L, 39071L, 34947L, 28171L, 45406L, 45856L, 
49326L, 43377L, 48764L, 33724L, 34761L, 46758L, 49261L, 45584L, 
46517L, 49075L, 39932L, 32842L, 32479L, 46359L, 49463L, 45847L, 
46358L, 49052L, 37525L, 47995L, 35313L, 45008L, 40799L, 45700L, 
43533L, 35827L, 47499L, 41313L, 44418L, 40804L, 45142L, 31069L, 
40062L, 45536L, 38873L, 33536L, 48334L, 42118L, 40098L, 46108L, 
48818L, 47620L, 43514L, 43545L, 36012L, 39750L, 29620L, 39760L, 
44732L, 46646L, 47161L, 31781L, 35490L, 36855L, 33891L, 48085L, 
43242L, 48479L, 44011L, 39380L, 44426L, 43994L, 47770L, 43554L, 
37877L, 40281L, 41632L, 47391L, 44265L, 32480L, 33016L), class = "data.frame")
r ggplot2 geospatial spatial
1个回答
0
投票

有几种方法可以实现这一目标。您可以通过

interp
包执行线性插值:

library(interp)

df2 <- interp(df$x, df$y, df$z, nx = 500, ny = 500) |> 
  interp2xyz() |> 
  as.data.frame()

或者使用

gam
进行样条平滑(我们从
interp
方法复制 NA 值以保留形状的轮廓)

library(mgcv)

df2$z2 <- mgcv::gam(z ~ s(y) + s(x), gamma = -1, data = df) |>
          predict(newdata = df2)
df2$z2[is.na(df2$z)] <- NA

我们可以看到这些方法与原始数据的比较:

library(ggplot2)
library(patchwork)

p1 <- ggplot(data = df, aes(x, y, height = 0.005, width = 0.005)) +
  geom_tile(aes(fill = z)) +
  scale_fill_distiller(palette = "Spectral", na.value = NA) +
  theme_classic() +
  ggtitle("Raw data")

p2 <- ggplot(data = df2, aes(x, y)) +
  geom_raster(aes(fill = z)) +
  scale_fill_distiller(palette = "Spectral", na.value = NA) + 
  theme_classic() +
  ggtitle("Linear interpolation")

p3 <- ggplot(data = df2, aes(x, y)) +
  geom_raster(aes(fill = z2)) +
  scale_fill_distiller(palette = "Spectral", na.value = NA) + 
  theme_classic() +
  ggtitle("Smoothing via GAM")

p1 + p2 + p3

© www.soinside.com 2019 - 2024. All rights reserved.