如何防止两个标签在条形图中重叠? [英] How to prevent two labels to overlap in a barchart?

查看:190
本文介绍了如何防止两个标签在条形图中重叠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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




  week = c(0,1,1,1,2,3, ,4,5)
状态= c('发货','发货','发货','发货','不发货','发货','发货','发货',' (周)=周,状态=状态)

p < - qplot(因子(周),数据= dat,geom =bar,fill = factor(状态))
p < - p + geom_bar()
#下面是最重要的一行,它是显示值
p <-p + stat_bin(aes(label = ..count ..),geom =text,vjust = -1,size = 3)
p


解决方案

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

一些样本数据(cod (b)

  set.seed(654)
week < - sample(0)由Didzis Elferts的答案启发: :9,3000,rep = TRUE,prob = rchisq(10,df = 3))
status < - 因子(rbinom(3000,1,0.15),labels = c(已发货已发送))
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 =身份)+
scale_y_continuous(休息= 100 * -1:5,
标签= 100 * c(1,0:5))+
geom_text(aes(y = sign )* max(V1)/ 30,label = abs(V1)))

/ p>



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


The image below shows a chart that I created with the code below. I highlighted the missing or overlapping labels. Is there a way to tell ggplot2 to not overlap labels?

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

解决方案

You can use a variant of the well-known population pyramid.

Some sample data (code inspired by Didzis Elferts' answer):

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)

Compute count scores for each week, then convert one category to negative values:

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

Draw the plot. Note that the y-axis labels are adapted to show positive values on either side of the baseline.

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

The plot:

For production purposes you'd need to determine the appropriate y-axis tick labels dynamically.

这篇关于如何防止两个标签在条形图中重叠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