棒图在ggplot中获取风速和风向数据

编程入门 行业动态 更新时间:2024-10-11 09:19:21
本文介绍了棒图在ggplot中获取风速和风向数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想绘制类似于这样的风速/风向数据的简图:

我在出色的oce包中找到了一个不错的情节,但是我想使用ggplot(或plotley)制作同样的情节.

WindSpeed< -c(1,2,3,5,7,2,3,4,5,6,7,8)WindDir< -c(180,90,320,200,350,10,270,50,9,100,110,129)TimeStamp< -c("2018-01-02 01:00","2018-01-02 02:00","2018-01-02 03:00","2018-01-02 04:00","2018-01-02 05:00," 2018-01-02 06:00," 2018-01-02 07:00," 2018-01-02 08:00," 2018-01-02 09:00," 2018-01-02 10:00," 2018-01-02 11:00," 2018-01-02 12:00)DF< -data.frame(TimeStamp,WindSpeed,WindDir)

上面的风向数据是以罗盘方向单位(风从罗盘方向吹来的)为单位,因此,我希望180数据指向正上方,而270数据指向右指向右侧(又名从西方吹向东方).

解决方案

Jon Spring的代码有效.我将验证(数据略有不同),并用他的代码显示输出.

数据略有不同,使用不同的 WindDir 突出显示对角线(和30s):

WindSpeed< -c(1,2,3,5,7,2,3,4,5,6,7,8)WindDir<-c(0,30,45,60,90,120,135,150,180,225,270,315)TimeStamp< -c("2018-01-02 01:00","2018-01-02 02:00","2018-01-02 03:00","2018-01-02 04:00","2018-01-02 05:00," 2018-01-02 06:00," 2018-01-02 07:00," 2018-01-02 08:00," 2018-01-02 09:00," 2018-01-02 10:00," 2018-01-02 11:00," 2018-01-02 12:00)DF< -data.frame(TimeStamp,WindSpeed,WindDir)DF $ TimeStamp<-as.POSIXct(DF $ TimeStamp)

为了更加清晰起见,我增加了Jon的箭头结尾,起点和颜色(分组为naveium)的代码.

ggplot(DF)+geom_segment(aes(x = TimeStamp,y = 0,xend = TimeStamp + lubridate :: dhours(WindSpeed * 1 * -cos((90-WindDir)/360 * 2 * pi)),yend = WindSpeed * 1 * -sin((90-WindDir)/360 * 2 * pi),col = factor(TimeStamp)),箭头=箭头(长度=单位(0.5,"cm")))+geom_point(aes(TimeStamp,0),size = 1)+coord_fixed(3600)+主题(legend.position =无")

我认为这清楚地表明45年代表现良好,特别是最后一条击中了网格线.

使用您的数据和此代码:

WindSpeed< -c(1,2,3,5,7,2,3,4,5,6,7,8)WindDir< -c(180,90,320,200,350,10,270,50,9,100,110,129)TimeStamp< -c("2018-01-02 01:00","2018-01-02 02:00","2018-01-02 03:00","2018-01-02 04:00","2018-01-02 05:00," 2018-01-02 06:00," 2018-01-02 07:00," 2018-01-02 08:00," 2018-01-02 09:00," 2018-01-02 10:00," 2018-01-02 11:00," 2018-01-02 12:00)DF< -data.frame(TimeStamp,WindSpeed,WindDir)DF $ TimeStamp<-as.POSIXct(DF $ TimeStamp)

礼物:

I'd like to plot a stick plot for wind speed/ direction data similar to this:

www.researchgate/figure/Stick-plot-of-mean-daily-wind-speed-and-direction-measured-at-Valentia-Island-from_fig5_226577448

I've found a good plot in the excelent oce package, but I'd like to make this same kind of plot using ggplot (or plotley).

WindSpeed<-c(1,2,3,5,7,2,3,4,5,6,7,8) WindDir<-c(180,90,320,200,350,10,270,50,9,100,110,129) TimeStamp<-c("2018-01-02 01:00","2018-01-02 02:00","2018-01-02 03:00","2018-01-02 04:00","2018-01-02 05:00","2018-01-02 06:00","2018-01-02 07:00","2018-01-02 08:00","2018-01-02 09:00","2018-01-02 10:00","2018-01-02 11:00","2018-01-02 12:00") DF<-data.frame(TimeStamp,WindSpeed, WindDir)

The above Wind direction data is in compass direction units (the compass direction the wind is blowing from), so I'd like the 180 data to point straight up, and the 270 data point to point straight to the right (AKA coming from the west and blowing East).

解决方案

Jon Spring's code works. I'll verify (with slightly different data) and show the output with his code.

Slightly different data, with different WindDir to highlight the diagonals (and 30s):

WindSpeed<-c(1,2,3,5,7,2,3,4,5,6,7,8) WindDir <- c(0, 30, 45, 60, 90, 120, 135, 150, 180, 225, 270, 315) TimeStamp<-c("2018-01-02 01:00","2018-01-02 02:00","2018-01-02 03:00","2018-01-02 04:00","2018-01-02 05:00","2018-01-02 06:00","2018-01-02 07:00","2018-01-02 08:00","2018-01-02 09:00","2018-01-02 10:00","2018-01-02 11:00","2018-01-02 12:00") DF<-data.frame(TimeStamp,WindSpeed, WindDir) DF$TimeStamp <- as.POSIXct(DF$TimeStamp)

For a little added clarity, I augmented Jon's code for arrow ends, starting points, and color (grouped naïvely).

ggplot(DF) + geom_segment(aes(x = TimeStamp, y = 0, xend = TimeStamp + lubridate::dhours(WindSpeed * 1 * -cos((90-WindDir) / 360 * 2 * pi)), yend = WindSpeed * 1 * -sin((90-WindDir) / 360 * 2 * pi), col = factor(TimeStamp) ), arrow = arrow(length = unit(0.5, "cm")) ) + geom_point(aes(TimeStamp, 0), size = 1) + coord_fixed(3600) + theme(legend.position = "none")

I think this clearly shows the 45s as good, specifically the last one hitting the grid lines.

Using your data and this code:

WindSpeed<-c(1,2,3,5,7,2,3,4,5,6,7,8) WindDir<-c(180,90,320,200,350,10,270,50,9,100,110,129) TimeStamp<-c("2018-01-02 01:00","2018-01-02 02:00","2018-01-02 03:00","2018-01-02 04:00","2018-01-02 05:00","2018-01-02 06:00","2018-01-02 07:00","2018-01-02 08:00","2018-01-02 09:00","2018-01-02 10:00","2018-01-02 11:00","2018-01-02 12:00") DF<-data.frame(TimeStamp,WindSpeed, WindDir) DF$TimeStamp <- as.POSIXct(DF$TimeStamp)

presents:

更多推荐

棒图在ggplot中获取风速和风向数据

本文发布于:2023-11-22 02:19:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615588.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:风速   风向   数据   ggplot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!