ggplot:如何根据变量类型在 geom_text 位置设置不同的对齐方式?

ggplot: How to set different alignments on the geom_text position based on type of variable?

我有一个显示 3 种类型变量的 100% 堆叠条形图。我已经设置了一个示例数据库,因此我可以更轻松地创建图表。

我已经用我需要的颜色和信息调整了图表。但我无法独立定位标签。这是当前代码和输出。

代码:

(empilhado<-ggplot(dfm, aes(y = Year, x = abs(value), fill = variable)) + 
  scale_x_continuous(sec.axis = sec_axis(trans = ~.*1, name="Trab."), expand=expansion(mult=c(0,0.05)))+
  geom_col(data = rotulo, aes(y = Year, x=abs(trabalho), fill=NULL), width = .7, colour="black", lwd=0.1, position = "fill", orientation = "y") +
  geom_label(data = rotulo, aes(y= Year, x = abs(trabalho), fill=NULL, label=paste(format(round(trabalho, digits=0), nsmall=0, decimal.mark=",", big.mark="."), 
                                                                                  format(round(aprovado, digits=0), nsmall=0, decimal.mark=",", big.mark="."))
                              ), color="black", size=4, position = position_fill(vjust=1.06)) +
  geom_col(width = .7, colour="black", lwd=0.1, position = "fill", orientation = "y") +
  geom_text(aes(label=format(round(value, digits=0), nsmall=0, decimal.mark=",", big.mark=".")),
            size=4, color="white", position = position_fill(vjust=0.5)) +
  theme(panel.grid.major =   element_line(colour = "gray90",size=0.75), panel.grid.minor =   element_line(colour = "gray90",size=0.75),
        legend.position="top", axis.text.x = element_blank(), axis.ticks.x = element_blank(),
        axis.title.x = element_blank(), panel.background = element_blank())+
  scale_fill_manual(values = c("#000000","tomato","blue"))

输出:

现在怎么样了? Position_fill(vjust=0.5),因此所有标签都在其各自的栏内居中。

我想要什么?为了能够在左侧设置 'Marionete' 标签的位置(就像 vjust=0 那样),保持 'Pedido' 标签原样(在 'Pedido' 堆叠的中心bar) 并将 'Fatura' 标签放在右边(就像 vjust=1 那样)。

提前致谢!

实现所需结果的一个选项是 compute/set 每个标签的位置和手动水平对齐,而不是使用 position="fill":

利用一些随机模拟数据:

library(ggplot2)
library(dplyr)

dfm <- dfm %>%
  group_by(Year) %>%
  arrange(desc(variable)) %>%
  mutate(
    pct = value / sum(value),
    x_label = case_when(
      variable == "Marionete" ~ 0,
      variable == "Pedido" ~ .5 * (cumsum(pct) + lag(cumsum(pct))),
      TRUE ~ 1
    ),
    hjust = case_when(
      variable == "Marionete" ~ 0,
      variable == "Pedido" ~ .5,
      TRUE ~ 1
    )
  )


ggplot(dfm, aes(y = Year, x = abs(value), fill = variable)) +
  scale_x_continuous(sec.axis = sec_axis(trans = ~ . * 1, name = "Trab."), expand = expansion(mult = c(0, 0.05))) +
  geom_col(width = .7, colour = "black", lwd = 0.1, position = "fill", orientation = "y") +
  geom_text(aes(x = x_label, label = format(round(value, digits = 0), nsmall = 0, decimal.mark = ",", big.mark = "."), hjust = hjust),
    size = 4, color = "white"
  ) +
  theme(
    panel.grid.major = element_line(colour = "gray90", size = 0.75), panel.grid.minor = element_line(colour = "gray90", size = 0.75),
    legend.position = "top", axis.text.x = element_blank(), axis.ticks.x = element_blank(),
    axis.title.x = element_blank(), panel.background = element_blank()
  ) +
  scale_fill_manual(values = c("#000000", "tomato", "blue"))

数据

set.seed(123)

dfm <- data.frame(
  Year = rep(c(2006:2016), each = 3),
  value = sample(1:100, 3 * 11, replace = TRUE),
  variable = c("Fatura", "Pedido", "Marionete")
)
dfm$variable <- factor(dfm$variable, levels = c("Fatura", "Pedido", "Marionete"))
dfm$Year <- factor(dfm$Year)