我有一些数据,其中某些组并未出现在所有月份。我将其绘制在条形图上,但带有显示数值的文本标签,但文本并未位于每个条形图的顶部。我尝试使用“闪避”位置作为标签,但没有成功。
下面是一个可重现的例子:
library(ggplot2)
library(dplyr)
library(scales)
data.frame(Month = as.Date(c("2015-01-01", "2015-01-01","2015-01-01",
"2015-02-01","2015-02-01","2015-02-01")),
types = rep(LETTERS[1:3],2),
percent = c(0.2,NA,NA,.39,.89,.59)) %>%
ggplot( aes(x = Month, y = percent, fill = types)) +
geom_bar(stat = "identity", position = "dodge") +
scale_y_continuous(labels = percent) +
scale_x_date(labels = date_format("%Y-%b")) +
geom_text(stat = 'identity',
aes(label = ifelse(percent > .02,
paste(round(percent*100,0),"%",sep = "") ,'')))
这是它的样子: