# vector of storm types **in required order**
all_statuses <- c("hurricane","tropical storm", "tropical depression")
# convert `status` variable to a factor
storms <- storms %>%
mutate(status=factor(month.abb[month], levels = month.abb))
storms <- storms %>%
mutate(status = factor(status, levels = all_statuses))
# use updated storms data to make bar plot
ggplot(storms,
# N.B. species **already converted to factor**
aes(x = month_fac, fill = all_statuses))
# use geom_bar to add bar plot layer with stacked bars
geom_bar()
Aesthetics must be either length 1 or the same as the data (11859): fill - the error i keep getting Any help would be appreciated
CodePudding user response:
I would pipe everything:
library(ggplot2)
library(dplyr)
library(forcats)
storms %>%
mutate(month_fac = factor(month.abb[month], levels = month.abb), ### as you did
order = case_when(status == "hurricane" ~ 1,
status == "tropical storm" ~ 2,
status == "tropical depression" ~ 3), ### create order
all_statuses = fct_reorder(status, order)) %>% ### create factor
ggplot(aes(x = month_fac, fill = all_statuses))
geom_bar()
Output is:
please be aware that there is no storm data for February and March. You need to add empty lines for that.