注释以上吧:(Annotation above bars:)

2019-06-23 23:30发布

在ggplot躲过柱状图中再次有我难住了。 我问了一下就到这里了几个星期上面标注的文字吧回( LINK ),并得到使用一个了不起的响应+ stat_bin(geom="text", aes(label=..count.., vjust=-1)) 我想既然我已经有我就与他们提供了计数..之前和之后,我告诉stat_bin ,该positiondodge 。 它线条起来在组中心和调整上下。 可能是一些未成年人。 请帮我拿到过栏的文本。

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

Answer 1:

我遇到麻烦位置闪去排队,于是我结束了创建一个position_dodge对象(是正确的术语?),将其保存到一个变量,然后使用该作为两个geoms位置。 有点僵硬,他们似乎仍然有点偏离中心。

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)



Answer 2:

更新 geom_bar()需要stat = "identity"

我想这是你想要和什么。

mtcars2 <- data.frame(type = factor(mtcars$cyl), group = factor(mtcars$gear))
library(plyr); library(ggplot2)
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(stat = "identity", colour = "black",position = "dodge", width = 0.8) +
  ylim(0, 14) +
  geom_text(aes(label = count, x = type, y = count), position = position_dodge(width = 0.8), vjust = -0.6)
p2



文章来源: Annotation above bars:
标签: r ggplot2