【问题标题】:R Plotting swell direction arrows with ggplot2 geom_spokeR用ggplot2 geom_spoke绘制膨胀方向箭头
【发布时间】:2017-03-12 14:14:08
【问题描述】:

我正在尝试绘制膨胀方向(md$Swell.Dir 和高度(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")

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

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

根据geom_spoke包;

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

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

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

这是 geom_spoke 函数的输出。

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

感谢您的帮助。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

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

    您需要修改一些内容。

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

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

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

    library(ggplot2)
    
    md <- 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"), 
                     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))
    
    md$newdir <- ((-md$Swell.Dir+90)/360)*2*pi
    
    ggplot(data = md, aes(x=UTC, y=m)) + 
      geom_point() + 
      geom_spoke(aes(angle=newdir), radius=0.05 )
    

    如果您愿意替代geom_spoke(),则可以使用geom_text() 和ANSI(unicode)箭头符号 来获得所需的输出:

    ggplot(data = md, aes(x=UTC, y=m)) + 
      geom_text(aes(angle=-Swell.Dir+90), label="→")
    

    【讨论】:

    • 嗨,Brian,感谢您的帮助,我会试一试。我真的很惊讶 R 中没有箭头绘图功能,其中可以绘制固定长度和给定角度的箭头,而不必使用坐标。
    • 我所看到的问题是 geom_spoke & geom_segment 需要 x,y,xend,yend 参数来绘制线段。使用上述数据,我们将时间序列和米之间的单位混合在一起。 x & xend 以时间单位表示,y &y end 以米为单位。我认为这是因为比例不正确而搞砸了功能。因此,箭头的实际方向可能不正确。我想要的是一个箭头,我可以在其中使用向量作为中心点值,并使用角度向量来指定角度点的方向。这将解决缩放问题。
    • geom_text 和箭头的“技巧”真的很巧妙!时间和 y 变量的不同单位没有问题。
    【解决方案2】:

    如果您有指南针度数,因此它从北方开始,方向顺时针,从度数到半径的转换不起作用。要将其转换为半径,您需要这样做:

    degree_bearing <- (450 - bearing) %% 360 
    radius <-  degree_bearing* pi / 180
    

    那么您将获得正确的半径。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-22
      • 1970-01-01
      • 2017-09-17
      • 2020-03-07
      • 1970-01-01
      相关资源
      最近更新 更多