In below plot , 1) How to align category 'A' to axis x (axis y start from 0)?
2) How to change sub_category fill color ? want change to 'pink' (whis the color samilar to category color) . Anyone can help?
library(tidyverse)
plot_data <- data.frame(category=c('A','A','B','C'),
sub_category=c('a1','a2','b1','c1'),
value=c(6,12,3,2))
plot_data %>% mutate(sub_category=if_else(category=='A',
sub_category,category)) %>%
pivot_longer(names_to = 'title',values_to ='cat_region',-value) %>%
filter(!(title=='sub_category'&cat_region %in% c('B','C') )) %>%
group_by(title,cat_region) %>%
summarise(value_sum=sum(value)) %>%
ggplot(aes(x=title,y=value_sum,fill=cat_region,
group=interaction(title,cat_region)))+geom_col()
I think you're looking for scale_fill_manual
to select the fill colors, and position_stack(reverse = TRUE)
to reverse the stacking order:
plot_data %>%
mutate(sub_category = if_else(category=='A', sub_category, category)) %>%
pivot_longer(names_to = 'title', values_to = 'cat_region', -value) %>%
filter(!(title == 'sub_category' & cat_region %in% c('B','C'))) %>%
group_by(title, cat_region) %>%
summarise(value_sum = sum(value)) %>%
ggplot(aes(x = title, y = value_sum, fill = cat_region,
group = interaction(title,cat_region))) +
geom_col(position = position_stack(reverse = TRUE)) +
scale_fill_manual(values = c(A = "#f8766d", a1 = "#ffa8a3",
a2 = "#ffe2e0", B = "#00b0f6",
C = "#74d67f")) +
theme_bw()