I am using a dataframe to create facet plot by months to view highest covid CFR states for each month (Issue shown in point 3).
- DF:
df_ind_stacked_cum <- read.csv(url("https://raw.githubusercontent.com/johnsnow09/covid19-df_stack-code/main/df_ind_stacked_cum.csv"))
- When I create a plot this is what I get:
2.2 code:
df_ind_stacked_cum %>%
filter(!State.UnionTerritory %in%
c("Maharashtra***","Punjab***","Chandigarh***","Telangana***", "Cases being reassigned to states")) %>%
mutate(month = lubridate::month(Date, label = TRUE, abbr = TRUE)) %>%
filter(Cases_type == "Confirmed") %>%
group_by(State.UnionTerritory, month) %>%
summarise(CFR = max(CFR, na.rm = TRUE)) %>%
group_by(month) %>%
top_n(n = 7, wt = CFR) %>%
ungroup() %>%
mutate(State.UnionTerritory = fct_reorder(State.UnionTerritory, CFR, max)) %>%
ggplot(aes(x = CFR, y = State.UnionTerritory,
fill = State.UnionTerritory, group = State.UnionTerritory )) +
geom_col() +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90),
legend.position = "none") +
facet_wrap(~month) +
# coord_cartesian(clip = "off") +
# scale_fill_tableau() +
labs(title = "Top 7 CFR States based on each month")
- Issue: When I add
scale_fill_brewer()then plot looks changed ?
3.2 Code:
df_ind_stacked_cum %>%
filter(!State.UnionTerritory %in%
c("Maharashtra***","Punjab***","Chandigarh***","Telangana***", "Cases being reassigned to states")) %>%
mutate(month = lubridate::month(Date, label = TRUE, abbr = TRUE)) %>%
filter(Cases_type == "Confirmed") %>%
group_by(State.UnionTerritory, month) %>%
summarise(CFR = max(CFR, na.rm = TRUE)) %>%
group_by(month) %>%
top_n(n = 7, wt = CFR) %>%
ungroup() %>%
mutate(State.UnionTerritory = fct_reorder(State.UnionTerritory, CFR, max)) %>%
ggplot(aes(x = CFR, y = State.UnionTerritory,
fill = State.UnionTerritory, group = State.UnionTerritory )) +
geom_col() +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90),
legend.position = "none") +
facet_wrap(~month) +
# coord_cartesian(clip = "off") +
# scale_fill_tableau() +
labs(title = "Top 7 CFR States based on each month") +
scale_fill_brewer(palette = "Paired")
- It gets changed based on
paletteI select:
scale_fill_brewer(palette = "Set2")
- How can I change colors without changing the plot?


