首页 > 解决方案 > 在 geom_text 中指定标签的位置

问题描述

我创建了一个条形图,后来添加了 geom_text。我想让标签从每个条的底部开始,我尝试使用位置 vjust 和 hjust,还指定 y = 0,但它们不起作用,因为标签有不同的长度。如果可能的话,我想通过指定 geom_text 参数来解决它。这是我的情节的一部分:

在此处输入图像描述

我想让每个标签都从相同的高度开始,或者只是在每个栏的底部

代码类似于我原来的

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)

标签: r

解决方案


它应该同时使用y=0来指定相对于图形hjust的位置,并指定文本相对于的位置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)

在 x 轴上对齐文本的绘图


推荐阅读