条形上方的注释: [英] Annotation above bars:

查看:28
本文介绍了条形上方的注释:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