# Creating Windrose Plots in R

Recently I had to plot some wind data and found a couple of packages to produce windrose plots. My first choice was Windrose in the circular package. Windrose was easy to use, and I really liked the appearance of the plots.  The only drawback was some difficulty getting the axis labels formatted the way I wanted. So, I decided to try Rosavent.  Rosavent was perfect because the axis labels were exactly what I needed, and it produced a nice legend on the plot.

Using rosavent is just a little tricky if you’re starting with a table of wind speed and direction because it takes a data frame with the frequency of wind speed for each direction class (Windrose does this work for you). This means you need to create class ranges for both wind direction and wind speed and calculate the number of times each wind speed class occurs in each direction class.

If you have a data frame with columns for wind speed and direction, there are just a few easy steps needed to get your data in the right format for Rosavent. To start we load Climatol, Reshape, and our data.
``` library(climatol) library(reshape) winds <- read.table("/input_dir/winds.csv", sep=",",  header=TRUE) ```
Next we need to convert our data into a frequency table with columns for our wind direction classes and rows for our wind speed classes. In the first seq argument below I have divided 360 degrees into 16 22.5 degree classes.  In the second I have divided wind speed into 10 classes each representing 1 m/sec.
``` freq_speed_table <- t(table(cut(winds\$wind_direction, seq(0, to=360, by=22.5)), cut(winds\$wind_speed, seq(0, to=10, by=1)))) ```
The data is now organized the way we need it, but rosavent takes a data frame – not a table, so we convert our data into a data frame.
``` freq_speed_df = data.frame(freq_speed_table, row.names = NULL, check.rows = FALSE, check.names = TRUE, stringsAsFactors = default.stringsAsFactors()) ```
To get our dataframe back into the structure of our table, we use cast from the reshape package:
``` freq_speed_df_cast = cast(freq_speed_df, Var1 ~ Var2) ```
Finally, we can plot our data with rosavent:
``` png('/output_dir/example_windrose_plot.png') rosavent(freq_speed_df_cast, 3, 5, ang=-2*pi/16, flab = 2, main="Example Plot", key = TRUE, margen = c(0, 0, 2, 0), uni = "Wind Speed m/s") dev.off() ```
..and this produces a nice windrose plot: 