R 使用 ggplot2 geom_spoke 绘制膨胀方向箭头

red*_*981 5 r ggplot2

我正在尝试绘制膨胀方向(md$Swell.Dir 和 height(md$m) 以便结果是类似于下面的图形;

风向&波高

这是我使用 dput 的数据的一个小样本;

md <- structure(list(UTC = structure(1:6, .Label = c("01-Feb-17 1200", 
"01-Feb-17 1500", "01-Feb-17 1800", "01-Feb-17 2100", "02-Feb-17 0000", 
"02-Feb-17 0300", "02-Feb-17 0600", "02-Feb-17 0900", "02-Feb-17 1200", 
"02-Feb-17 1500", "02-Feb-17 1800", "02-Feb-17 2100", "03-Feb-17 0000", 
"03-Feb-17 0300", "03-Feb-17 0600", "03-Feb-17 0900", "03-Feb-17 1200", 
"03-Feb-17 1500", "03-Feb-17 1800", "03-Feb-17 2100", "04-Feb-17 0000", 
"04-Feb-17 0300", "04-Feb-17 0600", "04-Feb-17 0900", "04-Feb-17 1200", 
"04-Feb-17 1500", "04-Feb-17 1800", "04-Feb-17 2100", "05-Feb-17 0000", 
"05-Feb-17 0300", "05-Feb-17 0600", "05-Feb-17 0900", "05-Feb-17 1200", 
"05-Feb-17 1500", "05-Feb-17 1800", "05-Feb-17 2100", "06-Feb-17 0000", 
"06-Feb-17 0300", "06-Feb-17 0600", "06-Feb-17 0900", "06-Feb-17 1200", 
"06-Feb-17 1500", "06-Feb-17 1800", "06-Feb-17 2100", "07-Feb-17 0000", 
"07-Feb-17 0300", "07-Feb-17 0600", "07-Feb-17 0900", "07-Feb-17 1200", 
"07-Feb-17 1500", "07-Feb-17 1800", "07-Feb-17 2100", "08-Feb-17 0000", 
"08-Feb-17 0300", "08-Feb-17 0600", "08-Feb-17 0900", "08-Feb-17 1200", 
"08-Feb-17 1500", "08-Feb-17 1800", "08-Feb-17 2100", "09-Feb-17 0000"
), class = "factor"), SigWave.m = c(1.7, 1.7, 1.6, 1.6, 1.7, 
1.8), WindWave.Dir = c(140L, 141L, 142L, 180L, 150L, 150L), Metres = c(1.7, 
1.7, 1.6, 1.6, 1.7, 1.7), WindWave.s = c(5.8, 5.7, 5.7, 5.5, 
5.4, 5.4), Swell.Dir = c(17L, 18L, 24L, 11L, 12L, 12L), m = c(0.5, 
0.5, 0.5, 0.3, 0.2, 0.2), Wave1.Dir = c(137L, 137L, 137L, 137L, 
141L, 143L)), .Names = c("UTC", "SigWave.m", "WindWave.Dir", 
"Metres", "WindWave.s", "Swell.Dir", "m", "Wave1.Dir"), row.names = c(NA, 
6L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)

首先,我将日期更改为正确的格式;

md$UTC <-as.POSIXct(md$UTC, format = "%d-%b-%y %H%M")
Run Code Online (Sandbox Code Playgroud)

根据 geom_spoke 包;

“这是 geom_segment 的极坐标参数化。当你有描述方向和距离的 > 变量时,它很有用。”

由于我的膨胀方向是极化的,我相信 geom_spoke 是使用正确的 geom 函数。然而,下面的代码没有给我想要的结果,而且辐条没有极化方向。

ggplot(data = md) + geom_spoke(mapping = aes(x = UTC, y = m, angle = Swell.Dir, radius = 0.5 ))
Run Code Online (Sandbox Code Playgroud)

这是 geom_spoke 函数的输出。

geom_spoke 输出

我在此处此处此处的这些链接上阅读了类似的问题,但尝试建议的解决方案并没有给我想要的结果。我认为我的问题可能在于 ggplot 如何计算 geom_segment 函数的 xend 参数,但我找到了解决这个问题的方法。

谢谢你的帮助。

Bri*_*n D 4

看看这里的例子:http ://docs.ggplot2.org/current/geom_spoke.html

\n\n

您需要修改一些内容。

\n\n
    \n
  1. 你如何定义角度。
    \n我认为现在的角度是以度为单位定义的,其中 0 度表示垂直向上(北)。但是,geom_spoke()代码使用sin()cos()函数,其中角度以弧度定义。
    \n根据示例,直接向右(东)是 0 或 2*pi,向上(北)是 pi/2,向左(西)是 pi,向下(南)是 pi*1.5,等等。所以你需要将您的学位转换为适当的单位。(即((-度+90)/360)*2*pi)

  2. \n
  3. 如何定义半径。
    \n 半径的显示方式取决于 x 轴和 y 轴的单位。请参阅:https ://github.com/tidyverse/ggplot2/blob/master/R/geom-spoke.r 如果 x 轴和 y 轴都以相同的单位定义,那么效果最好,因为这是假设的函数的数学。

  4. \n
\n\n

如果你真的想使用geom_spoke(),那么以下代码将帮助您完成大部分工作,但要知道,在将两个轴转换为相同单位之前,角度不会准确:

\n\n
library(ggplot2)\n\nmd <- data.frame(UTC = c("01-Feb-17 1200", "01-Feb-17 1500", "01-Feb-17 1800", "01-Feb-17 2100", "02-Feb-17 0000", "02-Feb-17 0300"), \n                 SigWave.m = c(1.7, 1.7, 1.6, 1.6, 1.7, 1.8), \n                 WindWave.Dir = c(140L, 141L, 142L, 180L, 150L, 150L), \n                 Metres = c(1.7, 1.7, 1.6, 1.6, 1.7, 1.7), \n                 WindWave.s = c(5.8, 5.7, 5.7, 5.5, 5.4, 5.4), \n                 Swell.Dir = c(17L, 18L, 24L, 11L, 12L, 12L), \n                 m = c(0.5, 0.5, 0.5, 0.3, 0.2, 0.2), \n                 Wave1.Dir = c(137L, 137L, 137L, 137L, 141L, 143L))\n\nmd$newdir <- ((-md$Swell.Dir+90)/360)*2*pi\n\nggplot(data = md, aes(x=UTC, y=m)) + \n  geom_point() + \n  geom_spoke(aes(angle=newdir), radius=0.05 )\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n\n

如果您愿意接受 的替代方案,那么您可以与 ANSI (unicode) 箭头符号一起geom_spoke()使用geom_text()\xe2\x86\x92来获得所需的输出:

\n\n
ggplot(data = md, aes(x=UTC, y=m)) + \n  geom_text(aes(angle=-Swell.Dir+90), label="\xe2\x86\x92")\n
Run Code Online (Sandbox Code Playgroud)\n