R中的箭头/箭头图

时间:2014-05-28 22:14:55

标签: r plot velocity

我是R的新手,在Windows 7上使用3.0.2版本。我试图绘制洋流的速度(速度和方向),更长的箭头意味着更强的速度。

我尝试使用ggplot中的箭头功能执行此操作,如本文中所述:How to map wind direction and speed (velocity plot) with R

然而,似乎这只能为所有人绘制相同长度的箭头,所以它只会提供有关方向的信息,而不是速度(除非我被误解?)。 ggplot似乎也很难使用ncdf格式的数据。

所以我尝试了其他一些事情,到目前为止还没有成功。我认为箭袋函数(包pracma)应该能够做我想做的事情,但我似乎无法弄清楚如何使用它。

我的数据是网格化的ncdf文件,其中包含以下维度:

 "file data_grid.nc has 2 dimensions:"
 "lat   Size: 61"
 "lon   Size: 121"
 "------------------------"
 "file data_grid.nc has 2 variables:"
 "double U[lat,lon]  Longname:U Missval:1e+30"
 "double V[lat,lon]  Longname:V Missval:1e+30"

抱歉,由于我的数据文件非常大,我无法提供可重现的示例。但我基本上有纬度和经度值以及相应的纬向(西向东)速度(U)和经向(从北向南)速度(V)。

我尝试过(在其他一些事情中):     quiver(lon, lat, U, V) 这给了我错误: Error in x + scale * u : non-conformable arrays

正如上面链接中的帖子一样,我认为U和V是错误的并且也需要是坐标,但老实说,我不理解包描述中给出的解释(u,v:x ,起点的y坐标。)

知道我可能会出错吗?

有关编码和格式的更多信息(*于5月30日添加): Argo <- open.ncdf("data_grid.nc") print.ncdf(Argo) lon = get.var.ncdf (Argo, "lon") # length =121 lat = get.var.ncdf (Argo, "lat") # length =61 U = get.var.ncdf (Argo, "U") #dim (U) = lat lon V = get.var.ncdf (Argo, "V") #dim(V) = lat lon U = t(U) #dim (U) = lon lat V = t(V) #dim (U) = lon lat

