问题
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).
回答1:
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:
来源:https://stackoverflow.com/questions/52895843/stick-plot-for-wind-speed-and-direction-data-in-ggplot