Reputation: 49
I am looking to add numerical values to the middle of each stack in the stacked bar graph (code below). Most of the examples I am finding are orientated towards information in one column and whenever I try to modify it, I run into errors about length requirements.
DA <- data.frame(
Imp=c("2015","2019"),
"mismatch"=c(220,209),
"match"=c(3465,3347),
"NA"=c(501,630),
check.names = FALSE)
DA %>%
pivot_longer(-Imp) %>%
ggplot(aes(x = Imp, y = value, fill = name)) + geom_col(position = "stack") +
scale_fill_manual(name=" ", values=c("aquamarine4", "orange", "coral")) +
theme_light() +
theme(legend.position = "bottom") +
scale_y_continuous(expand = c(0,0)) +
geom_text(aes(x=1, y=4300, label="Stretch it"), vjust=-1) +
labs(y="Count", x="Imputed Genotypes") +
geom_bar(stat = "identity", color="white", width = 1)
Upvotes: 0
Views: 1156
Reputation: 26225
Like this?
library(tidyverse)
DA <- data.frame(
Imp=c("2015","2019"),
"mismatch"=c(220,209),
"match"=c(3465,3347),
"NA"=c(501,630),
check.names = FALSE)
DA %>%
pivot_longer(-Imp) %>%
ggplot(aes(x = Imp, y = value, fill = name)) +
geom_col(color = "white", lwd = 1,
position = "stack", width = 0.75) +
scale_fill_manual(name="", values=c("aquamarine4", "orange", "coral")) +
scale_y_continuous(expand = c(0,0),
limits = c(0, 4200)) +
labs(y="Imputed Genotypes (Count)") +
geom_text(aes(label = value), color = "white", size = 5,
position = position_stack(vjust = 0.5),
show.legend = FALSE) +
theme_light(base_size = 18) +
theme(legend.position = "right",
axis.title.x = element_blank())
Created on 2021-12-19 by the reprex package (v2.0.1)
Upvotes: 3