`dput(head(lon))` = `structure(c(150, 150.5, 151, 151.5, 152, 152.5), .Dim = 6L)`
`dput(head(lat))` = `structure(c(-30, -29.5, -29, -28.5, -28, -27.5), .Dim = 6L)`
`dput(head(U))`   = `structure(c(-4.329999924, -4.329999924, -4.329999924, -4.329999924, 

-4.329999924,-4.329999924,-4.329999924,-4.329999924,-4.329999924, -4.329999924,-4.329999924,-4.329999924,-4.329999924,-4.329999924, -4.329999924,-4.329999924,-4.329999924,-3.400000095,-3.400000095, -3.400000095,-3.400000095,-3.400000095,-3.400000095,0.129999995, 0.129999995,0.129999995,0.129999995,0.129999995,0.129999995, 0.129999995,0.25,0.25,0.25,0.25,0.25,0.25,0.100000001, 0.100000001,0.100000001,0.100000001,0.100000001,0.100000001, -0.119999997,-0.119999997,-0.119999997,-0.119999997,-0.119999997, -0.119999997,-3.599999905,-3.599999905,-0.839999974,-0.839999974, 2.680000067,2.668006767,1.24000001,-4.110000134,-3.599999905, -3.599999905,-0.839999974,-0.839999974,1.24000001,1.24000001, 1.24000001,-4.110000134,-4.110000134,-3.599999905,1.24000001, 1.24000001,1.24000001,1.24000001,1.24000001,1.24000001,1.24000001, 1.24000001,1.24000001,1.24000001,1.24000001,1.24000001,1.24000001, 1.24000001,1.24000001,1.24000001,1.24000001,1.24000001,1.24000001, 1.24000001,1.24000001,1.24000001,1.24000001,1.955000048, 1.24000001,1.24000001,1.24000001,1.24000001,1.955000048, 1.950000048,-3.650000095,-3.190000057,-2.279999971,-2.279999971, -2.279999971,-0.180000007,-3.650000095,-3.190000057,-3.190000057, -2.279999971,-2.279999971,-2.279999971,-3.650000095,-3.190000057, -3.190000057,0.879999995,-2.279999971,-2.279999971,-3.650000095, -3.190000057,-3.190000057,4.800000191,0.879999995,0.879999995, -3.650000095,-3.190000057,-3.190000057,4.800000191,4.800000191, 0.879999995,-3.650000095,-3.190000057,-3.190000057,4.800000191, -2.980000019,-5.210000038,-3.650000095,-7.320000172,-3.190000057, 4.800000191,-2.980000019,-3.859999895,-6.599999905,-1.75999999, -3.970000029,-5.820000172,-2.980000019,-3.859999895,-6.599999905, -5.75,-4.039999962,-4.25,-3.869999886,-5.980000019,-5.75, -5.75,-4.039999962,-5.380000114,-2.549999952,-2.880000114, -5.75,-5.75,-5.380000114,-1.070000052,-1.070000052,-0.419999987, -5.820000172,-7.03000021,-3.650000095,-1.070000052,-9.68999958, -9.68999958,-5.820000172,-7.03000021,-3.650000095,-5.489999771, -3.779999971,2.230000019,-5.820000172,-7.03000021,-3.650000095, -5.489999771,-3.779999971,-1.379999995,-5.820000172,-7.03000021, -7.179999828,-5.380000114,-7.5,-6.769999981,-4.349999905, -2.539999962,-3.559999943,-9.090000153,-4.480000019,-4.559999943, -8.279999733,5.21999979,-9.039999962,-2.369999886,-2.5,-4.139999866, -1.679999948,7.1000000076,0.419999987,-2.789999962,-5.190000057, -7.570000172,3.769999981,3.769999981,0.419999987,1.7220000029, -0.959999979,6.289999962,-1.669999957,8.489999771,11.25, -5.420000076,-5.420000076,6.289999962,-6.130000114,-6.130000114, 11.25,14.69999981,12.14999962,14.40999985,0.839999974,0.839999974, -4.480000019,-14.76000023,-14.76000023,-1.230000019,11.61999989, 11.61999989,10.61999989,-14.76000023,-14.76000023,-8.479999542, 11.61999989,11.61999989,10.61999989,2.970000029,-3.170000076, -8.479999542,11.61999989,-0.01,-3.450000048,-3.450000048, -5.289999962,-4.619999886,-0.01,-0.01,-0.01,-0.01,0.109999999, 0.529999971,-0.400000006,1.7100000038,1.7100000038,1.7100000038, -0.360000014,0.930099998,-0.400000006,-0.400000006,-0.400000006, 0.829999983,17.43000031,8.220000267,-16.95999908,14.64000034, -0.400000006,9.770000458,2.4100000086,-6.5,-16.95999908,8.130000114, 14.64000034,-10.48999977,-14.42000008,-9.680000305,-4.110000134, -1.480000019,-0.829999983,-21.45000076,6.9400000057,-16.37000084, 22.75,22.75,11.73999977,15.78999996,-8.840000153,5.090000153, 22.75,22.75,0.629999995,5.769999981,5.429999828,-1.25,22.75, 22.75,0.629999995,5.769999981,5.429999828,-1.25,22.75,0.629999995, 5.769999981,5.769999981,5.429999828,5.429999828,22.75,5.769999981, 5.769999981,5.429999828,5.429999828,-4.840000153,2.099999905, 4.010000229,11.78999996,11.78999996,3.210000038,-7.110000134, 2.099999905,4.0110000229,11.78999996,11.78999996,3.210000038, 3.210000038,2.099999905,4.0110000229,12.85999966,11.78999996, 10.14999962,-3.180000067,2.099999905,4.010000229,12.85999966, 11.78999996,10.14999962,1.429999948,2.099999905,4.010000229, 6.809999943,3.539999962,-5.090000153,6.360000134,1.779999971, 7.309999943,-9.149999619,1.419999957,-1.49000001,-0.980000019, -0.579999983,-1.25999999,-14.05000019,8.220000267,0.200000003, 0.159999996,4.019999981,6.570000172,7.989999771,-6.239999771, 0.899999976,0.829999983,-1,-3.630000114,-0.889999986,-12.05000019, 15.03999996,5.849999905),. Dim = c(6L,61L))`

