0
votes

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

enter image description here

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
What have you tried? SO is not a free-code-service (idownvotedbecau.se/noattempt). I suggest a combination of segments and some trig functions would work well enough. (You'll also need as.POSIXct to convert your timestamp, though you'll need to add :00 to each.) - r2evans
actually they'll covert just fine without the extra :00 - hrbrmstr
I agree with the comment that your question will be more warmly received if you "show your work." This helps the community work better, helps you to learn, and helps you get well-directed responses. Also, you have typos in your last three timestamps. Fixing those will help make your question reproducible. - Jon Spring
This is a more challenging question than I anticipated, partly because you have to get the x / y ratio right for the angles to be correct, and in this case one will be a date-time and one will be normal numeric. I think this can be addressed by using geom_segment(aes(x = hour, y = 0, xend = TimeStamp + lubridate::dhours(WindSpeed * 1 * -cos(WindDir / 360 * 2 * pi)), yend = WindSpeed * 1 * -sin(WindDir / 360 * 2 * pi))) in combination with coord_fixed(ratio = 60 * 60). - Jon Spring
Crap, just saw this, thanks @hrbrmstr, no idea what I was thinking wrt ":00" - r2evans

1 Answers

1
votes

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")

sample wind-dir plot with modified wind direction

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:

sample windplot with original data