ground geom_text 到 x 轴(例如 y =0)

ground geom_text to x axis (e.g. y =0)

我有一个用 ggplot 制作的图表,看起来像这样:

我希望每个条形上的数字标签相对于 x 轴 grounded/glued,其中 y <= 0。

这是生成图表的代码:

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 将文本置于 x 轴底部,其中 y <= 0,可能还可以引入 angle = 45?

Link 到数据框 = https://drive.google.com/file/d/1b-5AfBECap3TZjlpLhl1m3v74Lept2em/view?usp=sharing

我在代理服务器后面阻止了与 google 驱动器的连接,因此我无法访问您的数据。我无法对此进行测试,但我会在我的数据集中引入一个新的标签字段,如果 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)

希望对您有所帮助。

您可以简单地在 geom_text 中定义 y 值(例如 -50)

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=-50, 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(唯一(df$crop)))+ 实验室(x=c(),y="Number of Pictures")

正如我在评论中所说,只需将文本的 y 坐标设置为 0 或以下,并指定角度:geom_text(aes(x=row, y=-100, label=bbch), angle=45)