`dput(head(V))` =`structure(c(-20.85000038, -20.85000038, -20.85000038, -20.85000038, 

-20.85000038,-20.85000038,-20.85000038,-20.85000038,-20.85000038, -20.85000038,-20.85000038,-20.85000038,-20.85000038,-20.85000038, -20.85000038,-20.85000038,-20.85000038,-18.28000069,-18.28000069, -18.28000069,-18.28000069,-18.28000069,-18.28000069,-2.059999943, -2.059999943,-2.059999943,-2.059999943,-2.059999943,-2.059999943, -2.059999943,-7.460000038,-7.460000038,-7.460000038,-7.460000038, -7.460000038,-7.460000038,-3.829999924,-3.829999924,-3.829999924, -3.829999924,-3.829999924,-3.829999924,3.7100000038,3.7100000038, 3.710000038,3.7100000038,3.7100000038,3.7100000038,9.899999619, 9.899999619,1.710000038,1.7100000038,-20.96999931,-20.96999931, 1.820000052,11.43999958,9.899999619,9.899999619,17110000038, 1.710000038,1.820000052,1.820000052,1.820000052,11.43999958, 11.43999958,9.899999619,1.820000052,1.820000052,1.820000052, 1.820000052,1.820000052,1.820000052,1.820000052,1.820000052, 1.820000052,1.820000052,1.820000052,1.820000052,1.820000052, 1.820000052,1.820000052,1.820000052,1.820000052,1.820000052, 1.820000052,1.820000052,1.820000052,1.820000052,1.820000052, 2.630000114,1.820000052,1.820000052,1.820000052,1.820000052, 2.630000114,2.630000114,-0.310000002,0.039999999,1.019999981, 1.019999981,1.019999981,0.460000008,-0.310000002,0.039999999, 0.039999999,1.01999981,1.01999981,1.019999981,-0.310000002, 0.039999999,0.039999999,-6.039999962,1.01999981,1.01999981, -0.310000002,0.039999999,0.039999999,2.380000114,-6.039999962, -6.039999962,-0.310000002,0.039999999,0.039999999,2.380000114, 2.380000114,-6.039999962,-0.310000002,0.039999999,0.039999999, 2.380000114,-0.600000024,1.090000033,-0.310000002,2.369999886, 0.039999999,2.380000114,-0.600000024,1.266699981,0.289999992, 1,-0.620000005,-1.429999948,-0.600000024,1.266699981,0.289999992, 0.639999986,-0.430000007,-1.480000019,-1.159999967,-4.860000134, 0.639999986,0.639999986,-0.430000007,-8.329999924,-5.079999924, 2.809999943,0.639999986,0.639999986,-8.329999924,6.210000038, 6.210000038,-4.679999828,1.32320052,2.109999895,1,6.210000038, 5.960000038,5.9600000038,1.32320052,2.109999895,1,1.620000005, 2.029999971,-0.200000003,1.32320052,2.109999895,1,1.620000005, 2.029999971,1.389999986,1.32320052,2.109999895,4.400000095, 0.050000001,2.720000029,4.110000134,2.309999943,1.850000024, -0.029999999,-1.830000043,1.700000048,-2.930000067,0.01, 2.839999914,3.00999999,-0.529999971,-0.330000013,-1.409999967, 2.430000067,-2.650000095,1.080000043,-0.050000001,-0.239999995, -0.439999998,5.53000021,5.53000021,1.080000043,5,5 -9.5000000153, 9.93999958,-7.989999771,9.210000038,9.789999962,-16.19000053, -16.19000053,9.93999958,-13.01000023,-13.01000023,9.789999962, 3.289999962,1.24000001,-6.099999905,7.699999809,7.699999809, 0.029999999,0.180000007,0.180000007,4.449999809,-3.539999962, -3.539999962,-3.799999952,0.180000007,0.180000007,1.019999981, -3.539999962,-3.539999962,-3.799999952,1.7300000019,-1.00999999, 1.019999981,-3.539999962,3.9800000019,-1.159999967,-1.159999967, 3.890000105,2.460000038,3.9800000019,3.9800000019,3.9800000019, 3.980000019,-0.870000005,0.50999999,-1.080000043,0.9300007, 0.930000007,0.9300007,3.299999952,-1.470000029,-1.080000043, -1.080000043,-1.080000043,-5.800000191,-11.10999966,-4.320000172, -5.239999771,-3.579999924,-1.080000043,-1.789999962,-4.179999828, -5.409999847,-5.239999771,-10.81000042,-3.579999924,11.25, -6.46999979,-3.369999886,0.860000014,4.610000134,8.699999809, 5.78000021,-4.829999924,9.789999962,5.420000076,5.420000076, 5.28000021,-5.150000095,10.21000004,4.309999943,5.420000076, 5.420000076,0.079999998,3.390000105,1.470000029,-5.699999809, 5.420000076,5.420000076,0.079999998,3.390000105,1.470000029, -5.699999809,5.420000076,0.079999998,3.390000105,3.390000105, 1.470000029,1.470000029,5.420000076,3.390000105,3.390000105, 1.470000029,1.470000029,-6.989999771,0,0.810000002,-4.539999962, -4.539999962,0.529999971,0.629999995,0,0.810000002,-4.539999962, -4.539999962,0.529999971,0.529999971,0,0.810000002,1.299999952, -4.539999962,-3.059999943,1.649999976,0,0.810000002,1.299999952, -4.539999962,-3.059999943,0.955099979,0,0.810000002,-3.230000019, 0.379999995,4.71999979,0.800000012,-4.909999847,-3.75,1.99000001, -4.139999866,2.799999952,5.139999866,-3,-1.25999999,12.77999973, -5.489999771,6.840000153,0.889999986,-0.07,4.619999886,-8.670000076, 4.639999866,-2.569999933,-2.380000114,0.170000002,-2.190000057, 3.930000067,-0.680000007,-0.460000008,-4.639999866),. Dim = c(6L, 61L))`

1 个答案:

答案 0 :(得分:0)

所以我假设length(lat)==61length(lon)=121 dim(U)=c(61,121)dim(V)=c(61,121)。如果是这样,那些与绘图的长度不兼容,但您可以重复latlon值足以匹配。这是使用基本图形绘制数据的一种方法(这里我将使用arrows而不是quiver,因为这基本上就是函数所做的一切)

lat=seq(-10,10,length.out=20)
lon=seq(-10,10,length.out=60)
u=matrix(runif(length(lat)*length(lon),-2,2),nrow=length(lat))
v=matrix(runif(length(lat)*length(lon),-2,2),nrow=length(lat))


scale<-1
plot(0,0,xlim=c(-10,10), ylim=c(-10,10), type="n")
arrows(lat[row(u)], lon[col(u)], 
    lat[row(u)] + scale * u, lon[col(u)] + scale * v, 
    angle = 10, length=.12)

现在我已经把矩阵做得更小了,所以它并不像一团糟。但length=angle=设置会影响箭头的绘制方式。您通过的坐标确定轴的长度

resulting image