r - 将 geom_text 接地到 x 轴(例如 y =0)
问题描述
我有一个用 ggplot 制作的图表,如下所示:
我希望将每个条上的数字标签接地/粘合到 y <= 0 的 x 轴。
这是生成图形的代码:
ggplot(data=df) +
geom_bar(aes(x=row, y=numofpics, fill = crop, group = 1), stat='identity') +
geom_point(data=df, aes(x = df$row, y=df$numofparcels*50, group = 2), alpha = 0.25) +
geom_line(data=df, aes(x = df$row, y=df$numofparcels*50, group = 2), alpha = 0.25) +
geom_text(aes(x=row, y=numofpics, label=bbch)) +
geom_hline(yintercept=300, linetype="dashed", color = "red", size=1) +
scale_y_continuous(sec.axis= sec_axis(~./50, name="Number of Parcels")) +
scale_x_discrete(name = c(),breaks = unique(df$crop), labels = as.character(unique(df$crop)))+
labs(x=c(), y="Number of Pictures")
我已经尝试过vjust
该position_nudge
元素geom_text
,但我能找到的每个解决方案都会将geom_text
相应元素的每个元素的位置更改为其当前位置。因此,我尝试的一切都会导致这种情况:
如何使 ggplot 将文本接地到 y <= 0 的 x 轴底部,可能还可以引入 a angle = 45
?
链接到数据框 = https://drive.google.com/file/d/1b-5AfBECap3TZjlpLhl1m3v74Lept2em/view?usp=sharing
解决方案
我在阻止与谷歌驱动器的连接的代理服务器后面,因此我无法访问您的数据。我无法对此进行测试,但我会在我的数据集中引入一个新的标签字段,如果 y<0,则将 y 设置为 0:
df <- df %>%
mutate(labelField = if_else(numofpics<0, 0, numofpics)
然后我会在我的 geom_text 调用中使用这个标签字段:
geom_text(aes(x=row, y=labelField, label=bbch), angle = 45)
希望有帮助。
推荐阅读
- python - 使用 tensorflow 进行文档分类
- java - Magnolia CMS - 如何使用 LinkFieldDefinition 重置所选值?
- javascript - 删除字符串引号内的双引号
- cryptography - CngKey 上的 SetProperty 始终更新密钥,同时将权限分配给机器密钥。这是正确的行为吗?
- vba - 如何在邮件中自动保存附件和备忘录
- add-on - 是否可以为 Google 日历开发插件?
- android - 如何在此处的地图 SDK (Android) 中请求替代路线?
- c - 我的代码中全局变量的值如何变化?
- powershell - 是否有选项如何通过代码在 Azure devops 中创建新的管道变量
- java - List.get(0) 在 Eclipse 的表达式窗口中工作正常但是当我运行我的代码时它会抛出 classcasteexception