ggplot中风速和风向数据的棒图

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

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

我在 excelent oce 包中找到了一个很好的情节,但我想使用 ggplot(或 plotley)制作同样的情节.

风速<-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)时间戳<-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 数据指向正右(AKA西吹东吹).

解决方案

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

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

风速<-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)时间戳<-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 的箭头末端、起点和颜色的代码进行了扩充(简单地分组).

ggplot(DF) +geom_segment(aes(x = 时间戳,y = 0,xend = 时间戳 + lubridate::dhours(WindSpeed * 1 * -cos((90-WindDir)/360 * 2 * pi)),yend = WindSpeed * 1 * -sin((90-WindDir)/360 * 2 * pi),col = 因子(时间戳)),箭头=箭头(长度=单位(0.5,厘米")))+geom_point(aes(TimeStamp, 0), size = 1) +坐标固定(3600)+主题(图例.位置=无")

我认为这清楚地表明了 45 秒,特别是最后一个击中网格线.

使用您的数据和此代码:

风速<-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)时间戳<-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:17:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615581.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:风速   风向   数据   ggplot

发布评论

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

>www.elefans.com

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