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

时间:2017-03-12 14:14:08

标签: r ggplot2

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

WindDirection&WaveHeight

以下是使用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包;

  

&#34;这是geom_segment的极坐标参数化。当你拥有描述方向和距离的&gt;变量时,这很有用。&#34;

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

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

这是geom_spoke函数的输出。

geom_spoke output

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

感谢您的帮助。

2 个答案:

答案 0 :(得分: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轴的定义单位与函数的数学假设相同,则效果最佳。

  3. 如果您真的想使用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="→")
    

答案 1 :(得分:0)

如果您有指南针度数,那么它是从北方开始的,并且方向是顺时针方向,则从度数到半径的转换无效。要将其转换为半径,您需要执行以下操作:

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

然后您将拥有正确的半径。