r - 在整个条形图中覆盖文本标签
问题描述
如上图所示,如何覆盖标签并将其在整个条形图中居中?
这是我尝试过的:
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]
解决方案
如果没有原始数据(或其样本),回答这类问题总是比较困难的。但是,假设您的数据看起来像这样:
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())
推荐阅读
- mysql - MySQL:通过将位于两个单独表中的价格和数量列相乘来计算餐厅数据库中每个订单的总价格
- python - 查询不会使用变量运行
- c++ - 用 const 定义的变量会占用内存吗?
- swift - 如何在对同一链中的 dataTaskPublisher 的请求中使用来自一个发布者的值?
- python - 如何从 while 循环中删除尾随逗号?
- python - 使用 matplotlib 的 fill_between 函数出现意外值错误
- reactjs - 反应和打字稿中的 useState() 效果不佳..!再次
- serenity-bdd - 我可以在 Serenity-JAVA 测试自动化框架的 @DefaultUrl 中传递一个捕获字符串值(基本上是带有令牌的 url)的变量吗
- node.js - 如何在开发过程中设置 NPM Typescript 包以维护导入语法?
- node.js - 如何在 Next.js 中获取“x-”标头