创建网格,然后在R中绘制网格上的点

时间:2013-07-16 02:24:47

标签: r graphics

我试图绘制一些复杂的图表。首先,我想创建网格,然后我想在网格中绘制变量。

要创建网格,我有网格角的坐标。对于每一行,我有8列代表四个角的坐标。示例数据集如下:

SN  X1  Y1  X2  Y2  X3  Y3  X4  Y4
1   512398.40   3001846.80  512397.40   3002769.80  513305.40   3002770.80  513306.40   3001847.80
2   513306.40   3001847.80  513305.40   3002770.80  514213.40   3002771.80  514214.40   3001848.80
3   514214.40   3001848.80  514213.40   3002771.80  515121.50   3002772.50  515123.20   3001850.00
4   515123.20   3001850.00  515121.50   3002772.50  516030.10   3002774.00  516031.40   3001851.50
5   516031.40   3001851.50  516030.10   3002774.00  516938.40   3002775.50  516939.70   3001852.80
6   516939.70   3001852.80  516938.40   3002775.50  517846.60   3002776.80  517848.20   3001853.80
7   517848.20   3001853.80  517846.60   3002776.80  518755.20   3002778.00  518756.40   3001855.00
8   518756.40   3001855.00  518755.20   3002778.00  519663.20   3002779.50  519664.60   3001856.50
9   519664.60   3001856.50  519663.20   3002779.50  520571.60   3002781.00  520573.20   3001858.00
10  520573.20   3001858.00  520571.60   3002781.00  521479.60   3002782.80  521481.40   3001859.80
11  521481.40   3001859.80  521479.60   3002782.80  522388.20   3002784.50  522389.60   3001861.50
12  522389.60   3001861.50  522388.20   3002784.50  523296.40   3002786.00  523298.20   3001863.00
13  523298.20   3001863.00  523296.40   3002786.00  524204.40   3002787.80  524206.40   3001864.80
14  524206.40   3001864.80  524204.40   3002787.80  525113.20   3002789.80  525114.60   3001866.80
15  525114.60   3001866.80  525113.20   3002789.80  526021.40   3002791.80  526023.40   3001868.80
16  526023.40   3001868.80  526021.40   3002791.80  526929.40   3002793.80  526931.40   3001870.80
17  526931.40   3001870.80  526929.40   3002793.80  527837.40   3002795.80  527839.60   3001872.80
18  527839.60   3001872.80  527837.40   3002795.80  528746.20   3002798.00  528748.30   3001874.80
19  513305.40   3002770.80  513304.40   3003693.80  514212.40   3003694.80  514213.40   3002771.80
20  514213.40   3002771.80  514212.40   3003694.80  515120.40   3003695.80  515121.50   3002772.50
21  515121.50   3002772.50  515120.40   3003695.80  516028.60   3003697.00  516030.10   3002774.00
22  516030.10   3002774.00  516028.60   3003697.00  516937.10   3003698.50  516938.40   3002775.50
23  516938.40   3002775.50  516937.10   3003698.50  517845.40   3003699.80  517846.60   3002776.80
24  517846.60   3002776.80  517845.40   3003699.80  518753.60   3003701.00  518755.20   3002778.00
25  518755.20   3002778.00  518753.60   3003701.00  519661.60   3003702.50  519663.20   3002779.50
26  519663.20   3002779.50  519661.60   3003702.50  520570.20   3003704.00  520571.60   3002781.00
27  520571.60   3002781.00  520570.20   3003704.00  521478.30   3003705.80  521479.60   3002782.80
28  521479.60   3002782.80  521478.30   3003705.80  522386.40   3003707.50  522388.20   3002784.50
29  522388.20   3002784.50  522386.40   3003707.50  523294.40   3003709.00  523296.40   3002786.00
30  523296.40   3002786.00  523294.40   3003709.00  524202.60   3003710.80  524204.40   3002787.80
31  524204.40   3002787.80  524202.60   3003710.80  525111.40   3003712.80  525113.20   3002789.80
32  525113.20   3002789.80  525111.40   3003712.80  526019.40   3003714.80  526021.40   3002791.80
33  526021.40   3002791.80  526019.40   3003714.80  526927.40   3003716.80  526929.40   3002793.80
34  526929.40   3002793.80  526927.40   3003716.80  527835.40   3003718.80  527837.40   3002795.80
35  527837.40   3002795.80  527835.40   3003718.80  528743.80   3003721.00  528746.20   3002798.00
36  513304.40   3003693.80  513303.40   3004616.80  514211.40   3004617.80  514212.40   3003694.80
37  514212.40   3003694.80  514211.40   3004617.80  515119.40   3004618.80  515120.40   3003695.80
38  515120.40   3003695.80  515119.40   3004618.80  516027.40   3004620.00  516028.60   3003697.00
39  516028.60   3003697.00  516027.40   3004620.00  516935.60   3004621.50  516937.10   3003698.50
40  516937.10   3003698.50  516935.60   3004621.50  517844.20   3004622.80  517845.40   3003699.80
41  517845.40   3003699.80  517844.20   3004622.80  518752.30   3004624.00  518753.60   3003701.00
42  518753.60   3003701.00  518752.30   3004624.00  519660.30   3004625.50  519661.60   3003702.50
43  519661.60   3003702.50  519660.30   3004625.50  520568.40   3004627.00  520570.20   3003704.00
44  520570.20   3003704.00  520568.40   3004627.00  521476.30   3004629.00  521478.30   3003705.80
45  521478.30   3003705.80  521476.30   3004629.00  522384.60   3004630.50  522386.40   3003707.50
46  522386.40   3003707.50  522384.60   3004630.50  523292.60   3004632.00  523294.40   3003709.00
47  523294.40   3003709.00  523292.60   3004632.00  524201.20   3004633.80  524202.60   3003710.80
48  524202.60   3003710.80  524201.20   3004633.80  525109.40   3004635.80  525111.40   3003712.80
49  525111.40   3003712.80  525109.40   3004635.80  526017.40   3004637.80  526019.40   3003714.80
50  526019.40   3003714.80  526017.40   3004637.80  526925.40   3004639.80  526927.40   3003716.80
51  526927.40   3003716.80  526925.40   3004639.80  527833.40   3004641.80  527835.40   3003718.80
52  515119.40   3004618.80  515118.40   3005541.80  516026.40   3005543.00  516027.40   3004620.00
53  516027.40   3004620.00  516026.40   3005543.00  516934.40   3005544.50  516935.60   3004621.50
54  516935.60   3004621.50  516934.40   3005544.50  517842.50   3005545.80  517844.20   3004622.80
55  517844.20   3004622.80  517842.50   3005545.80  518750.50   3005547.00  518752.30   3004624.00
56  518752.30   3004624.00  518750.50   3005547.00  519658.50   3005548.50  519660.30   3004625.50
57  519660.30   3004625.50  519658.50   3005548.50  520566.50   3005550.00  520568.40   3004627.00
58  520568.40   3004627.00  520566.50   3005550.00  521474.60   3005551.50  521476.30   3004629.00
59  521476.30   3004629.00  521474.60   3005551.50  522383.20   3005553.00  522384.60   3004630.50
60  522384.60   3004630.50  522383.20   3005553.00  523291.30   3005554.80  523292.60   3004632.00
61  523292.60   3004632.00  523291.30   3005554.80  524199.40   3005556.80  524201.20   3004633.80
62  524201.20   3004633.80  524199.40   3005556.80  525107.40   3005558.80  525109.40   3004635.80
63  525109.40   3004635.80  525107.40   3005558.80  526015.40   3005560.80  526017.40   3004637.80
64  526017.40   3004637.80  526015.40   3005560.80  526923.40   3005562.80  526925.40   3004639.80
65  515118.40   3005541.80  515117.40   3006464.80  516025.50   3006466.00  516026.40   3005543.00
66  516026.40   3005543.00  516025.50   3006466.00  516933.30   3006467.50  516934.40   3005544.50
67  516934.40   3005544.50  516933.30   3006467.50  517841.30   3006468.80  517842.50   3005545.80
68  517842.50   3005545.80  517841.30   3006468.80  518749.30   3006470.00  518750.50   3005547.00
69  518750.50   3005547.00  518749.30   3006470.00  519657.30   3006471.50  519658.50   3005548.50
70  519658.50   3005548.50  519657.30   3006471.50  520565.30   3006473.00  520566.50   3005550.00
71  520566.50   3005550.00  520565.30   3006473.00  521473.30   3006474.50  521474.60   3005551.50
72  521474.60   3005551.50  521473.30   3006474.50  522381.40   3006476.00  522383.20   3005553.00
73  522383.20   3005553.00  522381.40   3006476.00  523289.40   3006477.80  523291.30   3005554.80
74  523291.30   3005554.80  523289.40   3006477.80  524197.40   3006479.80  524199.40   3005556.80
75  524199.40   3005556.80  524197.40   3006479.80  525105.40   3006481.80  525107.40   3005558.80
76  516025.50   3006466.00  516024.20   3007389.00  516931.60   3007390.50  516933.30   3006467.50
77  516933.30   3006467.50  516931.60   3007390.50  517839.50   3007391.80  517841.30   3006468.80
78  517841.30   3006468.80  517839.50   3007391.80  518747.50   3007393.00  518749.30   3006470.00
79  518749.30   3006470.00  518747.50   3007393.00  519655.50   3007394.50  519657.30   3006471.50
80  519657.30   3006471.50  519655.50   3007394.50  520563.50   3007396.00  520565.30   3006473.00
81  520565.30   3006473.00  520563.50   3007396.00  521471.50   3007397.50  521473.30   3006474.50
82  521473.30   3006474.50  521471.50   3007397.50  522379.30   3007399.00  522381.40   3006476.00
83  522381.40   3006476.00  522379.30   3007399.00  523287.40   3007400.80  523289.40   3006477.80
84  523289.40   3006477.80  523287.40   3007400.80  524195.40   3007402.80  524197.40   3006479.80
85  516024.20   3007389.00  516022.50   3008312.00  516930.20   3008313.50  516931.60   3007390.50
86  516931.60   3007390.50  516930.20   3008313.50  517838.40   3008314.80  517839.50   3007391.80
87  517839.50   3007391.80  517838.40   3008314.80  518746.50   3008316.00  518747.50   3007393.00
88  518747.50   3007393.00  518746.50   3008316.00  519654.30   3008317.50  519655.50   3007394.50
89  519655.50   3007394.50  519654.30   3008317.50  520562.30   3008319.00  520563.50   3007396.00
90  520563.50   3007396.00  520562.30   3008319.00  521470.30   3008320.50  521471.50   3007397.50
91  521471.50   3007397.50  521470.30   3008320.50  522377.70   3008322.00  522379.30   3007399.00
92  518746.50   3008316.00  518745.10   3009239.00  519652.60   3009240.50  519654.30   3008317.50
93  519654.30   3008317.50  519652.60   3009240.50  520560.50   3009242.00  520562.30   3008319.00
94  520562.30   3008319.00  520560.50   3009242.00  521468.40   3009243.80  521470.30   3008320.50

