Hi all,
This is a follow-up to this question. Thanks to the help from @FJCC , I modified the code a bit. But it still did meet my expectation and I failed to find a solution for filling different colors based on the x-axis values.
My aim is to fill diary and journal with different colors like this. I appreciate your help and your time!
# test dataset
# A tibble: 10 x 4
date gender genre group
<dbl> <chr> <chr> <chr>
1 1778 female diary s
2 1778 female journal of
3 1778 male diary s
4 1778 male diary of
5 1778 male journal s
6 1782 female diary of
7 1782 female diary s
8 1782 female journal of
9 1782 female journal s
10 1782 male journal of
ggplot(test) +
geom_bar(mapping = aes(x = interaction(date, genre, lex.order = TRUE), fill = group, color = group),
position = "fill", width=0.7) +
geom_segment(mapping = aes(x=x,y=y, xend=xend, yend= yend),
data = data.frame(x=2.5, xend=2.5, y =0, yend = -0.2)) +
labs(y = "Percentage") +
theme_bw() +
theme(legend.position = "none",
plot.margin = unit(c(1, 1, 4, 1), "lines"),
axis.title.x = element_blank(),
axis.text.x = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
axis.title = element_text(size=rel(1.5)), axis.text.y = element_text(size=rel(1.5))) +
annotate(geom = "text", x = 1:4, y = -0.05, label = c("diary", "journal", "diary", "journal"), size = 6) +
annotate(geom = "text", x = c(1.5, 3.5), y = -0.15, label = c("1778", "1782"), size = 7) +
coord_cartesian(ylim = c(-0.2, 1), expand = FALSE, clip = "off", xlim = c(0.5, 4.5))