Bearing comparison and ocean current velocity

I have two bearing and speed data matrices 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 would like to draw the direction of the current bearings with arrows, and the magnitude / speed of the current is represented by the length of the arrow, something like these cards:

enter image description here Wind stress index from Shankar et al. 2002

I know that a package ocecan do something similar, but it, in particular, works with different types of oceanographic data than the matrices / data frames that I use.

Does anyone know how to do this? I got to the raster objects using a function raster()from the library raster:

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 would do this with basic R graphics or with a package that plays well with basic R graphics (like not ggplot2or lattice).

:

, ., , .. , .. (2002). . , 52: 62-120. doi: 10.1016/S0079-6611 (02) 00024-1

+4
2

R:

plot(bearing.rst)  # your base map, I use this because I didn't have it

:

arr.coor <- rasterToPoints(bearing.rst)
arr.coor <- cbind(arr.coor[,-3], bearing=c(t(bearings)), speed=c(t(speed)))

:

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)

:

arrows(arr.coor[,1],arr.coor[,2],arr.coor[,5],arr.coor[,6])

enter image description here

, ggplot2. , .

+4

ggplot

bearings <- 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)


speed <- 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)



df <- data.frame(x = rep(1:5,5), 
                 y = rep(1:5, each = 5),
                 bearings = bearings,
                 speed = speed)

df$dx <- sin((df$bearings)/360*pi*2)*df$speed
df$dy <- cos((df$bearings)/360*pi*2)*df$speed

ggplot(df, aes(x, y)) +
  geom_segment(aes(xend = x + dx, yend = y + dy),
               arrow = arrow(length = unit(0.1,"cm"))) 
+1

Source: https://habr.com/ru/post/1690550/


All Articles