I have a stacked area plot with labels to the right for the values. Three values only appeared in the last bin of data, so they only have values for the upper bound of the x-axis. As a result they show up blank at the top. Is there a way for the plot to show a very thin bar for each of them on top of the stack?
Data:
time <- structure(list(Strategy = structure(c(1L, 2L, 3L, 4L, 5L, 6L,
7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L), levels = c("active immunization",
"immune response", "lipid metabolism", "microtubule stabilization",
"oxidative stress", "proteostasis network", "tau enzyme/PTM",
"cell regeneration/restoration", "general neuroprotection", "neuromodulator/transmission",
"non-pharmacological", "passive immunization", "tau aggregation",
"tau reduction", "energy metabolism", "epigenome/transcription",
"metal ion modulation", "neurotrophin pathway", "tau isoform imbalance correction",
"cellular senescence", "combination", "tau propagation"), class = "factor"),
bin = c("2004-2009", "2004-2009", "2004-2009", "2004-2009",
"2004-2009", "2004-2009", "2004-2009", "2010-2014", "2010-2014",
"2010-2014", "2010-2014", "2010-2014", "2010-2014", "2010-2014",
"2010-2014", "2010-2014", "2010-2014", "2010-2014", "2010-2014",
"2010-2014", "2010-2014", "2015-2019", "2015-2019", "2015-2019",
"2015-2019", "2015-2019", "2015-2019", "2015-2019", "2015-2019",
"2015-2019", "2015-2019", "2015-2019", "2015-2019", "2015-2019",
"2015-2019", "2015-2019", "2015-2019", "2015-2019", "2015-2019",
"2020-2024", "2020-2024", "2020-2024", "2020-2024", "2020-2024",
"2020-2024", "2020-2024", "2020-2024", "2020-2024", "2020-2024",
"2020-2024", "2020-2024", "2020-2024", "2020-2024", "2020-2024",
"2020-2024", "2020-2024", "2020-2024", "2020-2024", "2020-2024",
"2020-2024", "2020-2024"), N = c(1L, 2L, 4L, 1L, 1L, 1L,
2L, 9L, 3L, 2L, 3L, 1L, 7L, 10L, 3L, 7L, 3L, 8L, 12L, 6L,
1L, 4L, 16L, 1L, 2L, 5L, 13L, 14L, 3L, 11L, 8L, 35L, 13L,
1L, 2L, 1L, 1L, 3L, 2L, 4L, 21L, 1L, 2L, 3L, 17L, 20L, 2L,
26L, 21L, 8L, 27L, 10L, 4L, 5L, 3L, 3L, 2L, 1L, 3L, 2L, 2L
), mycolors = c("#97B1BD", "#FBFBB4", "#EBBD63", "#C4D367",
"#D9B382", "#D9D7C9", "#C8EABC", "#97B1BD", "#FBFBB4", "#EBBD63",
"#C4D367", "#D9B382", "#D9D7C9", "#C8EABC", "#F0D1E1", "#C3B4D0",
"#E39699", "#A9A0B2", "#8DD3C7", "#E9877F", "#EED0CD", "#97B1BD",
"#FBFBB4", "#EBBD63", "#C4D367", "#D9B382", "#D9D7C9", "#C8EABC",
"#C3B4D0", "#E39699", "#A9A0B2", "#8DD3C7", "#E9877F", "#EED0CD",
"#C7D98C", "#CDB7CE", "#BE88BF", "#DED7DA", "#CBE5C4", "#97B1BD",
"#FBFBB4", "#EBBD63", "#C4D367", "#D9B382", "#D9D7C9", "#C8EABC",
"#F0D1E1", "#C3B4D0", "#E39699", "#A9A0B2", "#8DD3C7", "#E9877F",
"#EED0CD", "#C7D98C", "#CDB7CE", "#BE88BF", "#DED7DA", "#CBE5C4",
"#C2ADC0", "#E4EB9C", "#FFED6F"), cum = c(0.5, 2, 5, 7.5,
8.5, 9.5, 11, 4.5, 10.5, 13, 15.5, 17.5, 21.5, 30, 36.5,
41.5, 46.5, 52, 62, 71, 74.5, 2, 12, 20.5, 22, 25.5, 34.5,
48, 56.5, 63.5, 73, 94.5, 118.5, 125.5, 127, 128.5, 129.5,
131.5, 134, 2, 14.5, 25.5, 27, 29.5, 39.5, 58, 69, 83, 106.5,
121, 138.5, 157, 164, 168.5, 172.5, 175.5, 178, 179.5, 181.5,
184, 186)), class = c("data.table", "data.frame"), row.names = c(NA,
-61L))
Code:
library(ggplot2)
library(ggrepel)
t_type <- "bin"
p3 <- ggplot(time, aes(x = get(t_type), y = N)) +
geom_area(
aes(group = Strategy, fill = Strategy),
position = position_stack(reverse = TRUE)
) +
scale_fill_manual(
values = unique(time$mycolors),
labels = unique(time$Strategy)
) +
geom_text_repel(
data = ~ filter(., get(t_type) == last(time[[t_type]])),
aes(y = cum, label = Strategy),
direction = "y",
hjust = "left",
segment.color = 'gray',
na.rm = TRUE,
xlim = c(4.2, 6),
ylim = c(0, 200)
) +
scale_y_continuous(limits = c(0, 200), expand = c(0, 0)) +
scale_x_discrete(expand = expansion(mult = c(0, 0.3))) +
labs(x = "Year of study", y = "Number of evaluations") +
theme_classic() +
theme(
# text = element_text(family = "Arial"),
axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1),
legend.position = 'none'
)
If you make the zero values explicit like so:
complete_time <- complete(time, Strategy, bin, fill = list(N = 0))
you get a more consistent plot:
Note that the "step" effect is removed. This is consistent with the stacked area interpretation where the area linearly changes between adjacent time periods. In your plot, some changes are shown as linear changes, while other changes are shown as instantaneous.