我想绘制类似于这样的风速/风向数据的简图:
我在出色的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数据指向右指向右侧(又名从西方吹向东方)。
答案 0 :(得分:1)
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的箭头结尾,起点和颜色(天真地分组)的代码。
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")
我认为这清楚地表明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)
礼物: