So far we’ve found a satisfying way to count the number of tropical depressions as well as the number of tropical storms. We could adapt the recent commands to get a table with counts of hurricanes of category 1, another table for hurricanes of category 2, and so on and so forth.
Interestingly, we can also identify the maximum category for each type of system, all at once. All we have to do is remove the filter() command, as follows:
system_status = storms |>group_by(year, name) |>summarise(wind_max =max(wind),.groups ="drop")slice_head(system_status, n =10)
We can take a further step and add a column that displays the category status in text form.
How can you accomplish this? One nice option is with the case_when() function. Let’s take a look at the command that gets the job done, and then we discuss it.
# adding a wind category status in text formatsystem_status = storms |>group_by(year, name) |>summarise(wind_max =max(wind), .groups ="drop") |>mutate(wind_scale =case_when( wind_max <=33~-1L, wind_max <=63~ 0L, wind_max <=82~ 1L, wind_max <=95~ 2L, wind_max <=112~ 3L, wind_max <=136~ 4L, wind_max >=137~ 5L ) )slice_head(system_status, n =10)
As you can tell, the input to case_when() consists of multiple conditions based on the variable wind_max. The first condition is:
wind_max <= 33 ~ -1
This means that the value -1 will be associated to all the wind_max values less than or equal to 33. Basically, this indicates a tropical depression.
The next condition is:
wind_max <= 63 ~ 0
which means that 0 will be associated to all the wind_max values less than or equal to 63 (but greater than 33). This indicates a tropical storm.
Observe also the use of the tilde ~ to indicate the output for each text value.
We can take a further step to add another column wind_categ that displays the category status in text form. That is, for wind_scale == -1 have an associated value of "td"; for wind_scale == 0 have an associated value of "ts"; for wind_scale == 1 have an associated value of "cat1", etc.
# adding a wind category status in text formatsystem_status = system_status |>mutate(wind_categ =case_when( wind_scale ==-1~'td', wind_scale ==0~'ts', wind_scale ==1~'cat1', wind_scale ==2~'cat2', wind_scale ==3~'cat3', wind_scale ==4~'cat4', wind_scale ==5~'cat5' ))slice_head(system_status, n =10)
With system_status we can then create a barchart, mapping wind_scale to the fill attribute of the geom_col():
system_status |>count(year, wind_scale) |>ggplot() +geom_col(aes(x = year, y = n, fill =factor(wind_scale))) +labs(title ="Number of Storms per Year, and Category",y ="Count") +theme_minimal()
This barchart allows us to see how different storm categories are distributed over time. As we know from the graphics obtained in chapter 13, the number of systems shows an increasing trend. Despite the eye catching color palette, and the clear increasing pattern, it is hard to tell whether all the storm categories exhibit the growing trend.
An alternative visual display is to use facets so that we separate each category in its own frame (see below).
system_status |>count(year, wind_scale) |>ggplot() +geom_col(aes(x = year, y = n, fill =factor(wind_scale))) +facet_wrap(~ wind_scale) +labs(title ="Number of Storms Over Time, and Category",subtitle ="Tropical storms have increased in the last 4 decades",y ="Count") +theme_minimal() +theme(panel.grid.minor =element_blank())
In this plot we can easily see that the main type of systems that have been constantly increasing every decade are tropical storms (category 0). This pattern was not so evident in the preceding graphic that does not have facets. If you look at hurricanes of category 3, their number seems to remain stable between 0 and 2 almost every year.
One last step: what happens if we specify wind_scale as an ordinal factor? Since the numeric scaled in wind_scale is an ordinal scale, we can take advantage of R’s factors and make this column an ordered factor via the function ordered(). The visual benefit of doing this is that ggplot() will use a special color palette called viridis which relies on a sequence of blues and yellows, while avoiding reds, in order to increase the readability of data visualizations.
system_status = system_status |>mutate(wind_scale =ordered(wind_scale))system_status |>count(year, wind_scale) |>ggplot() +geom_col(aes(x = year, y = n, fill = wind_scale)) +facet_wrap(~ wind_scale) +labs(title ="Number of Storms Over Time, and Category",subtitle ="Tropical storms have increased in the last 4 decades",y ="Count") +theme_minimal() +theme(panel.grid.minor =element_blank())