如上图所示,如何覆盖标签并使其在整个条形图中居中?
这是我尝试过的:
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]
答案 0 :(得分:1)
在没有原始数据(或其样本)的情况下回答此类问题总是比较困难。但是,假设您的数据如下所示:
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)
然后,关键是要意识到您不希望将geom_label
映射到位置美学。您可以通过简单地指定x,y位置而不用将它们包装在aes
调用中来添加它:
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())