要创建网格,我需要连接每行的四个角点,然后继续下一行。连接每行上的所有点后,我的网格就完成了。

网格完成后,我想在网格上绘制变量,变量应该适合网格的整个矩形/四边形。为了绘制网格,我有每个网格的质心和要绘制的变量。

SN  XC  YC  ELEV
1   512851.9    3002308.8   1.5
2   513759.9    3002309.8   1.5
3   513758.9    3003232.8   1.5
4   513757.9    3004155.8   1.5
5   514668.1    3002310.8   1.5
6   514666.9    3003233.7   1.5
7   514665.9    3004156.8   1.5
8   515576.6    3002312 1.573
9   515575.2    3003234.8   1.5
10  515574  3004157.9   1.5
11  515572.9    3005080.9   1.5
12  515571.9    3006003.9   1.5
13  516484.9    3002313.5   1.816
14  516483.6    3003236.3   1.524
15  516482.2    3004159.3   1.5
16  516481  3005082.3   1.5
17  516479.9    3006005.3   1.5
18  516478.7    3006928.3   1.5
19  516477.1    3007851.3   1.5
20  517393.2    3002314.7   2.122
21  517391.9    3003237.7   1.857
22  517390.6    3004160.7   1.746
23  517389.2    3005083.7   1.788
24  517387.9    3006006.7   1.778
25  517386.4    3006929.7   1.635
26  517384.9    3007852.7   1.5
27  518301.6    3002315.9   2.492
28  518300.2    3003238.9   2.309
29  518298.9    3004161.9   2.236
30  518297.4    3005084.9   2.239
31  518295.9    3006007.9   2.146
32  518294.4    3006930.9   1.887
33  518293  3007853.9   1.622
34  519209.9    3002317.3   2.831
35  519208.4    3003240.3   2.72
36  519207  3004163.3   2.64
37  519205.4    3005086.3   2.574
38  519203.9    3006009.3   2.429
39  519202.4    3006932.3   2.124
40  519201  3007855.3   1.774
41  519199.6    3008778.3   1.583
42  520118.2    3002318.8   3.079
43  520116.7    3003241.8   2.997
44  520115.1    3004164.8   2.899
45  520113.4    3005087.8   2.797
46  520111.9    3006010.8   2.634
47  520110.4    3006933.8   2.275
48  520108.9    3007856.8   1.795
49  520107.4    3008779.8   1.637
50  521026.5    3002320.4   3.244
51  521024.9    3003243.4   3.158
52  521023.3    3004166.5   3.05
53  521021.5    3005089.4   2.93
54  521019.9    3006012.3   2.723
55  521018.4    3006935.3   2.244
56  521016.9    3007858.3   1.648
57  521015.4    3008781.3   1.637
58  521934.7    3002322.2   3.348
59  521933.1    3003245.2   3.244
60  521931.4    3004168.2   3.111
61  521929.7    3005091 2.94
62  521928.1    3006013.8   2.655
63  521926.4    3006936.8   2.15
64  521924.7    3007859.8   1.5
65  522843.1    3002323.8   3.395
66  522841.4    3003246.8   3.259
67  522839.5    3004169.8   3.066
68  522837.9    3005092.6   2.799
69  522836.3    3006015.4   2.434
70  522834.4    3006938.4   2.06
71  523751.4    3002325.4   3.365
72  523749.5    3003248.4   3.179
73  523747.7    3004171.4   2.906
74  523746.1    3005094.4   2.555
75  523744.4    3006017.3   2.185
76  523742.4    3006940.3   1.851
77  524659.7    3002327.3   3.187
78  524657.9    3003250.3   2.946
79  524656.2    3004173.3   2.613
80  524654.4    3005096.3   2.284
81  524652.4    3006019.3   2
82  525568.2    3002329.3   2.834
83  525566.4    3003252.3   2.532
84  525564.4    3004175.3   2.128
85  525562.4    3005098.3   1.82
86  526476.4    3002331.3   2.343
87  526474.4    3003254.3   1.977
88  526472.4    3004177.3   1.562
89  526470.4    3005100.3   1.5
90  527384.5    3002333.3   1.753
91  527382.4    3003256.3   1.5
92  527380.4    3004179.3   1.5
93  528292.9    3002335.4   1.5
94  528290.7    3003258.4   1.5

