条形上方的注释: [英] Annotation above bars:
问题描述
ggplot 中的躲闪条形图再次让我难倒.几周前,我询问了关于在条形上方注释文本的问题(LINK)使用 + stat_bin(geom="text", aes(label=..count.., vjust=-1))
得到了很好的回应.我想,因为我已经有了计数,所以我只需在前后提供 ..
给它们,然后我告诉 stat_bin
position
是躲闪
.它将它们排列在组的中心并上下调整.大概是小事.请帮我把文字越过栏.
dodged bar plot in ggplot again has me stumped. I asked about annotating text above bars on here a few weeks back (LINK) and got a terrific response to use + stat_bin(geom="text", aes(label=..count.., vjust=-1))
. I figured since I already have the counts I'll just supply them with out the ..
before and after and I told stat_bin
that the position
was dodge
. It lines them up over the center of the group and adjusts up and down. Probably something minor. Please help me to get the text over the bars.
mtcars2 <- data.frame(type=factor(mtcars$cyl),
group=factor(mtcars$gear))
library(plyr); library(ggplot)
dat <- rbind(ddply(mtcars2,.(type,group), summarise,
count = length(group)),c(8,4,NA))
p2 <- ggplot(dat,aes(x = type,y = count,fill = group)) +
geom_bar(colour = "black",position = "dodge",stat = "identity") +
stat_bin(geom="text", aes(position='dodge', label=count, vjust=-.6))
推荐答案
我在让位置闪避对齐时遇到了麻烦,所以我最终创建了一个 position_dodge 对象(这是正确的术语吗?),将它保存到一个变量,然后将其用作两个几何体的位置.有点令人气愤的是,他们似乎仍然有点偏离中心.罢工>
I was having trouble getting the position dodges to line up, so I ended up creating a position_dodge object (is that the right terminology?), saving it to a variable, and then using that as the position for both geoms. Somewhat infuriatingly, they still seem to be a little off centre.
dodgewidth <- position_dodge(width=0.9)
ggplot(dat,aes(x = type,y = count, fill = group)) +
geom_bar(colour = "black", position = dodgewidth ,stat = "identity") +
stat_bin(geom="text", position= dodgewidth, aes(x=type, label=count), vjust=-1)
这篇关于条形上方的注释:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!