Szymon Fraś
Szymon Fraś

Reputation: 106

Specifying the position of a label in geom_text

I created a barplot and later added geom_text. I would like to make labels start at the bottom of each bar, I tried to use position vjust and hjust, also specify y = 0, but they didn't work, because labels have different lengths. I would like to solve it by specyfing geom_text arguments if possible. That's part of how my plot looks:

enter image description here

I want to make every label start at the same height, or just at the bottom of each bar

Code similar to my original

xxx <- sample(letters,1000, replace = T)
xxx <- data.frame(x=xxx)
text <- c(rep(c("b","adsasdasasd"),13))
library(tidyverse)
xxx %>%
  count(x) %>%
  ggplot(aes(x,n))+
  geom_bar(stat="identity")+
  geom_text(aes(x, label = text),y=0, angle=90)

Upvotes: 0

Views: 3430

Answers (1)

Alexlok
Alexlok

Reputation: 3134

It should work using both y=0 to specify the position relative to the graph, and hjust to specify the position of the text relative to the y:

library(tidyverse)
xxx <- sample(letters,1000, replace = T)
xxx <- data.frame(x=xxx)
text <- c(rep(c("b","adsasdasasd"),13))

xxx %>%
  count(x) %>%
  ggplot(aes(x,n))+
  geom_bar(stat="identity")+
  geom_text(aes(x, label = text), y=0, hjust="bottom", angle=90)

plot with text aligned on x axis

Upvotes: 1

Related Questions