在上述数据集中,ELEV是高程,是要在网格上绘制的变量。因此,我们的想法是在绘制网格时,应为每个网格分配一个名称。因此,稍后可以参考绘制变量相同的名称/数字来绘制变量。

我需要的输出如下:

enter image description here

注意:网格大小仅在此样本数据集上是常量。我正在处理的数据集非常庞大,包含不规则网格。 任何绘图包/解决方案均可接受。

1 个答案:

答案 0 :(得分:1)

这不是最终的解决方案(肯定是半解决方案)但我认为这是一个好的开始。我正在使用ggplot2。主要是我使用geom_polygon创建网格,并通过添加新的EELV颜色层来填充它。

  1. 首先,我正在阅读您的数据。 grid.dd <- read.table(text="SN X1 Y1 X2...",header=TRUE)eelv.d <- read.table(text="SN XC YC ELEV...",header=TRUE)
  2. 然后我正在重塑数据。
  3. 我绘制网格
  4. 这是我的R代码:

    ## add new id to identifiy polygon, each row is a polygon
    grid.dd$id <- rownames(grid.dd)
    ## put the data in the long format since geom_polygon 
    ## needs aes(x,y,group) using reshape
    dd.long <- reshape(grid.dd, dir='long', varying=list(c(1,3,5,7),
                                          c(2,4,6,8)), v.names=c('X', 'Y'),times =1:4)
    ## attempt to use merge but this gives a strange result
    ## dat <- merge(dd.long,eelv.d,by.x='id',by.y='SN')
    
    library(ggplot2)
    ggplot(dd.long, aes(x=X, y=Y)) + 
      geom_polygon(aes( group=factor(id)),fill='transparent',col='black')+
      geom_point(data=eelv.d,aes(x=XC,y=YC,col=ELEV),size=10)+
      scale_color_gradientn(colours = rainbow(10))+
      theme_bw()
    

    enter image description here