Search code examples
rggplot2positiongeom-text

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


I have a graph made in ggplot that looks like this:

bubblegum

I wish to have the numeric labels at each of the bars to be grounded/glued to the x axis where y <= 0.

This is the code to generate the graph as such:

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")

I've tried vjust and experimenting with position_nudge for the geom_text element, but every solution I can find changes the position of each element of the geom_text respective to its current position. As such everything I try results in situation like this one:

bubblegum2

How can I make ggplot ground the text to the bottom of the x axis where y <= 0, possibly with the possibility to also introduce a angle = 45?

Link to dataframe = https://drive.google.com/file/d/1b-5AfBECap3TZjlpLhl1m3v74Lept2em/view?usp=sharing


Solution

  • As I said in the comments, just set the y-coordinate of the text to 0 or below, and specify the angle : geom_text(aes(x=row, y=-100, label=bbch), angle=45)

    enter image description here