【发布时间】:2018-01-10 11:52:03
【问题描述】:
我想用 ggplot 创建一个堆积条形图,并为其添加(居中)标签:当值太低时,我不想显示标签。
df<-data.frame(x=unlist(strsplit("AAAABBBB","")),
z=unlist(strsplit("ABCDABCD","")),
y=c(40,5,30,10,50,60,5, 40))
# this works fine
library(ggplot2)
ggplot(df, aes(x=x, y=y, fill = z)) + geom_bar(stat="identity") +
geom_text(data = df, aes(x=x, y=y, label = y), position = position_stack(vjust=0.5))
但是当我像这样过滤值时(见下文),它也会改变每个标签的位置。这适用于散点图,但由于定位基于堆叠值,因此标签显示得太低。
#don't show values 5 or less
ggplot(df, aes(x=x, y=y, fill = z)) + geom_bar(stat="identity") +
geom_text(data = df[df$y > 5,], aes(x=x, y=y, label = y), position =
position_stack(vjust=0.5))
【问题讨论】: