R使用ggplot2 geom_辐条绘制涌浪方向箭头
我试图绘制涌浪方向(md$sweel.Dir)和高度(md$m),以便得到一个类似于下图的图形 下面是我使用dput的一个小样本数据R使用ggplot2 geom_辐条绘制涌浪方向箭头,r,ggplot2,R,Ggplot2,我试图绘制涌浪方向(md$sweel.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"
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")
这是geom_spoke函数的输出
我在这些链接上读到过类似的问题,但尝试建议的解决方案并没有给我预期的结果。我认为我的问题可能在于ggplot如何计算geom_段函数的xend-yend参数,但我找到了一种解决方法
感谢您的帮助。请看下面的示例: 您需要修改一些内容
我假设现在的角度是以度为单位定义的,其中0度是直线(北)。然而,
geom_spoke()
code使用了sin()
和cos()
函数,其中角度是以弧度为单位定义的。根据该示例,向右(东)直线为0或2*pi,向上(北)直线为pi/2,向左(西)直线为pi,向下(南)直线为pi*1.5,等等。因此,您需要将度数转换为适当的单位。(即(((-degrees+90)/360)*2*pi
半径的显示方式取决于x轴和y轴的单位。请参阅:如果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 )
如果你有罗盘度数,那么它从北方开始,方向是顺时针的,从度数到半径的转换不起作用。要将其转换为半径,你需要执行以下操作:
degree_bearing <- (450 - bearing) %% 360
radius <- degree_bearing* pi / 180
degree\u轴承嗨,Brian,谢谢你的帮助,我将尝试一下。我真的很惊讶R中没有箭头绘图功能,在R中,箭头可以以固定长度和给定角度绘制,而无需使用坐标。从我看到的问题是,geom\u辐条和geom\u段需要x、y、xend、yend ARG来绘制seg根据以上数据,我们混合了时间序列和米之间的单位。x和xend是时间单位,y和y end是米。我认为这是因为比例不正确,导致函数出错。因此,箭头的实际方向可能不正确。我想要的是一个可以使用向量的箭头对于中心点值,使用角度向量指定角度点的方向。这将解决缩放问题。
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 )
ggplot(data = md, aes(x=UTC, y=m)) +
geom_text(aes(angle=-Swell.Dir+90), label="→")
degree_bearing <- (450 - bearing) %% 360
radius <- degree_bearing* pi / 180