I have two matrices of bearing and speed data for ocean currents
bearings <- matrix(data = c(170.0833, 175.6863, 182.3538, 180.3335, 170.8965,
178.3276, 182.3693, 187.2196, 182.3533, 168.3498,
189.1664, 187.6813, 187.0393, 180.2259, 166.8412,
193.4223, 188.5367, 182.4128, 175.2626, 167.3058,
192.2930, 185.5073, 175.0302, 168.6284, 167.8392),
ncol = 5, nrow = 5, byrow = F)
speed <- matrix(data = c(0.1389173, 0.1585099, 0.1796583, 0.2021887, 0.2117295,
0.1196745, 0.1463118, 0.1637266, 0.1730471, 0.1804999,
0.1309982, 0.1546123, 0.1593298, 0.1517513, 0.1550037,
0.1621728, 0.1694083, 0.1606560, 0.1459710, 0.1457233,
0.1659898, 0.1535861, 0.1396885, 0.1294339, 0.1337756),
ncol = 5, nrow = 5, byrow = F)
I wanted to graph the direction of the current bearings with arrows, while the magnitude/speed of the current is represented by the length of the arrow, a bit something like these maps:
Wind stress figure taken from Shankar et al. 2002
I know the package oce
may be able to do something like that, but it specifically works with different types of oceanographic data rather than matrices/data frames that I'm using.
Anyone happen to know how to do that? I have gotten as far as making them into raster objects using the raster()
function from the raster
library:
library(raster)
bearing.rst <- raster(bearings,
xmn = 66,
xmx = 67.3333,
ymn = 10.6667,
ymx = 12)
speed.rst <- raster(speed,
xmn = 66,
xmx = 67.3333,
ymn = 10.6667,
ymx = 12)
Ideally I'd do this with base R graphics, or with a package that plays nice with base R graphics (e.g. not ggplot2
or lattice
).
Graph from:
Shankar, D., Vinayachandra, P.N., & Unnikrishnan, A.S. (2002). The monsoon currents in the north Indian Ocean. Progress in Oceanography, 52: 62-120. doi: 10.1016/S0079-6611(02)00024-1
with base R:
plot(bearing.rst) # your base map, I use this because I didn't have it
Get your starting coordinates:
arr.coor <- rasterToPoints(bearing.rst)
arr.coor <- cbind(arr.coor[,-3], bearing=c(t(bearings)), speed=c(t(speed)))
Calculate your finishing coordinates with trigonometric functions:
x1 <- arr.coor[,1] + arr.coor[,4] * cos(arr.coor[,3]*pi/180)
y1 <- arr.coor[,2] + arr.coor[,4] * sin(arr.coor[,3]*pi/180)
arr.coor <- cbind(arr.coor, x1, y1)
Plot your arrows:
arrows(arr.coor[,1],arr.coor[,2],arr.coor[,5],arr.coor[,6])
I guess the same principal could work with ggplot2. The idea is to get a table with all your arrows origin and end.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With