如何防止两个标签在条形图中重叠?

Mar*_*tin 8 r ggplot2

下图显示了我使用下面的代码创建的图表.我突出显示缺少或重叠的标签.有没有办法告诉ggplot2不重叠标签?

在此输入图像描述

week = c(0, 1, 1, 1, 1, 2, 2, 3, 4, 5)
statuses = c('Shipped', 'Shipped', 'Shipped', 'Shipped', 'Not-Shipped', 'Shipped', 'Shipped', 'Shipped', 'Not-Shipped', 'Shipped')

dat <- data.frame(Week = week, Status = statuses)

p <- qplot(factor(Week), data = dat, geom = "bar", fill = factor(Status))
p <- p + geom_bar()
# Below is the most important line, that's the one which displays the value
p <- p + stat_bin(aes(label = ..count..), geom = "text", vjust = -1, size = 3)
p
Run Code Online (Sandbox Code Playgroud)

Ale*_*ael 10

您可以使用众所周知的人口金字塔的变体.

一些示例数据(代码灵感来自Didzis Elferts的回答):

set.seed(654)
week <- sample(0:9, 3000, rep=TRUE, prob = rchisq(10, df = 3))
status <- factor(rbinom(3000, 1, 0.15), labels = c("Shipped", "Not-Shipped"))
data.df <- data.frame(Week = week, Status = status)
Run Code Online (Sandbox Code Playgroud)

计算每周的计数分数,然后将一个类别转换为负值:

library("plyr")
plot.df <- ddply(data.df, .(Week, Status), nrow)
plot.df$V1 <- ifelse(plot.df$Status == "Shipped",
                     plot.df$V1, -plot.df$V1)
Run Code Online (Sandbox Code Playgroud)

绘制情节.请注意,y轴标签适合在基线的任一侧显示正值.

library("ggplot2")
ggplot(plot.df) + 
  aes(x = as.factor(Week), y = V1, fill = Status) +
  geom_bar(stat = "identity", position = "identity") +
  scale_y_continuous(breaks = 100 *     -1:5, 
                     labels = 100 * c(1, 0:5)) +
  geom_text(aes(y = sign(V1) * max(V1) / 30, label = abs(V1)))
Run Code Online (Sandbox Code Playgroud)

剧情:

情节

出于生产目的,您需要动态确定适当的y轴刻度标签.


Did*_*rts 7

制作了新的样本数据(受@agstudy代码的启发).

week <- sample(0:5,1000,rep=TRUE,prob=c(0.2,0.05,0.15,0.5,0.03,0.1))
statuses <- gl(2,1000,labels=c('Not-Shipped', 'Shipped'))
dat <- data.frame(Week = week, Status = statuses)
Run Code Online (Sandbox Code Playgroud)

使用ddply()库中的函数为标签创建plyr了新的数据框text.df.列count包含的每个组合的观测数WeekStatus.然后添加ypos包含count每周累加和加15的列.这将用于y位置.对于Not-Shipped ypos替换为-10.

library(plyr)
text.df<-ddply(dat,.(Week,Status),function(x) data.frame(count=nrow(x)))
text.df<-ddply(text.df,.(Week),transform,ypos=cumsum(count)+15)
text.df$ypos[text.df$Status=="Not-Shipped"]<- -10
Run Code Online (Sandbox Code Playgroud)

现在geom_text()使用新数据框绘制标签.

ggplot(dat,aes(as.factor(Week),fill=Status))+geom_bar()+
  geom_text(data=text.df,aes(x=as.factor(Week),y=ypos,label=count))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


ags*_*udy 5

避免重叠的一种解决方案是用于躲避条和文本的位置.为避免遗漏您可以设置的值ylim.这是一个例子.

在此输入图像描述

##  I create some more realistic data similar to your picture
week <- sample(0:5,1000,rep=TRUE)
statuses <- gl(2,1000,labels=c('Not-Shipped', 'Shipped'))
dat <- data.frame(Week = week, Status = statuses)

## for dodging
dodgewidth <- position_dodge(width=0.9)
## get max y to set ylim
ymax <- max(table(dat$Week,dat$Status))+20
ggplot(dat,aes(x = factor(Week),fill = factor(Status))) + 
  geom_bar( position = dodgewidth ) +
  stat_bin(geom="text", position= dodgewidth, aes( label=..count..),
           vjust=-1,size=5)+
  ylim(0,ymax)
Run Code Online (Sandbox Code Playgroud)