Reputation: 11
How can I overlay a label and center it across the entire bar plot as shown in the image above?
Here is what I have tried:
ggplot(my_NYC_Crimes, aes(x=Year, y=Crime.total)) +
geom_bar(stat = "identity", fill= "#b43a74") +
scale_y_continuous(labels = scales::comma) + theme_minimal() +
theme(axis.title.x = element_blank()) + theme(axis.title.y = element_blank())
+ theme_void() + geom_text(aes(label = "Major felonies"), position =
position_stack(0.5), color = "white")[enter image description here][1]
Upvotes: 1
Views: 212
Reputation: 173783
It's always more difficult to answer this kind of question without the original data (or a sample thereof). However, assuming your data looks something like this:
my_NYC_Crimes <- data.frame(Year = 2001:2019,
Crime.total = c(seq(32, 20, -1.1), 21, 23, 25, 23, 21, 20, 18, 17) * 1000)
Then the key is to realise that you do not want the geom_label
to be mapped to a positional aesthetic. You can add it by simply specifying the x, y positions without having them wrapped in an aes
call:
ggplot(my_NYC_Crimes, aes(x = Year, y = Crime.total)) +
geom_col(fill= "#b33877") +
scale_y_continuous(breaks = 1:3 * 10000) +
geom_label(label = "Major felonies", x = 2010, y = 7500, size = 10,
color = "white", fill = "#b33877", label.size = 0) +
theme_void() +
theme(panel.grid.major.y = element_line())
Upvotes: 2