我最近绘制了下一个data.frame的堆积条形图:
dput(head(mdfrsubsetN))
structure(list(MESES = structure(1:6, .Label = c("Enero", "Febrero",
"Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre",
"Octubre", "Noviembre", "Diciembre"), class = "factor"), variable = structure(c(1L,
1L, 1L, 1L, 1L, 1L), .Label = c("19N", "19DURAC", "50N", "50DURAC",
"PUSHN", "PUSHDURAC", "GRATISN", "GRATISDURAC"), class = "factor"),
value = c(NA, NA, 1431, 982, 766, NA)), row.names = c(NA,
6L), class = "data.frame")
使用以下代码:
ggplot(mdfrsubsetN, aes(MESES, value, fill = variable)) +
geom_col(position = "fill") +
scale_y_continuous(labels = percent)
现在,我想使用辅助data.frame将geom_text应用于其对应的小节。 data.frame具有下一个结构。
dput(head(mdfrsubsetDURAC))
structure(list(MESES = structure(1:6, .Label = c("Enero", "Febrero",
"Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre",
"Octubre", "Noviembre", "Diciembre"), class = "factor"), variable = structure(c(2L,
2L, 2L, 2L, 2L, 2L), .Label = c("19N", "19DURAC", "50N", "50DURAC",
"PUSHN", "PUSHDURAC", "GRATISN", "GRATISDURAC"), class = "factor"),
value = c(NA, NA, 5.04, 5.01, 4.61, NA)), row.names = 13:18, class = "data.frame")
我尝试了下一个代码,但是结果不是我想要的。
ggplot(mdfrsubsetN, aes(MESES, value, fill = variable)) +
geom_col(position = "fill") +
scale_y_continuous(labels = percent) +
geom_text(data=mdfrsubsetDURAC, aes(x = MESES, y = value, label = value), position = "fill", vjust="center")+
theme_classic() +
scale_fill_discrete(name="PROMOS",
breaks=c("19N", "50N","PUSHN","GRATISN"),
labels=c("19", "50%", "PUSH", "GRATIS"))
这是结果。
我希望这些注释位于右侧栏上,因为现在它们位于我需要它们的位置上方。如果我可以将它们放在每个条形的中心,那将是完美的。
答案 0 :(得分:1)
尝试
geom_text(data=mdfrsubsetDURAC, aes(x = MESES, y = value, label = value), hjust = 0.5, vjust = 0.5,position = position_fill(vjust = 0.5))