【发布时间】:2017-06-29 07:05:50
【问题描述】:
我不知道如何在ggplot2 的条形图中以每个闪避条为中心显示标签。
我知道我可以使用position = "dodge" 避开条形图,并且我知道为了使标签显示在每个条形图的中心,我需要在geom_text() 或geom_label() 命令中添加position = position_dodge(width = 1)。
但由于某种原因它不起作用(见下图)。我还添加了我的数据和代码。
df <- structure(list(Measure = structure(c(2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1988",
"2017"), class = "factor"), Province = structure(c(1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L), .Label = c("BC", "AB", "SK", "MB", "ON", "QC", "NB",
"PE", "NS", "NL"), class = "factor"), Value = c(363L, 61L, NA,
69L, NA, NA, 127L, 12L, 92L, 18L, 178L, 29L, 41L, 92L, 284L,
1019L, 267L, 27L, 77L, 22L)), .Names = c("Measure", "Province",
"Value"), row.names = 41:60, class = "data.frame")
ggplot(df, aes(x=Province, y=Value)) + geom_bar(aes(fill=Measure), position="dodge",
stat="identity") + geom_label(aes(label=Value), position = position_dodge(width=1))
【问题讨论】: