ggplot中风速和风向数据的棒图 [英] Stick Plot for wind speed and direction data in ggplot

查看:27
本文介绍了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:

https://www.researchgate.net/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中风速和风向数据的棒图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