Adding scale_fill_brewer() to the code causes change in plot values?

224 Views Asked by At

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).

  1. DF:
df_ind_stacked_cum <- read.csv(url("https://raw.githubusercontent.com/johnsnow09/covid19-df_stack-code/main/df_ind_stacked_cum.csv"))
  1. When I create a plot this is what I get:

2.1 enter image description here

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") 
  1. Issue: When I add scale_fill_brewer() then plot looks changed ?

3.1 enter image description here

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")
  1. It gets changed based on palette I select:

scale_fill_brewer(palette = "Set2")

enter image description here

  1. How can I change colors without changing the plot?
0

There are 0 best solutions below