如何防止两个标签在条形图重叠?(How to prevent two labels to overl

2019-08-31 16:13发布

下面的图像示出了我用下面的代码创建图表。 我强调了丢失或重叠的标签。 有没有办法告诉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

Answer 1:

您可以使用知名的变体人口金字塔 。

一些示例数据(代码由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)))

剧情:

对于生产目的你需要动态地确定合适的y轴刻度标签。



Answer 2:

取得新的采样数据(由@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))



Answer 3:

一个解决方案,以避免重叠是用来酒吧和文本躲闪位置。 为了避免您可以设置缺失值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)


Answer 4:

基于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"))



文章来源: How to prevent two labels to overlap in a barchart?
标签: r ggplot2