0

I have a dataset which includes BSP, TWD, TWS, as well as label for different crew watches. With this I would like to create a wind rose graph that shows the boat speed and wind direction by day or each watch. I found some direction at 17266788 and followed the function created by Andy Clifton. I was able to replicate the function and results using the same wind data and then apply the function without any facet to my sail log data. When applying the facet portion to the wind data I got the same results reflected in the answer posted by Andy. However, when I went to try and add a facet by either the day (an integer formatted from a UTC attribute) or watch label I get the error <At least one layer must contain all faceting variables: utc_int.factor>

Here is a subset data.frame

df_0 <- data.frame(UTC = c(45117.171132,45117.17371,45117.358592,45117.360963,45117.376829,45117.393799,45117.401636,45117.465761,45117.483411,45117.488394), BSP = c(7.343,8.033,6.816,8.197,11.814,8.724,6.997,10.269,10.614,10.796), TWD = c(75.5,55.5,37.6,39.9,27.7,136.1,122,33.7,46.8,45.1), TWS = c(16.41,14.45,16.5,18.68,17.33,17.79,17.93,17.97,22.36,18.43), watch_lbl_adj = c("K","K","A","A","B","B","B","C","C","C"))

After which I ran the following:

df_0.wrose.1 <- plot.windrose(data = df_0, spd = df_0$BSP, dir =df_0$TWD) 
    df_0 <- df_0 %>% mutate(watch_asfactor = factor(watch_lbl_adj)) 
    df_0.wrose.1 <- plot.windrose(data = df_0, spd = df_0$BSP, dir = df_0$TWD) 
    df_0.wrose.2 <- df_0.wrose.1 + facet_wrap(~watch_asfactor, ncol = 2) 

Running:

df_0.wrose.1 <- plot.windrose(data = df_0, spd = df_0$BSP, dir = df_0$TWD)

produced a windrose plot similar to what Andy got with his function. After running

df_0.wrose.1 + facet_wrap(~watch_asfactor, ncol = 2)

I received the error. I believe I'm doing the same sequence of steps Andy showed after adding the Year & Month although I'm not specifying the levels when creating a separate factor. I found 6992592 which suggests some elements when passed to ggplot are dropped but I don't get the error when I follow Andy's answer exactly. I also looked at 50528747 and the answer maybe in the answers provided but it isn't making sense to me; a newbie with R.

neilfws
  • 32,751
  • 5
  • 50
  • 63
inneljpn
  • 3
  • 2

1 Answers1

1

To fix your issue and get a facetted plot pass the column names as characters:

library(ggplot2)
library(dplyr)
library(RColorBrewer)

df_0 <- df_0 %>% mutate(watch_asfactor = factor(watch_lbl_adj))

plot.windrose(data = df_0, spd = "BSP", dir = "TWD") +
  facet_wrap(~watch_asfactor, ncol = 2)

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Thank you @stefan. This fixed my issue and I see now the difference in what I did vs. how Andy passed the column names. However, I'm sure I have understood the why. If there is some reference or documentation that will help with understanding that would be appreciated. I am reading through R graphics Cookbook. – inneljpn Aug 30 '23 at 19:28
  • I think this is an inconsistency in Andy's function and as he notes himself it is more a proof of concept than a fully-worked out general solution. Concerning your question, the issue is simply that when you pass the columns as numeric vectors, then only these two columns are kept in the data passed to `ggplot()` and that's the reason why you get an error when facetting by a third variable. – stefan Aug 30 '23 at 20:50