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

时间:2013-04-21 01:48:25

标签: r ggplot2

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

enter image description here

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

4 个答案:

答案 0 :(得分:10)

您可以使用众所周知的population pyramid的变体。

一些示例数据(代码灵感来自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)

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

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

画出情节。请注意,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)))

情节:

plot

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

答案 1 :(得分: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)

使用库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

现在使用新数据框将标签与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))

enter image description here

答案 2 :(得分:5)

避免重叠的一个解决方案是使用闪避条和文本的位置。为避免遗漏值,您可以设置ylim。这是一个例子。

enter image description here

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

答案 3 :(得分:3)

根据Didzis绘图,您还可以通过保持y轴上的位置不变并将文本着色为与图例相同的颜色来提高可读性。

library(ggplot2)
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)


library(plyr)
text.df<-ddply(dat,.(Week,Status),function(x) data.frame(count=nrow(x)))
text.df$ypos[text.df$Status=="Not-Shipped"]<- -15
text.df$ypos[text.df$Status=="Shipped"]<- -55

p <- ggplot(dat,aes(as.factor(Week),fill=Status))+geom_bar()+
geom_text(data=text.df,aes(x=as.factor(Week),y=ypos,label=count),colour=ifelse(text.df$Status=="Not-Shipped","#F8766D","#00BFC4"))

enter image description here