上面的注释: [英] Annotation above bars:

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

问题描述

在ggplot闪避酒吧阴谋再次让我难住。我几周前在这里询问了关于注释文本上方的文本( LINK )并使用 + stat_bin(geom =text,aes(label = .. count ..,vjust = -1))得到了非常好的回应。我想,因为我已经有了计数,我只会在 .. 之前和之后提供它们,并告诉 stat_bin 位置闪避。它将它们排列在组的中心并上下调整。可能是小事。请帮我把文字放在横条上。


  mtcars2 < -  data.frame(type = factor(mtcars $ cyl),
组=因子(mtcars $齿轮))
图书馆(plyr);库(ggplot)
dat <-rbind(ddply(mtcars2,。(type,group),summarize,
count = length(group)),c(8,4,NA))

p2 < - ggplot(dat,aes(x = type,y = count,fill = group))+
geom_bar(color =black,position =dodge,stat = identity)+
stat_bin(geom =text,aes(position ='dodge',label = count,vjust = - 。6))


解决方案

我无法让位置闪避排队,所以我最终创建了一个position_dodge对象术语?),将其保存到变量中,然后将其用作两个geoms的位置。 有点令人生气,他们似乎还是偏离了一点偏心。

  dodgewidth < -  position_dodge(width = 0.9)
ggplot(dat,aes(x =类型,y = count,fill = group))+
geom_bar(color =black,position = dodgewidth,stat =identity)+
stat_bin(geom =text,position = dodgewidth ,aes(x = type,label = count),vjust = -1)


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)) 

解决方案

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天全站免登陆